Opening Word from Outlook

  • Thread starter Southern at Heart
  • Start date
S

Southern at Heart

I have a command button on my contact form in Outlook. In it's code, I'd
like to have it open Word, but with a template I specify, strMyTemplate...
Can I do this?
thanks,
Southern@Heart
 
G

Graham Mayor

This is more an Outlook topic than a Word one, however the following Outlook
code should do the trick.
Note that the extra wdWindowState lines ensure that Word opens in front of
Outlook. If Word is already running it will use that rather than open
another instance. The extra unused code at the end closes Word after running
any processes where indicated. Ensure that the Word Object Library is
checked in the vba references.

Sub OpenWord()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim WordWasNotRunning As Boolean
Dim strMyTemplate As String
strMyTemplate = "D:\Template Path\Template Name.dot"

WordWasNotRunning = False
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
WordWasNotRunning = True
End If
Set wdDoc = wdApp.Documents.Add strMyTemplate

wdApp.WindowState = wdWindowStateMinimize
wdApp.Visible = True
wdApp.WindowState = wdWindowStateNormal

' do whatever else you want here -e.g. wdApp.Run YourWordMacro etc.


' and when you're finished
'If WordWasNotRunning = True Then
' wdApp.Quit
'End If
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Southern at Heart

Great! One more thing, after I start a new document from the template, I
need to run the function Paste Envelope, that is located in the vba of that
Word template. How do I call this function from my Outlook code?
thanks;
southern@heart
 
S

Southern at Heart

I'm trying this code you wrote, but I'm getting an error on the line:
Set wdDoc = wdApp.Documents.Add strMyTemplate
....when I paste this code in, that line shows up red. Is the syntax on it
not quite right?

....to run my macro in Word, I see this line, but not sure of the syntax:
' do whatever else you want here -e.g. wdApp.Run YourWordMacro etc.

My macro is called Paste_Envelope, so would it be

wdApp.Run("Paste_Envelope")

I can't test this theory because that other line is giving me an error.
thanks.
 
G

Graham Mayor

Oops

Try
Set wdDoc = wdApp.Documents.Add(strMyTemplate)
then add your other line where indicated.
wdApp.Run("Paste_Envelope")


If you are creating envelopes from contacts then you might be interested in
the method used at http://www.gmayor.com/Macrobutton.htm and the automated
envelope templates that you can download from my web site also.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top