Solved

Second call to open spreadsheet fro Access fails.

Posted on 2014-04-30
3
363 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
[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
  • 2
3 Comments
 
LVL 48

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 48

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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: …

707 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