Solved

Open a file stored in SharePoint from Excel using VBA

Posted on 2009-04-08
4
11,495 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:BenniBoy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 23

Accepted Solution

by:
irudyk earned 500 total points
ID: 24097519
Try something like the following to see if that works for you:
Variable Declaration:
LoadFilePath = "http://server//file path/filename.xls"
 
Dim objWB As Workbooks
Set objWB = Workbooks.Open(LoadFilePath)
  
For arrayLnItem = 1 To lastRow
        With objWB.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

0
 
LVL 23

Expert Comment

by:irudyk
ID: 24097541
Sorry,
Dim objWB As Workbooks
should have read
Dim objWB As Workbook
 
0
 

Author Closing Comment

by:BenniBoy
ID: 31568027
Thank you for your help...worked great.
For my own understanding, can you explain why the original code that I used did not work?
0
 
LVL 23

Expert Comment

by:irudyk
ID: 24100894
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!

0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question