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
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

Signature
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> 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
Southern at Heart - 06 Mar 2008 13:53 GMT
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
> This is more an Outlook topic than a Word one, however the following Outlook
> code should do the trick.
[quoted text clipped - 39 lines]
> > thanks,
> > Southern@Heart
Southern at Heart - 06 Mar 2008 14:01 GMT
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.
Graham Mayor - 06 Mar 2008 15:16 GMT
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.

Signature
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> I'm trying this code you wrote, but I'm getting an error on the line:
> Set wdDoc = wdApp.Documents.Add strMyTemplate
[quoted text clipped - 11 lines]
> I can't test this theory because that other line is giving me an
> error. thanks.