Solved

Reference Excel Spreadsheet with VBA

Posted on 2007-11-15
5
1,408 Views
Last Modified: 2008-02-01
I'm using the below code to mess with an open Excel spreadsheet in VBA. It works fine unless my file rpt_OverviewforExport.xls is not the only Excel file open. How to make this more robust?
Thanks,

mv

    Dim gobjExcel As Excel.Application
    Dim WS As Excel.Worksheet
    Set gobjExcel = GetObject(, "Excel.Application")
    Set WS = gobjExcel.Workbooks("rpt_OverviewforExport.xls").Sheets(1)
0
Comment
Question by:Michael Vasilevsky
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:spattewar
ID: 20291186
you can try something like

   Dim gobjExcel As Excel.Application
    Dim WS As Excel.Worksheet
    Set gobjExcel = GetObject(, "Excel.Application")
    on error resume next
    Set WS = gobjExcel.Workbooks("rpt_OverviewforExport.xls").Sheets(1)
   If WS is nothing then
       Msgbox "Kindly keep the required sheet open"
       Exit Sub/Function
   End If
    ' continue with your error handling
    On Error Goto <Errorhandler>

Let me know.
$wapnil
0
 
LVL 10

Author Comment

by:Michael Vasilevsky
ID: 20291209
Well I open the workbook programmatically so the problem is not that it's closed, the problem is that if the user has another instance of Excel running I get a "Subscript out of range" error.

Any good way to handle that? Maybe I'm just referencing the workbook I'm interested in wrong?
Thx,

mv
0
 
LVL 22

Expert Comment

by:spattewar
ID: 20291249
Ok.

When you open the workbook programmitically do you not store the reference of the workbook and the excel instance that is used to open the workbook. Can you paste the code that is used to open the workbook programmitically.thanks.

$wapnil
0
 
LVL 10

Author Comment

by:Michael Vasilevsky
ID: 20291790
See below. I don't store it. That's another issue (and another open EE question) I have: how to get the file path and name? 'Cause with the below if a user selects a different name or path than the hardcoded, it's obviously not going to work.
Thanks!

mv

stDocName = "rpt_OverviewforExport"
    DoCmd.OpenReport stDocName, acPreview
    DoCmd.OutputTo acReport, stDocName
    Set xlApp = CreateObject("excel.application")
    xlApp.Visible = True
    xlApp.Workbooks.Open ("C:\TVS\rpt_OverviewforExport.xls")
    xlApp.Application.ActiveWorkbook.RunAutoMacros (xlAutoOpen)
    Call FormatOverviewforExportRpt
    DoCmd.Close acReport, stDocName, acSave
0
 
LVL 22

Accepted Solution

by:
spattewar earned 500 total points
ID: 20292288
Hi,

You can do the following.

1) Ask the user to pick up the file by using the windows function.
Dim sListFileName As String
sListFileName = Application.GetOpenFilename( _
                        ("Excel Files (*.xls), *.xls"), _
                        1, _
                        "Select a Trade List", _
                        "Select", _
                        False)
' and then
Dim xlWB as Excel.Workboook
set xlWB = xlApp.Workbooks.Open (sListFileName)

2) To store the reference to the workbook and the application you can pass the objects as parameter to the function.
for e.g.
Call FormatOverviewforExportRpt <<xlApp, xlWB>>

$wapnil
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now