?
Solved

Second call to open spreadsheet fro Access fails.

Posted on 2014-04-30
3
Medium Priority
?
368 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 49

Accepted Solution

by:
Dale Fye earned 2000 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 49

Expert Comment

by:Dale Fye
ID: 40031752
glad I could help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

840 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