Solved

Reference Excel Spreadsheet with VBA

Posted on 2007-11-15
5
1,444 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

679 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