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
Solved

Second call to open spreadsheet fro Access fails.

Posted on 2014-04-30
3
361 Views
Last Modified: 2014-04-30
I use the VBA below to open an Excel workbook.
It opens and I close it again manually. The Excel window also closes.
If I try the process again, It does not open.
If I look on windows taskmanager, I see there is an Excel instance under processes. If I close that, the VBA works again one time.
Advice please.

My code is:
Private Sub cmdOpenExcelWorkbook_Click()
'   Open Excel Workbook
Dim excelApp As Excel.Application
Dim wb As Excel.Workbook
Dim WorkbookToOpen As String

    WorkbookToOpen = Me.txtOrigATD      'A textbox on my form
    Set excelApp = CreateObject("Excel.Application")
    Set wb = Excel.Workbooks.Open(WorkbookToOpen)
    excelApp.Visible = True
                '    wb.ActiveSheet.Range("d1").Value = 100     'Passes a value to a cell on the spreadsheet.

    Set excelApp = Nothing
    Set wb = Nothing
End Sub

Open in new window

0
Comment
Question by:Fritz Paul
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40031677
Excel automation can be tricky.  You must close and release all of the objects you create during your Access module in the proper order.

Try inverting lines 13 and 14 in the above.

Are you certain you want to create a new instance of Excel, even if one is already open?  Or would it be better to use the version that is already open.  If so you could try:

On Error Resume next
Set ExcelApp = GetObject(, "Excel.Application")
if err.number <> 0 then Set ExcelApp = CreateObject("Excel.Application")
On Error goto ProcError  '<= whatever you are using for an error handler.
0
 

Author Closing Comment

by:Fritz Paul
ID: 40031703
Thanks, it's working.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40031752
glad I could help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

808 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