H
horndog
I created a VB front end for mail merges. The mail merge issue I a
having problems with is that I have two combo boxes, one pulls th
month (long format, i.e January) from the database and the Year i
4-digit format.
If a user selects a month and year that doesn't not exist, then i
states the recordset is empty, but run-time error (in run-time). Whe
the application is compiled, then it says the string is longer than 25
characters.
I have tried to suppress the error, but I think since VB just fires of
the data and opens up word, word takes over and controls the error, i
this correct?
Here is the code
PHP code
-------------------
Private Sub cmdReCallDate_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
'Start a new document in Word
Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Add
With oDoc.MailMerge
'Insert the mail merge fields temporarily so that
'you can use the range containing the merge fields as a layout
'for your labels -- to use this as a layout, you can add it
'as an AutoText entry.
With .Fields
.Add oApp.Selection.Range, "PatientFirstName"
oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "PatientLastName"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "PatientStreet"
oApp.Selection.TypeParagraph
' oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "PatientCity"
oApp.Selection.TypeText ", "
.Add oApp.Selection.Range, "PatientState"
oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "PatientPostalCode"
' oApp.Selection.TypeText " -- "
' .Add oApp.Selection.Range, "Country"
End With
Dim oAutoText As Word.AutoTextEntry
Set oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)
oDoc.Content.Delete 'Merge fields in document no longer needed now
'that the AutoText entry for the label layout
'has been added so delete it.
'Set up the mail merge type as mailing labels and use
.MainDocumentType = wdMailingLabels
.OpenDataSource Name:="", Connection:="DSN=Brent;DATABASE=NAME; uid=; pwd=;", SQLStatement:="SELECT PatientFirstName, PatientLastName, PatientStreet, PatientCity, PatientState, PatientPostalCode FROM Patients, exams WHERE exams.PatientID = Patients.PatientID AND exams.ReturnMonth = '" & cmbMonth.Text & "' And exams.ReturnYear = " & cmbYear.Text, SubType:=wdMergeSubTypeWord2000
'Specify your data source here
'Create the new document for the labels using the AutoText entry
'you added -- 5160 is the label number to use for this sample.
'You can specify the label number you want to use for the output
'in the Name argument.
oApp.MailingLabel.CreateNewDocument Name:="5160", Address:="", _
AutoText:="MyLabelLayout", LaserTray:=wdPrinterManualFeed
'Execute the mail merge to generate the labels.
.Destination = wdSendToNewDocument
.Execute
'Delete the AutoText entry you added
oAutoText.Delete
End With
'Close the original document and make Word visible so that
'the mail merge results are displayed
oDoc.Close False
oApp.Visible = True
'Prevent save to Normal template when user exits Word
oApp.NormalTemplate.Saved = True
End Sub
having problems with is that I have two combo boxes, one pulls th
month (long format, i.e January) from the database and the Year i
4-digit format.
If a user selects a month and year that doesn't not exist, then i
states the recordset is empty, but run-time error (in run-time). Whe
the application is compiled, then it says the string is longer than 25
characters.
I have tried to suppress the error, but I think since VB just fires of
the data and opens up word, word takes over and controls the error, i
this correct?
Here is the code
PHP code
-------------------
Private Sub cmdReCallDate_Click()
Dim oApp As Word.Application
Dim oDoc As Word.Document
'Start a new document in Word
Set oApp = CreateObject("Word.Application")
Set oDoc = oApp.Documents.Add
With oDoc.MailMerge
'Insert the mail merge fields temporarily so that
'you can use the range containing the merge fields as a layout
'for your labels -- to use this as a layout, you can add it
'as an AutoText entry.
With .Fields
.Add oApp.Selection.Range, "PatientFirstName"
oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "PatientLastName"
oApp.Selection.TypeParagraph
.Add oApp.Selection.Range, "PatientStreet"
oApp.Selection.TypeParagraph
' oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "PatientCity"
oApp.Selection.TypeText ", "
.Add oApp.Selection.Range, "PatientState"
oApp.Selection.TypeText " "
.Add oApp.Selection.Range, "PatientPostalCode"
' oApp.Selection.TypeText " -- "
' .Add oApp.Selection.Range, "Country"
End With
Dim oAutoText As Word.AutoTextEntry
Set oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)
oDoc.Content.Delete 'Merge fields in document no longer needed now
'that the AutoText entry for the label layout
'has been added so delete it.
'Set up the mail merge type as mailing labels and use
.MainDocumentType = wdMailingLabels
.OpenDataSource Name:="", Connection:="DSN=Brent;DATABASE=NAME; uid=; pwd=;", SQLStatement:="SELECT PatientFirstName, PatientLastName, PatientStreet, PatientCity, PatientState, PatientPostalCode FROM Patients, exams WHERE exams.PatientID = Patients.PatientID AND exams.ReturnMonth = '" & cmbMonth.Text & "' And exams.ReturnYear = " & cmbYear.Text, SubType:=wdMergeSubTypeWord2000
'Specify your data source here
'Create the new document for the labels using the AutoText entry
'you added -- 5160 is the label number to use for this sample.
'You can specify the label number you want to use for the output
'in the Name argument.
oApp.MailingLabel.CreateNewDocument Name:="5160", Address:="", _
AutoText:="MyLabelLayout", LaserTray:=wdPrinterManualFeed
'Execute the mail merge to generate the labels.
.Destination = wdSendToNewDocument
.Execute
'Delete the AutoText entry you added
oAutoText.Delete
End With
'Close the original document and make Word visible so that
'the mail merge results are displayed
oDoc.Close False
oApp.Visible = True
'Prevent save to Normal template when user exits Word
oApp.NormalTemplate.Saved = True
End Sub