SteveJH
IS-IT--Management
- Apr 13, 2012
- 1
I would be very grateful for any help with this problem.
I have been struggling to get the following code to run when I place the program on a server(just keeps generating a Run-time error 5151 message, saying that Word was unable to read this document. It may be corrupt. Try one of the following ...)
I am mailmerging from Access. The code works perfectly on laptop etc, however, it will NOT run when the access program is located on the server along with the Word templates (i.e. in the same folder etc).
My code is as follows:
Private Sub GenerateDoctorLetter_Click()
Me.Label4thStep.BackColor = RGB(0, 204, 0)
If IsNull(Me!LetterType) Then
MsgBox "You must enter Step 1 - the 'Letter Type' field before proceeding. Press OK then select the letter type from the list and then press Return or Tab."
Cancel = True
Else
Dim AddyLineVar As String
AddyLineVar = AddyLineVar + vbCrLf + [Dr Title] + " " + [Dr Initial] + " " + [DrSurname]
AddyLineVar = AddyLineVar + vbCrLf + [Practice]
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![Address1sf]
'If Address 2 isn't null, add line break and Address 2
If Not IsNull(Me!MailMergeDoctorLetterPracticeSubForm.Form![Address2sf]) Then
AddyLineVar = AddyLineVar + vbCrLf + (Me!MailMergeDoctorLetterPracticeSubForm.Form![Address2sf])
End If
'If Town 1 isn't null, add line break and Town1
If Not IsNull(Me!MailMergeDoctorLetterPracticeSubForm.Form![Town1sf]) Then
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![Town1sf]
End If
'If Town City isn't null, add line break and Town City
If Not IsNull(Me!MailMergeDoctorLetterPracticeSubForm.Form![TownCitysf]) Then
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![TownCitysf]
End If
'Tack on line break then County, Post Code.
If Not IsNull(Me!MailMergeDoctorLetterPracticeSubForm.Form![Countysf]) Then
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![Countysf]
End If
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![PostCodesf]
DoctorLine2 = [Dr Title] + " " + [DrSurname]
PatientLine = [FirstName] + " " + [LastName] + ", " + [PatientAddress1] + ", " + [PatientTownCity] + ", " + [PatientPostCode]
If Not IsNull([Text123]) Then
DOB = [Text123]
End If
If Not IsNull(Me.[ScreenDate]) Then
ScreenDateVar = Me.ScreenDate
ScreenDateVar = Format(ScreenDate, "dd mmmm yyyy")
End If
If Not IsNull(Me.[LetterFrom]) Then
InsertNameWhoLetterFrom = Me.LetterFrom
End If
If Not IsNull(Me.[ReturnByDateEntry]) Then
ReturnByDateVar = Me.ReturnByDateEntry
ReturnByDateVar = Format(ReturnByDateEntry, "dd mmmm yyyy")
End If
'Declare an instance of Microsoft Word.
Dim Wrd As New Word.Application
Set Wrd = CreateObject("Word.Application")
'Specify the path and name to the Word document.
Dim MergeDoc As String
MergeDoc = Application.CurrentProject.Path + "\"
MergeDoc = MergeDoc + Me.LetterType
'Open the document template, make it visible.
Wrd.Documents.Add ([MergeDoc])
Wrd.Visible = True
Wrd.Activate
'Replace each bookmark with current data.
With Wrd.ActiveDocument.Bookmarks
.Item("AddressLines").Range.Text = AddyLineVar
.Item("DoctorLine2").Range.Text = DoctorLine2
.Item("PatientLine").Range.Text = PatientLine
.Item("DOB").Range.Text = DOB
.Item("ScreeningDate").Range.Text = ScreenDateVar
.Item("ReturnByDate").Range.Text = ReturnByDateVar
.Item("InsertNameWhoLetterFrom").Range.Text = InsertNameWhoLetterFrom
End With
DoCmd.Close acForm, "MailMergeDoctor"
End If
End Sub
Thank you in advance
I have been struggling to get the following code to run when I place the program on a server(just keeps generating a Run-time error 5151 message, saying that Word was unable to read this document. It may be corrupt. Try one of the following ...)
I am mailmerging from Access. The code works perfectly on laptop etc, however, it will NOT run when the access program is located on the server along with the Word templates (i.e. in the same folder etc).
My code is as follows:
Private Sub GenerateDoctorLetter_Click()
Me.Label4thStep.BackColor = RGB(0, 204, 0)
If IsNull(Me!LetterType) Then
MsgBox "You must enter Step 1 - the 'Letter Type' field before proceeding. Press OK then select the letter type from the list and then press Return or Tab."
Cancel = True
Else
Dim AddyLineVar As String
AddyLineVar = AddyLineVar + vbCrLf + [Dr Title] + " " + [Dr Initial] + " " + [DrSurname]
AddyLineVar = AddyLineVar + vbCrLf + [Practice]
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![Address1sf]
'If Address 2 isn't null, add line break and Address 2
If Not IsNull(Me!MailMergeDoctorLetterPracticeSubForm.Form![Address2sf]) Then
AddyLineVar = AddyLineVar + vbCrLf + (Me!MailMergeDoctorLetterPracticeSubForm.Form![Address2sf])
End If
'If Town 1 isn't null, add line break and Town1
If Not IsNull(Me!MailMergeDoctorLetterPracticeSubForm.Form![Town1sf]) Then
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![Town1sf]
End If
'If Town City isn't null, add line break and Town City
If Not IsNull(Me!MailMergeDoctorLetterPracticeSubForm.Form![TownCitysf]) Then
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![TownCitysf]
End If
'Tack on line break then County, Post Code.
If Not IsNull(Me!MailMergeDoctorLetterPracticeSubForm.Form![Countysf]) Then
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![Countysf]
End If
AddyLineVar = AddyLineVar + vbCrLf + Me!MailMergeDoctorLetterPracticeSubForm.Form![PostCodesf]
DoctorLine2 = [Dr Title] + " " + [DrSurname]
PatientLine = [FirstName] + " " + [LastName] + ", " + [PatientAddress1] + ", " + [PatientTownCity] + ", " + [PatientPostCode]
If Not IsNull([Text123]) Then
DOB = [Text123]
End If
If Not IsNull(Me.[ScreenDate]) Then
ScreenDateVar = Me.ScreenDate
ScreenDateVar = Format(ScreenDate, "dd mmmm yyyy")
End If
If Not IsNull(Me.[LetterFrom]) Then
InsertNameWhoLetterFrom = Me.LetterFrom
End If
If Not IsNull(Me.[ReturnByDateEntry]) Then
ReturnByDateVar = Me.ReturnByDateEntry
ReturnByDateVar = Format(ReturnByDateEntry, "dd mmmm yyyy")
End If
'Declare an instance of Microsoft Word.
Dim Wrd As New Word.Application
Set Wrd = CreateObject("Word.Application")
'Specify the path and name to the Word document.
Dim MergeDoc As String
MergeDoc = Application.CurrentProject.Path + "\"
MergeDoc = MergeDoc + Me.LetterType
'Open the document template, make it visible.
Wrd.Documents.Add ([MergeDoc])
Wrd.Visible = True
Wrd.Activate
'Replace each bookmark with current data.
With Wrd.ActiveDocument.Bookmarks
.Item("AddressLines").Range.Text = AddyLineVar
.Item("DoctorLine2").Range.Text = DoctorLine2
.Item("PatientLine").Range.Text = PatientLine
.Item("DOB").Range.Text = DOB
.Item("ScreeningDate").Range.Text = ScreenDateVar
.Item("ReturnByDate").Range.Text = ReturnByDateVar
.Item("InsertNameWhoLetterFrom").Range.Text = InsertNameWhoLetterFrom
End With
DoCmd.Close acForm, "MailMergeDoctor"
End If
End Sub
Thank you in advance