Link to home
Start Free TrialLog in
Avatar of BenniBoy
BenniBoyFlag for United States of America

asked on

Open a file stored in SharePoint from Excel using VBA

How do I open a file stored in a SharePoint document library using Excel VBA and then write to the file?  

I am able to open the file but the code to transfer values from an array to the open template fail to work.
Variable Declaration:
LoadFilePath = "http://server//file path/filename.xls"
 
Workbooks.Open (LoadFilePath)
  
For arrayLnItem = 1 To lastRow
        With Workbooks(LoadFileName).Worksheets(1)
            .Cells(LoadFileRow, 1).Value = flatFileArray(arrayLnItem, 1)
            .Cells(LoadFileRow, 2).Value = flatFileArray(arrayLnItem, 2)
            .Cells(LoadFileRow, 3).Value = flatFileArray(arrayLnItem, 3)
        End With
Next arrayLnItem

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry,
Dim objWB As Workbooks
should have read
Dim objWB As Workbook
 
Avatar of BenniBoy

ASKER

Thank you for your help...worked great.
For my own understanding, can you explain why the original code that I used did not work?
To clarify, the original code you posted did not open the workbook up into a container/object variable that you could make reference to in order to manipulate the file after it was opened.
Using Workbooks(LoadFileName).Open will just open the file in Excel, but give you no further control over it.  Because of that, your code would not understand what you are referring to when you used Workbooks(LoadFileName).Worksheets(1).
Using Set objWB = Workbooks.Open(LoadFilePath) will place the existece of the Excel file within the objWB object which (given how it was defined - i.e. as a Workbook object) will allow you to control what is done to the file.
I hope that explanation helps!