Word Template Automation

F

Frank Brady

The following code opens a word template as a new document
and appends text to the new document. This works in
Windows 98, however when ported to Windows XP I get ERROR
5152 - this is not a valid file name. The file is there
and it is not corrrupt.

The error occurs in the following line:

Set doc1 = wrd.Documents.Add
(Template:="c:\Access\Data\RefLetterTemp",
NewTemplate:=False)



Private Sub cmdReferralLetter_Click()
Dim db As Database
Dim rs As Recordset
Dim strSql As String
Dim strMD

On Error GoTo err_handler

strSql = "SELECT * FROM tblPhysican where
tblPhysican.ID = """ & strMD & """;"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSql)

Dim wrd As Word.Application
Dim doc1 As Word.Document
Dim rng As Word.Range
Dim SerNo As String
SerNo = CStr(CDec(Time()))
SerNo = Mid(SerNo, InStr(SerNo, ".") + 1)

Set wrd = New Word.Application

'set pointer to new doc based on Referral templet
Set doc1 = wrd.Documents.Add
(Template:="c:\Access\Data\RefLetterTemp",
NewTemplate:=False)

With wrd.ActiveDocument
Set rng = .Bookmarks("DrName").Range
rng.InsertBefore Nz(rs!FNAME & " " & rs!LNAME & "
M.D.")
Set rng = .Bookmarks("DrStreet").Range
rng.InsertBefore Nz(rs!ADDR1)

If rs!ADDR2 <> "" Then
Set rng = .Bookmarks("DrAddLn2").Range
rng.InsertBefore vbCrLf & Nz(rs!ADDR2)
End If

Set rng = .Bookmarks("DrCSZ").Range
rng.InsertBefore Nz(rs!City) & ", " & Nz(rs!State)
& " " & Nz(rs!Zip) & vbCrLf & vbCrLf
Set rng = .Bookmarks("DrLname").Range
rng.InsertBefore Nz(rs!LNAME)
Set rng = .Bookmarks("Re").Range
rng.InsertBefore Forms![frmpatients]![LNAME] & ", "
& Forms![frmpatients]![FNAME]
Set rng = .Bookmarks("PtFname").Range
rng.InsertBefore Forms![frmpatients]![FNAME]
Set rng = .Bookmarks("HeShe").Range
rng.InsertBefore IIf(Forms![frmpatients]![SEX]
= "M", "He", "She")
Set rng = .Bookmarks("DOS").Range
rng.InsertBefore Format(Me![DateSeen], "dddd, mmm d
yyyy")
' Returns "Wednesday, Jan 27 1993".
Set rng = .Bookmarks("Note").Range
rng.InsertBefore Nz(Me![Note])

.SaveAs "C:\My Documents\" & rs!LNAME & SerNo
End With

wrd.Visible = True


exit_Hear:
Set rng = Nothing
Set doc1 = Nothing
Set wrd = Nothing
Exit Sub

err_handler:
Select Case Err.Number
Case Else
MsgBox Err.Number & " " & Err.Description,
vbExclamation, "Error Referal Letter to MS Word"
End Select
Resume exit_Hear

End Sub
 
E

Eric Lawrence [MSFT]

Is there some reason your template filename doesn't have a file extension?

--
Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

It makes no differance .dot is default. I am running the
code form an access project. The code works fine if run
form a module in word.
-----Original Message-----
Is there some reason your template filename doesn't have a file extension?

--
Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.

The following code opens a word template as a new document
and appends text to the new document. This works in
Windows 98, however when ported to Windows XP I get ERROR
5152 - this is not a valid file name. The file is there
and it is not corrrupt.

The error occurs in the following line:

Set doc1 = wrd.Documents.Add
(Template:="c:\Access\Data\RefLetterTemp",
NewTemplate:=False)



Private Sub cmdReferralLetter_Click()
Dim db As Database
Dim rs As Recordset
Dim strSql As String
Dim strMD

On Error GoTo err_handler

strSql = "SELECT * FROM tblPhysican where
tblPhysican.ID = """ & strMD & """;"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSql)

Dim wrd As Word.Application
Dim doc1 As Word.Document
Dim rng As Word.Range
Dim SerNo As String
SerNo = CStr(CDec(Time()))
SerNo = Mid(SerNo, InStr(SerNo, ".") + 1)

Set wrd = New Word.Application

'set pointer to new doc based on Referral templet
Set doc1 = wrd.Documents.Add
(Template:="c:\Access\Data\RefLetterTemp",
NewTemplate:=False)

With wrd.ActiveDocument
Set rng = .Bookmarks("DrName").Range
rng.InsertBefore Nz(rs!FNAME & " " & rs!LNAME & "
M.D.")
Set rng = .Bookmarks("DrStreet").Range
rng.InsertBefore Nz(rs!ADDR1)

If rs!ADDR2 <> "" Then
Set rng = .Bookmarks("DrAddLn2").Range
rng.InsertBefore vbCrLf & Nz(rs!ADDR2)
End If

Set rng = .Bookmarks("DrCSZ").Range
rng.InsertBefore Nz(rs!City) & ", " & Nz(rs!State)
& " " & Nz(rs!Zip) & vbCrLf & vbCrLf
Set rng = .Bookmarks("DrLname").Range
rng.InsertBefore Nz(rs!LNAME)
Set rng = .Bookmarks("Re").Range
rng.InsertBefore Forms![frmpatients]![LNAME] & ", "
& Forms![frmpatients]![FNAME]
Set rng = .Bookmarks("PtFname").Range
rng.InsertBefore Forms![frmpatients]![FNAME]
Set rng = .Bookmarks("HeShe").Range
rng.InsertBefore IIf(Forms![frmpatients]![SEX]
= "M", "He", "She")
Set rng = .Bookmarks("DOS").Range
rng.InsertBefore Format(Me![DateSeen], "dddd, mmm d
yyyy")
' Returns "Wednesday, Jan 27 1993".
Set rng = .Bookmarks("Note").Range
rng.InsertBefore Nz(Me![Note])

.SaveAs "C:\My Documents\" & rs!LNAME & SerNo
End With

wrd.Visible = True


exit_Hear:
Set rng = Nothing
Set doc1 = Nothing
Set wrd = Nothing
Exit Sub

err_handler:
Select Case Err.Number
Case Else
MsgBox Err.Number & " " & Err.Description,
vbExclamation, "Error Referal Letter to MS Word"
End Select
Resume exit_Hear

End Sub


.
 
E

Eric Lawrence [MSFT]

On which line does your code fail?

It makes no differance .dot is default. I am running the
code form an access project. The code works fine if run
form a module in word.
-----Original Message-----
Is there some reason your template filename doesn't have a file extension?

--
Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.

The following code opens a word template as a new document
and appends text to the new document. This works in
Windows 98, however when ported to Windows XP I get ERROR
5152 - this is not a valid file name. The file is there
and it is not corrrupt.

The error occurs in the following line:

Set doc1 = wrd.Documents.Add
(Template:="c:\Access\Data\RefLetterTemp",
NewTemplate:=False)



Private Sub cmdReferralLetter_Click()
Dim db As Database
Dim rs As Recordset
Dim strSql As String
Dim strMD

On Error GoTo err_handler

strSql = "SELECT * FROM tblPhysican where
tblPhysican.ID = """ & strMD & """;"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSql)

Dim wrd As Word.Application
Dim doc1 As Word.Document
Dim rng As Word.Range
Dim SerNo As String
SerNo = CStr(CDec(Time()))
SerNo = Mid(SerNo, InStr(SerNo, ".") + 1)

Set wrd = New Word.Application

'set pointer to new doc based on Referral templet
Set doc1 = wrd.Documents.Add
(Template:="c:\Access\Data\RefLetterTemp",
NewTemplate:=False)

With wrd.ActiveDocument
Set rng = .Bookmarks("DrName").Range
rng.InsertBefore Nz(rs!FNAME & " " & rs!LNAME & "
M.D.")
Set rng = .Bookmarks("DrStreet").Range
rng.InsertBefore Nz(rs!ADDR1)

If rs!ADDR2 <> "" Then
Set rng = .Bookmarks("DrAddLn2").Range
rng.InsertBefore vbCrLf & Nz(rs!ADDR2)
End If

Set rng = .Bookmarks("DrCSZ").Range
rng.InsertBefore Nz(rs!City) & ", " & Nz(rs!State)
& " " & Nz(rs!Zip) & vbCrLf & vbCrLf
Set rng = .Bookmarks("DrLname").Range
rng.InsertBefore Nz(rs!LNAME)
Set rng = .Bookmarks("Re").Range
rng.InsertBefore Forms![frmpatients]![LNAME] & ", "
& Forms![frmpatients]![FNAME]
Set rng = .Bookmarks("PtFname").Range
rng.InsertBefore Forms![frmpatients]![FNAME]
Set rng = .Bookmarks("HeShe").Range
rng.InsertBefore IIf(Forms![frmpatients]![SEX]
= "M", "He", "She")
Set rng = .Bookmarks("DOS").Range
rng.InsertBefore Format(Me![DateSeen], "dddd, mmm d
yyyy")
' Returns "Wednesday, Jan 27 1993".
Set rng = .Bookmarks("Note").Range
rng.InsertBefore Nz(Me![Note])

.SaveAs "C:\My Documents\" & rs!LNAME & SerNo
End With

wrd.Visible = True


exit_Hear:
Set rng = Nothing
Set doc1 = Nothing
Set wrd = Nothing
Exit Sub

err_handler:
Select Case Err.Number
Case Else
MsgBox Err.Number & " " & Err.Description,
vbExclamation, "Error Referal Letter to MS Word"
End Select
Resume exit_Hear

End Sub


.
 

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