Solved

Reference Excel Spreadsheet with VBA

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

705 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

20 Experts available now in Live!

Get 1:1 Help Now