I am using the below code, to open the Excel file from SharePoint.
I am Little confused how, I should code, for the accessing the file from an Folder in SharePoint.
The Excel file, i Need is stored in ; SharePoint>> Document >> Test >> Data >>July
and the link to my SharePoint is like this, https://forum/content/008200/default.aspx?RootFolder=%2Fcontent%2F00008200%20Documents%2DTest%2F0001%20%29&FolderCTID=0x01200083BC38D90EC5674491B520CC48282737&View={00112127-0FE6-44A4-A5FB-86BC6C4E835B}&InitialTabId=Ribbon%2EDocument&VisibilityContext=WSSTabPersistence
Could someone help me, How i should code, to acess an Excel file from a Folder in SharePoint.
I have attached a Piece of code, which i reffered it from the Forum. and would be helpful, if you could comment the lines, if you can suggest me a code.
Sub Share()
Dim SWB As Workbook
Dim WB As Variant
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "https://forum/Content/008200/default.aspx" & "\"
.AllowMultiSelect = False
.show
For Each WB In .SelectedItems
Set SWB = Workbooks.Open(WB)
Next
End With
If SWB Is Nothing Then Exit Sub
End Sub
i don't know , how can you with vb but i assume your file in Document Library if it is you can use this code to get document.
SPFile myExcelFile = SPContext.Current.Web.GetFile("/Documents/Test/Data/July/excelFile.xslx");
Stream excelFileStream = myExcelFile.OpenBinaryStream();
Related
My code is simple.
I have copied it off of the VBA example site: https://learn.microsoft.com/en-us/office/vba/api/office.filedialog
Additionally, every other variation I can find online has the same problem.
All I am attempting is to open a File Dialogbox (Similar to a file explorer) where a user can select a folder or files.
However, I carry on getting this error:
It then highlights this line of code:
Additionally, I have added references to Microsoft Office 16.0 Object Library, and everything I can think of:
How do I fix this or get this to Run?
Thank You
Here is the Code:
Sub Main()
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is aString,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is aString that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example displays the path in a message box.
MsgBox "The path is: " & vrtSelectedItem
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub
Disclaimer: I'm sure there is a better/more correct way to do this but it works for me in VBA for SolidWorks. I am not a real programmer. Just trying to help because I know how few resources there are on VBA programming in SW.
All I really did was add the Excel Object library and change Application to Excel.Application.
Despite what GSerg said, there can be an Excel.Application if you add the Excel object library as a reference.
Despite macropods condescension, you are not stuck with what SW provides or the generic file browser and you don't need to re-install Office or SW. The generic file browser doesn't show your Quick Access items. SolidWorks doesn't run on Mac OS. His "answer" does not work in SW VBA.
Here is my working function (started with code from here):
Function GetFolder(Title As String)
Dim folder As FileDialog
Dim selected_folder As String
Set folder = Excel.Application.FileDialog(msoFileDialogFolderPicker)
With folder
.AllowMultiSelect = False
.ButtonName = "Ok"
.Filters.Clear
.InitialFileName = Excel.Application.DefaultFilePath
.Title = Title
If .Show <> -1 Then GoTo NextCode
selected_folder = .SelectedItems(1)
End With
NextCode:
Debug.Print selected_folder
GetFolder = selected_folder
Set folder = Nothing
End Function
I added the Excel & Office Object Libraries to the default VBA SW:
If you have a way to do this that shows the Quick Access items without having to use the Excel Object Library I'm very interested!
I assume you're using a Windows system, since Application.FileDialog is not available/fully supported on Macs.
I suggest you start over, without adding a plethora of irrelevant references. The following works just fine without adding any to the default references:
Sub Demo()
Dim fd As FileDialog, vrtSelectedItem As Variant
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
MsgBox "The path is: " & vrtSelectedItem
Next
End If
End With
Set fd = Nothing
End Sub
If the code still doesn't work for you, a repair/reinstall of Office or your SolidWorks installation is most likely called for.
For code that works on Macs and PCs, see: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-msoffice_custom-mso_365hp/showing-dialogs-word-for-mac-vba/513ea974-378d-4ebe-95c3-a0221a9287ff
I am trying to copy the values of one column in a sheet to a text file. The code I currently have causes runtime error 434.
Sheets("Output to fcf.1").Columns("A").SaveToText "P:\4_Calcs\02. Flag Mapping\test_.txt"
If I try and save the whole sheet
Sheets("Output to fcf.2").SaveToText "P:\Clear Project Drive\CLE10276 AWS SMP Model Assessmnts\4_Calcs\02. Flag Mapping\test2_.txt"
I get the entire sheet converted into text rather than just the text in the sheet. Is there a simple way to do this?
Thanks in advance!
Not sure which Excel version you have but I don't see a method for SaveToText.
But this procedure should work, or at least get you started...
Sub SaveColumn(sheetName As String, columnName As String, fileName As String)
Dim cell
Dim fso
Dim file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile(fileName, True)
For Each cell In Sheets(sheetName).Columns(columnName).Cells
If cell.Value <> "" Then
file.WriteLine cell.Value
End If
Next
file.Close
Set file = Nothing
Set fso = Nothing
End Sub
To call it...
SaveColumn "Output to fcf.1", "A", "P:\4_Calcs\02. Flag Mapping\test_.txt"
This is designed to be used as a macro.
Step by step guide:
1) From excel, hit Alt+F11 on your keyboard.
2) From the menu bar, click Insert, then Module
3) Copy and paste the code provided below into the new module that opens.
NOTE: DocPath = "C:\docs\data.txt" should be wherever you want the output file saved, including the file's actual name. Remember, the folder you want the output file to be located in should ALREADY exist. This does not create the folder if it can't be found.
4) From the menu bar, click Tools, then References. Make sure both "Microsoft Office 14.0 Object Library" as well as "Microsoft Word 14.0 Object Library" are checked, and hit okay (See screenshot for details)
5) Save the document as an .xlsm file (This file type supports Macros)
6) Close the VBA editor. Back in Excel, on the ribbon click View and then Macros. Your new macro should be in the list as ExportToTXT
7) Select it and hit run.
Sub ExportToTXT()
Dim DocPath As String
Dim MsgBoxCompleted
Columns("A").Select
Dim AppWord As Word.Application
Set AppWord = CreateObject("Word.Application")
AppWord.Visible = False
Selection.Copy
DocPath = "C:\docs\data.txt"
'Create and save txt file
AppWord.Documents.Add
AppWord.Selection.Paste
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
Application.CutCopyMode = False
AppWord.Quit (wdDoNotSaveChanges)
Set AppWord = Nothing
MsgBoxCompleted = MsgBox("Process complete.", vbOKOnly, "Process complete")
End Sub
Good luck, and if you have any questions, don't hesitate to ask.
NOTE: These directions might seem overly simplified for your skill level, but I wrote the answer like this to potentially help others in the future.
EDIT
Change
DocPath = "C:\docs\data.txt"
to
DocPath = "C:\docs\data.fcf"
And change
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath, FileFormat:=wdFormatText
to
AppWord.ActiveDocument.SaveAs2 Filename:=DocPath
The output file will be .fcf format. Whether or not it will open properly is something I'm not sure of. You'd have to test in the program you're using.
I have three files one is a excel file enabled with macro where my macro is(1), the csv file to run the macro on(2). The new csv file that would be opened(3)
I am new to userform I created a web browser control and was able to initialize in the userform and added the code
Private Sub UserForm_Initialize()
Me.WebBrowser1.Navigate "http://sharepoint_site.aspx"
End Sub
now when I click on the required csv file I get file download. There how do I just open the file and make this newly opened csv file as active? There are many csv files on the sharepoint site.The user selects a specific file and gets a file download box where it should jusst open that csv file. The reason for using userform as suggested by #David was to better control the newly opened CSV file and have the name of the file stored to perform the next steps of the code rather than file 2 where the macro is run to be the active workbook.Below code was my previous code which was part of a case statement.
Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
With IE
.Visible = True
.navigate ("https://site.aspx")
MsgBox "Select the file and click open file"
Here obviously activated the file (2) where the macro ran but wanted to activate the newly opened file. Any help on this is greatly appreciated & thank you in advance.
Try something like this instead of the WebBrowser control, my apologies for pointing you in the wrong direction:
Sub foo()
Dim SummaryWB As Workbook
Dim vrtSelectedItem As Variant
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "https:\\your_sharepoint\team\folder" & "\"
.AllowMultiSelect = False
.Show
For Each vrtSelectedItem In .SelectedItems
Set SummaryWB = Workbooks.Open(vrtSelectedItem)
Next
End With
If SummaryWB Is Nothing Then Exit Sub
Call SomeOtherMacro(SummaryWB)
End Sub
So then you have some other macro that will process this workbook, you send the workbook to it like the above Call statement, and make sure that the other procedure accepts a Workbook:
Sub SomeOtherMacro(wb as Workbook)
' This macro will do something to the workbook
wb.Worksheets(1).Select
MsgBox wb.Name & " sheet 1 is now selected!"
End Sub
Modified from this answer.
I have built a macro for building and running SQL Queries. I am pretty happy with it so far. The only thing I would like to add is the ability in Windows to double click a .sql file and it opens inside the macro. Sample is below:
This is the code that opens an SQL file when Load Query is pressed.
Sub LoadQuery()
Dim fNameAndPath As Variant
fNameAndPath = Application.GetOpenFilename(FileFilter:="SQL Query Files (*.sql), *.sql", Title:="Select File To Be Opened")
If fNameAndPath = False Then Exit Sub
Open fNameAndPath For Input As #1
Sheets("Sheet1").SQL_Query = Input$(LOF(1), 1)
Close #1
Sheets("Sheet1").SQLFileName.Caption = fNameAndPath
End Sub
Is this even possible? I don't think it will be but thought I would check with you guys first.
What have I tried so far? Nothing, because I don't even know where to start, Uncle Google didn't hook a brother up in fact he just confused me even more.
Using the ideas below I went with this:
Option Explicit
Dim xlApp, xlBook, fNameAndPath
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("G:\Analytics Reporting Archive\SQL Client.xlsm", 0, True)
xlApp.Open WScript.Arguments(0) For Input As #1
xlBook.Sheets("Sheet1").SQL_Query = Input$(LOF(1), 1)
xlApp.Close #1
xlBook.Sheets("Sheet1").SQLFileName.Caption = fNameAndPath
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Quit
This didn't work, it didn't like opening the text file (sql file) so I went with creating a small routine in the Excel app:
Sub LoadQueryDBLClick(QueryFileName As String)
Open QueryFileName For Input As #1
Sheets("Sheet1").SQL_Query = Input$(LOF(1), 1)
Close #1
Sheets("Sheet1").SQLFileName.Caption = fNameAndPath
End Sub
This is then called in the VBS like so:
Option Explicit
Dim xlApp, xlBook, fNameAndPath
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("G:\Analytics Reporting Archive\SQL Client.xlsm", 0, True)
xlApp.Run "LoadQueryDBLClick " & WScript.Arguments(0)
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Quit
Brilliant right?
NO :(. It "would" work, of that I am confident (and extremely grateful to you guys that posted replies for me that got me this far) but alas Citrix strikes again:
Error: ActiveX component can't create object: 'Excel.Application'
I am confident that you guys have solved this in that Citrix is now the issue.
See here for how to create a custom extension and associate it with an action.
https://superuser.com/questions/406985/programatically-associate-file-extensions-with-application-on-windows
What might work in your case is to associate the extension with a vbs file which takes the launching SQL filepath as a parameter and opens your Excel file using automation, then loads the SQL file into the workbook.
You'll want to right-click on an SQL file and select Open With. Choose, or browse to Excel.exe. Set it as the default application to open SQL files with.
This will open your SQL files in Excel.
From here you'll need to teach your macro to detect when it was launched by an SQL file and run as desired.
I know this has been asked before, but for the life of me I cannot get this simple code to work. I keep getting the compile error "User-defined type not defined" on Dim wbopen As Workbook
line. I know the workbook has to be open to define it, and the file path should be Z:\Manufacturing\02- Schedules\01- Buffer Prep
while the file name is the only .xls in the folder. Why is this happening? Also, this is executing in a Word file. Not sure if that matters. Thanks everyone!
Sub fileopen()
Dim wbopen As Workbook
Dim strFileName As String
Dim strFilePath As String
strFilePath = "Z:\Manufacturing\02- Schedules\01- Buffer Prep\"
strFileName = Dir(strFilePath & "*.xls")
Set wbopen = Workbook.Open(strFileName)
End Sub
It does matter that it is a Word Document. You have to add a reference to Microsoft Excel Object Library. From the top bar:
Tools -> References -> find Microsoft Excel [version number] Object Library and check it.