Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Word Template - wanting to run a Macro on opening

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David - 06 Jan 2005 23:36 GMT
Hi

I have a Word template that I've created which has a macro attached to
insert bookmarks across a letter template we have in our database
system.  The database system uses the object libraries in Word to
create the Word Document and can use a template to start its design
with.

This works fine and the Macro will work across the letter template we
extract from our database.  What I am wanting to do is make the code
run automatically as soon as the template has been opened.

Here is the code I have so far.

Sub Bookmarks()

   'Enter the number of lines in the letterhead.  I.E six lines is 11
   LineCount = 11
   
   ' Pat Name Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Patient Name:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="patname"
   
   ' Your Ref Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Your Ref:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="yourref"
   
   ' Address Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Address:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="address"
   
       
   ' Our Ref Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Our Ref:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="ourref"
       
   'DOB Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "DOB:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="dob"
     
   ' Reqdate Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Request Date:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="reqdate"
     
   
   ' Sex Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Sex:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="sex"
   
   
   ' CollectDate Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Collection Date:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="collectdate"
   
   
   ' Medicare Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Medicare No:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="medicare"
   
   ' RecDoc Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Receiving Doctor:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="recdoc"
   
   ' RecDocRef Bookmark
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="RecDocRef"
   
       
   ' CopyToDoc Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Copy To Doctor:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="copytodoc"
   
   ' CopyToDocRef Bookmark
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="copytodocref"
   
   ' Examination Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "Examination:"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="examination"
   
   ' Findings Bookmark
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst,
Count:=LineCount
   With Selection.Find
       .Text = "$$"
       .Wrap = wdFindContinue
   End With
   Selection.Find.Execute
   Selection.Delete Unit:=wdCharacter, Count:=1
   Selection.MoveRight Unit:=wdCell
   ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:="Findings"
   
   ' Go Back to Top of Document
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst

   
End Sub

With another part of code.

Public WithEvents appWord As Word.Application

Private Sub appWord_NewDocument(ByVal Doc As Document)
   Call Bookmarks
End Sub

In the Documents

How can I make this automate once the merge from our database to the
word template is complete?
symbolism@gmail.com - 06 Jan 2005 23:52 GMT
Just to clarify further - our medical database inserts the text into
the Word Template either by Merge or Object Insert, I'm not sure which
one. I have tried pretty much every On Event to get this running as
soon as the template has been opened and the text is inserted to
automate it but it doesn't seem to work.
Chuck - 07 Jan 2005 16:23 GMT
Hi David

You could use your code in an AutoNew macro in a module in the template.

However, just curious, why are you using code to set the bookmarks each time
a letter is created based on the template?  Why not just put the bookmarks in
the template?

Chuck

> Hi
>
[quoted text clipped - 203 lines]
> How can I make this automate once the merge from our database to the
> word template is complete?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.