Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reference Excel Spreadsheet with VBA

Posted on 2007-11-15
5
Medium Priority
?
1,473 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
[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
  • 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

715 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