Solved

Second call to open spreadsheet fro Access fails.

Posted on 2014-04-30
3
357 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS Access Bound Objects. 6 28
Access 2010 Run Time Not Using Custom Toolbar 7 17
Error in SQL Query 36 39
Max per month 3 15
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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

743 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