Code to start Word

  • Thread starter Thread starter Southern at Heart
  • Start date Start date
S

Southern at Heart

Would there be vba code that I could assign to a command button that would
open word to create a new word document via the template strMyTemplate?
thanks,
Southern@Heart
 
I'm trying to use this code again, and it won't work... Here's what I have:

Sub Standard_Letter()
Dim Wd As Word.Application
Dim Doc As Word.Document

Set Wd = CreateObject("Word.Application")
Set Doc = Wd.Documents.Open("C:\Documents and Settings\My Name\Application
Data\Microsoft\Templates\Outlook Letters\Standard Envelope.dot")
End Sub

I have the template name/folder correct. I added a reference in Outlook
Code to Word Library 11.0 But when I run this from a command button, it
doesn't do anything. No error, it just doesn't open Word. Am I missing
something simple?
thanks,
Southern at Heart
 
Are you positive it isn't working? If you aren't getting any errors when
the code runs open the Task Manager (Ctrl+Alt+Delete) and see if the Word
application is running as a process.

If you want to see the Word.Document and Word is actually running you have
to make the Word.Application visible.
 
Okay, I added Wd.visible, and it is showing up now. However, it opens this
template as read only, but I really need to create a NEW document from this
template, rather than OPEN this template. Can I do that?
Here's what I have now:

Sub Standard_Letter()
Dim Wd As Word.Application
Dim Doc As Word.Document

Set Wd = CreateObject("Word.Application")
Set Doc = Wd.Documents.Open("C:\Documents and Settings\My Name\Application
Data\Microsoft\Templates\Standard Envelope.dot")
Wd.Visible = True
End Sub
 
For that use the Documents.Add method. See the Object Browser help for
information on that method.
 
Okay, I got it.
I'm noticing though that in the task pane, there are a lot of winword.exe
showing up in the processes, but not the applications. I've closed Word and
it doesn't show it as running on the bottom of my screen, but there were like
8 instances of winword.exe in the processes, each taking up a bunch of
memory. Does my code have a memory leak or something? Here's the final code
I use in Outlook, via a button on the standard menu that shows up when I open
a contact to view it's details. (my code copies their address, then it opens
this word template and, using AutoNew, some code in that template pastes
their address into a new envelope...


Sub Standard_Letter()
Dim Wd As Word.Application
Dim Doc As Word.Document

'Copy the Name/Address
Contact_Copy

'create a new word doc with the template
Set Wd = CreateObject("Word.Application")
Set Doc = Wd.Documents.Add("C:\Documents and Settings\My Name\Application
Data\Microsoft\Templates\Outlook Letters\Standard Envelope.dot")
Wd.Visible = True

End Sub


many thanks!
 
You can certainly set your Wd and Doc objects to Nothing at the end of your
sub, but that's not likely the problem. You are using CreateObject() on
Word.Application, so each call will create a new instance of the Word
application process.

You might want to look into using GetObject() instead. Use that and if it
returns Wd as Nothing then call CreateObject() to create the instance
instead of piggy-backing on an existing Word instance.
 
Back
Top