Word Template - wanting to run a Macro on opening

D

David

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

symbolism

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

Chuck

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
 

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