objExcel.Application.Run Parameter Syntax

Azerty99

New Member
Joined
Mar 3, 2009
Messages
38
Calling Excel from Word.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I have a sub in Excel called ChooseRegion, Excel syntax:<o:p></o:p>
ChooseRegion 2<o:p></o:p>
Or <o:p></o:p>
Call ChooseRegion(2)<o:p></o:p>
<o:p></o:p>
I have a Sub called Reset which resets Excel to the top of the list:<o:p></o:p>
Excel:<o:p></o:p>
Call ChooseRegion(0)<o:p></o:p>
This works fine :)<o:p></o:p>
<o:p></o:p>
Syntax From Word:<o:p></o:p>
objExcel.Application.Run ("Reset")<o:p></o:p>
This runs within Excel<o:p></o:p>
Call ChooseRegion(0)<o:p></o:p>
This works fine :)<o:p></o:p>
<o:p></o:p>
But<o:p></o:p>
Syntax From Word:<o:p></o:p>
objExcel.Application.Run ("ChooseRegion(2)")<o:p></o:p>
Doesn't run the macro :crash:<o:p></o:p>
<o:p></o:p>
The help suggests the following syntax:<o:p></o:p>
objExcel.Application.Run ("ChooseRegion" ,"2") :crash:<o:p></o:p>
But the VBA editor claims that this is bad syntax and flags it as Red<o:p></o:p>
<o:p></o:p>
Details about the functionality:<o:p></o:p>
The chooseRegion macro sets the filter name in a data matrix for a chart. Word opens Excel, retrieves data from an External Database, filters the data and plots a chart. It then copies this chart into Word. There are 12 Regions. <o:p></o:p>
<o:p></o:p>
Office 2003, Windows XP<o:p></o:p>
<o:p></o:p>
Any ideas as the the correct syntax?

This is in Excel as a Module:
Public Sub ChooseRegion(RegionNum As Integer)
Sheets("Chart").Select
Range("SelRegion").Value = RegionNum
RegionName = Sheets("Info").Range("RegionName") 'This is a VLookup
Selection.AutoFilter Field:=2, Criteria1:=RegionName
Debug.Print "Chosen Region: " & RegionName
End Sub<o:p></o:p>
 
Untested**

Either of these should be fine:

Code:
Call objExcel.Run("ChooseRegion", 2)

Code:
objExcel.Run "ChooseRegion", 2
 
Upvote 0
Although now my code runs fine, I have found that the first time I call an Excel Sub from Word, it ignores the call. Thereafter it runs everything else. So I have inserted a dummy call just to get past that "funny".

I do hope this is not indicative of something else going wrong!
 
Upvote 0
Hi Jon,

Sounds strange to me. If you post the Word VBA and Excel VBA code I'd be happy to have a quick look at it for you?
 
Upvote 0
Hi Jon,

Sounds strange to me. If you post the Word VBA and Excel VBA code I'd be happy to have a quick look at it for you?

Colin,

I've been running for a few days now, using the calls dozens of times, with no issues (YET!). At the risk of being over confident by even thinking of posting here - you know how Dr. Murphy behaves!

Word (Module):
Code:
Sub InsertCharts(WidgetPath As String, exWorkBook As String)
    Dim objExcel As New Excel.Application
    Set wb = objExcel.Workbooks.Open(WidgetPath & exWorkBook)
    Call objExcel.Run("ResetMacro") ' First instance does not work...
    Call objExcel.Run("ResetMacro") ' This works fine
    wb.Close SaveChanges:=True
    Set wb = Nothing
End Sub

Excel (Module):
Code:
Public Sub ResetMacro()
    'Reset to the top of the list
    Call ChooseRegion(0)
End Sub
 
Public Sub ChooseRegion(RegionNum As Integer)
    'Choose Region According to Region Number
    Sheets("Chart").Select
    Range("SelRegion").Value = RegionNum
    RegionName = Sheets("Info").Range("RegionName")
    Selection.AutoFilter Field:=2, Criteria1:=RegionName
    Debug.Print "Chosen Region: " & RegionName
End Sub
The issue might be that I have the Excel VBA code in an Add-In. I.e. I use this same code across many workbooks. There is no VBA code in the work book itself with the chart in it.

Obviously this is not all the code, as I have code to copy the plotted chart into the corresponding bookmarked place in the Word document, but hopefully I have extracted what is needed.
 
Upvote 0
I haven't tested, but if you change your Word module to this does it work 1st time?
Code:
Sub InsertCharts(WidgetPath As String, exWorkBook As String)
    Dim objExcel As Excel.Application
    Dim wb as Excel.Workbook
 
    Set objExcel = New Excel.Application
    Set wb = objExcel.Workbooks.Open(WidgetPath & exWorkBook)
 
    Call objExcel.Run("ResetMacro") ' This works fine
 
    wb.Close SaveChanges:=True
    Set wb = Nothing
    objExcel.Quit
    Set objExcel = Nothing
End Sub
 
Last edited:
Upvote 0
Tried this.

Still no luck!

Code:
Sub InsertCharts(WidgetPath As String, exWorkBook As String)
    Dim objExcel As Excel.Application
    Dim wb As Excel.Workbook
 
    Set objExcel = New Excel.Application
    Set wb = objExcel.Workbooks.Open(WidgetPath & exWorkBook)
    objExcel.Visible = True
 
    Call objExcel.Run("ResetMacro") ' Does not work
    Call objExcel.Run("ResetMacro") ' This works fine

But will leave it as your fix above cured the main problem!
 
Last edited:
Upvote 0
Hi,

Just to be sure... it's working okay now?

Yes Colin. Your fix on the Call fixed the main communication problem between Word and Excel.
The latter problem with the first call not working is not really a problem, if ya know about it! So problem solved. Documenting it, well that's another story...

Thank you once again.

Jon
 
Upvote 0

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