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