Access to run Excel Macros

Mykal

New Member
Joined
Dec 5, 2003
Messages
5
I have access opening a excel document and running one macro. I leave the excel document open so I can run another macro later. When the time comes to activate excel and run macro 2, I have a problem getting the second macro to run. I can get excel window activated. I thank you in advance for any assistance provided.

Thanks.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
what happens when you try to run the maco? do you still have a reverence to the excel file in code?

Peter
 
Upvote 0
When I get to the second macro I see the excel window come to the front and everything just stops. When I go back to access, the error in the message box says:

Run-time error'91':
Object variable or with block variable not set

Thank you.
 
Upvote 0
Can you post your code? It could be that the variable for Xl is going out of scope between sessions.

Peter
 
Upvote 0
Here is basically the code that I am using right now.

Private Sub XMacro1()
Dim ExcelApp As Object
Dim fNameAndPath As String

fNameAndPath = "\\comp1\Planning\Database\Macro.xls"
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.WorkBooks.Open (fNameAndPath)
ExcelApp.Visible = True
vardirpath = Me.DirPath

WordApp.Run "Macro1", vardirpath


End Sub

Private Sub XMacro2()
Dim ExcelApp As Object

VarTI1 = DFirst("[TI]", "List")
ExcelApp.Activate "Macro2", VarTI1
End Sub

Thank you for you time and assistance with this.
 
Upvote 0
In the second macro you have not referenced Excel.

Do you need to keep Excel open after running the first macro?

If not this might work:

Code:
Private Sub XMacro1()
Dim ExcelApp As Object
Dim fNameAndPath As String

    fNameAndPath = "\\comp1\Planning\Database\Macro.xls"
    
    Set ExcelApp = CreateObject("Excel.Application")
    
    ExcelApp.Workbooks.Open (fNameAndPath)
    
    ExcelApp.Visible = True
    
    vardirpath = Me.DirPath
    
    ExcelApp.Run "Macro1", vardirpath
    
    ExcelApp.Close

End Sub

Private Sub XMacro2()
Dim ExcelApp As Object

    fNameAndPath = "\\comp1\Planning\Database\Macro.xls"
    
    Set ExcelApp = CreateObject("Excel.Application")
    
    ExcelApp.Workbooks.Open (fNameAndPath)
    
    ExcelApp.Visible = True
    
    VarTI1 = DFirst("[TI]", "List")

    ExcelApp.Activate "Macro2", VarTI1
    
    ExcelApp.Close
    
End Sub

BTW what do the macros do? Is it not something that could be done in Access?
 
Upvote 0
I think that you just need to move your ExcelApp to the Declaration Section at the top of the module. I would also set it to nothing at the end of the second macro to free up the memory again. try:-
Code:
Dim ExcelApp As Object 

Private Sub XMacro1() 
Dim fNameAndPath As String 
fNameAndPath = "\\comp1\Planning\Database\Macro.xls" 
Set ExcelApp = CreateObject("Excel.Application") 
ExcelApp.WorkBooks.Open (fNameAndPath) 
ExcelApp.Visible = True 
vardirpath = Me.DirPath 

WordApp.Run "Macro1", vardirpath 


End Sub 

Private Sub XMacro2() 
VarTI1 = DFirst("[TI]", "List") 
ExcelApp.Activate "Macro2", VarTI1 
set ExcelApp = nothing
End Sub

HTH

Peter
 
Upvote 0

Forum statistics

Threads
1,221,808
Messages
6,162,097
Members
451,742
Latest member
JuanMark10

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top