Accessing Word From a Command Button

Jack_58

Board Regular
Joined
Dec 3, 2002
Messages
208
I have a Microsoft Access Database with a command Button that I need to be able to a Open Microsoft Word document and then Run a Word Macro.

So what I would need is the code to Open Word, Open the File then Execuite the Macro, then return the Word document to normal.

Sure Hope someone can help me with this.



Thanks



Jack
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
This code will run a publicly declared sub routine 'test' contained in a module of document 'test.doc'

Code:
Public Sub RunWordMacro()
On Error GoTo Err_OpenGuide

    Dim oApp As Object

    Set oApp = CreateObject("Word.Application")
    oApp.Documents.Add "c:\Test.doc"
    oApp.Run "test"
    Set oApp = Nothing
Exit_OpenGuide:
    Exit Sub

Err_OpenGuide:
    MsgBox Err.Description
    Resume Exit_OpenGuide
    
End Sub
 
Upvote 0
Norie said:
This code will run a publicly declared sub routine 'test' contained in a module of document 'test.doc'

Code:
Public Sub RunWordMacro()
On Error GoTo Err_OpenGuide

    Dim oApp As Object

    Set oApp = CreateObject("Word.Application")
    oApp.Documents.Add "c:\Test.doc"
    oApp.Run "test"
    Set oApp = Nothing
Exit_OpenGuide:
    Exit Sub

Err_OpenGuide:
    MsgBox Err.Description
    Resume Exit_OpenGuide
    
End Sub

Looks Great, a Couple of Questions,

In the SetoApp = CreateObject("Do I place the name of my file here")

oApp.Documents.Add "Am I correct in that this is the path to the document?)

OApp Runn = "Name of Macro"..do I need the Quote marks


Lastly,

Will this code work on a command button on Click Procedure?


Thanks Again!!!



Jack
 
Upvote 0
In the SetoApp = CreateObject("Do I place the name of my file here")

No, what this line does is create an object which is an instance of Word

oApp.Documents.Add "Am I correct in that this is the path to the document?)

Yes

OApp Runn = "Name of Macro"..do I need the Quote marks

Yes

Will this code work on a command button on Click Procedure

As far as I know it should.

The way I was originally using it was via a commandbar control which opened up a sorta help file for the user.

I don't know how running a macro in the document might affect things.

One thing I wanted to ask was what the macro actually does and what you meant by this

return the Word document to normal.

Hope you understand all that.
 
Upvote 0
Norie said:
In the SetoApp = CreateObject("Do I place the name of my file here")

No, what this line does is create an object which is an instance of Word

oApp.Documents.Add "Am I correct in that this is the path to the document?)

Yes

OApp Runn = "Name of Macro"..do I need the Quote marks

Yes

Will this code work on a command button on Click Procedure

As far as I know it should.

The way I was originally using it was via a commandbar control which opened up a sorta help file for the user.

I don't know how running a macro in the document might affect things.

One thing I wanted to ask was what the macro actually does and what you meant by this

return the Word document to normal.

Hope you understand all that.

How would I be able to run another Macro after the first one has completed Automatically?


Thanks



Jack
 
Upvote 0
Try this:

Code:
Public Sub RunWordMacro() 
On Error GoTo Err_OpenGuide 

    Dim oApp As Object 

    Set oApp = CreateObject("Word.Application") 
    oApp.Documents.Add "c:\Test.doc" 

    oApp.Run "test" 
    oApp.Run "test2"

    Set oApp = Nothing 

Exit_OpenGuide: 
    Exit Sub 

Err_OpenGuide: 
    MsgBox Err.Description 
    Resume Exit_OpenGuide 
    
End Sub
 
Upvote 0
Norie said:
Try this:

Code:
Public Sub RunWordMacro() 
On Error GoTo Err_OpenGuide 

    Dim oApp As Object 

    Set oApp = CreateObject("Word.Application") 
    oApp.Documents.Add "c:\Test.doc" 

    oApp.Run "test" 
    oApp.Run "test2"

    Set oApp = Nothing 

Exit_OpenGuide: 
    Exit Sub 

Err_OpenGuide: 
    MsgBox Err.Description 
    Resume Exit_OpenGuide 
    
End Sub

How do I get the Document to Return to the Home POsition and Unhighlight?


Thanks Again


Jack
 
Upvote 0
What I would do is in Word record a macro that does what this:

to Return to the Home Position and Unhighlight

Put it in the document you are using.

Then use oApp.Run "MyMacro", where MyMacro is the macro you recorded in word.
 
Upvote 0
Norie said:
What I would do is in Word record a macro that does what this:

to Return to the Home Position and Unhighlight

Put it in the document you are using.

Then use oApp.Run "MyMacro", where MyMacro is the macro you recorded in word.

Listeed below is the Macro I have

Sub Line_Space()
Selection.WholeStory
With Selection.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = 8
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
End With
End Sub

Where would I use this option?


Thanks Again



Jack
 
Upvote 0

Forum statistics

Threads
1,221,713
Messages
6,161,463
Members
451,708
Latest member
PedroMoss2268

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