Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I'm a freelance consultant, and your site's helped me with many issues. I just wanted you to know that someone does appreciate the intelligent help your site offers."

Geography

Where in the world do Tek-Tips members come from?

Mail merge to location on server - code will not work

SteveJH (IS/IT--Management)
13 Apr 12 9:31
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
 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close