Hi,
I am very reluctant to post my code or Word document here as the database is currently running on international level.
I will post extracts from my code:
Code:
Public Function CreateWordLetterETS(strdocpath As String)
On Error GoTo errhand1
If IsNull(strdocpath) Or strdocpath = "" Then
Exit Function
End If
Dim Dbs As Database
Dim objWord As Object
Dim objWordDoc As Word.Document
Dim PrintResponse
Dim TEMP
Dim QtyBrokenDays As Double
Set Dbs = CurrentDb
Set Dbs1 = CurrentDb
Set Dbs3 = CurrentDb
Set Dbs2 = CurrentDb
Set Dbs4 = CurrentDb
'create reference to Word Object
QtyBrokenDays = 0
Set objWord = CreateObject("Word.Application")
Set objWordDoc = objWord.Documents.Open(strdocpath)
Code:
With objWord .Visible = True
.Documents.Open (strdocpath)
.ActiveDocument.Paragraphs(14).Range.Select
.ActiveDocument.Paragraphs(14).Range.Text = "Competency declaration for"
.Selection.Paragraphs.Alignment = wdAlignParagraphCenter: .ActiveDocument.Paragraphs.Indent
.ActiveDocument.Paragraphs(14).Range.Font.Size = 20
'-----------------------------------------------------------------------------------------
.ActiveDocument.Paragraphs(15).Range.Select
.ActiveDocument.Paragraphs(15).Range.Text = Rst1!FirstName & " " & Rst1!Surname
.Selection.Paragraphs.Alignment = wdAlignParagraphCenter
'.ActiveDocument.Paragraphs.Indent
.ActiveDocument.Paragraphs(15).Range.Font.Size = 20
.ActiveDocument.Paragraphs(15).Range.Font.Color = RGB(0, 0, 0) 'BLACK
'---------------------------------------------------------------------------------
.ActiveDocument.Paragraphs(17).Range.Select
.ActiveDocument.Paragraphs(17).Range.Text = "ID Number"
.ActiveDocument.Paragraphs(17).Range.Font.Color = RGB(192, 0, 0) 'RED
.Selection.Paragraphs.Alignment = wdAlignParagraphCenter
.ActiveDocument.Paragraphs(17).Range.Font.Size = 20
.ActiveDocument.Paragraphs(18).Range.Select
.ActiveDocument.Paragraphs(18).Range.Font.Color = RGB(0, 0, 0) 'BLACK
.ActiveDocument.Paragraphs(18).Range.Font.Size = 20
.Selection.Paragraphs.Alignment = wdAlignParagraphCenter
Code:
.ActiveDocument.Paragraphs(31).Range.Select: .Selection.Paragraphs.Alignment = wdAlignParagraphLeft .ActiveDocument.Paragraphs(31).Range.Font.Size = 16
If Format(Rst!CertificateExpDate, "\#dd-mm-yyyy\#") <> Format("31-12-2999", "\#dd-mm-yyyy\#") Then
.ActiveDocument.Paragraphs(31).Range.Text = Rst!AssDate & Space(77) & Rst!CertificateExpDate
Else
.ActiveDocument.Paragraphs(31).Range.Text = Space(2) & Rst!AssDate & Space(81) & "N/A"
End If
.ActiveDocument.Paragraphs(31).Range.Font.Color = RGB(0, 0, 0) 'BLACK
.ActiveDocument.Paragraphs(32).Range.Select
.ActiveDocument.Paragraphs(32).Range.Font.Size = 10
.ActiveDocument.Paragraphs(32).Range.Text = "______________________" & Space(107) & "___________________"
.ActiveDocument.Paragraphs(33).Range.Select: .ActiveDocument.Paragraphs(33).Range.Font.Size = 10
.ActiveDocument.Paragraphs(33).Range.Font.Size = 16
.ActiveDocument.Paragraphs(33).Range.Text = "Assessment Date" & Space(68) & "Expiry Date"
'----------------------------------------------------------------------------------------------------
.ActiveDocument.Paragraphs.Add: .ActiveDocument.Paragraphs.Add
.ActiveDocument.Paragraphs(36).Range.Select
.ActiveDocument.Paragraphs(36).Range.Font.Size = 10
.ActiveDocument.Paragraphs(36).Range.Text = Space(140) & "____________________________"
.ActiveDocument.Paragraphs.Add
.ActiveDocument.Paragraphs(37).Range.Select
.ActiveDocument.Paragraphs(37).Range.Font.Size = 16
.ActiveDocument.Paragraphs(37).Range.Text = Space(88) & "Certification Officer"
.ActiveDocument.Paragraphs(37).Range.Font.Color = RGB(192, 0, 0) 'RED
'Now save or Update the certificate number
Rst4.Close
If Forms!Frm_PrintCertificates.Check9.Value = False Then
SQL4 = "INSERT INTO Tbl_CertificateNumbers " _
& "(CertificateNumberPrefix, CertificateMonthYear, CertificateStudent, CertificateDate, Certificate_ETS_IFSAC, CertificateCertNumber, CertificateStartDate, CertificateEndDate, " _
& "CertificatePrinted, CertificateRePrintNo, CertificateRePrintReason, Certificate_IFSAC_SealNumber) " _
& "VALUES " _
& "(" & NewCertificateNumber & ", " & Format(Date, "mm") + Format(Date, "yyyy") & ", " & Val(Forms!Frm_PrintCertificates.TempID) & ", " & Format(Date, "\#mm-dd-yyyy\#") & ", 'ETS', " _
& " " & Val(Forms!Frm_PrintCertificates.CertNo) & ", " & Format(Forms!Frm_PrintCertificates.From, "\#mm-dd-yyyy\#") & ", " & Format(Forms!Frm_PrintCertificates.ToDate, "\#mm-dd-yyyy\#") & ", " _
& "True, 0, 'N/A', 'N/A');"
DoCmd.RunSQL SQL4
ElseIf Forms!Frm_PrintCertificates.Check9.Value = True Then
Code:
'Save the certificate to C:\Temp so that the template does not get overwritten If Not objWord Is Nothing Then
Dim DocName
DocName = "C:\Temp\" & UserComputer & "\ID_" & Forms!Frm_PrintCertificates.TempID & "_" & "Course_" & Forms!Frm_PrintCertificates.TmpCourseNo & ".pdf"
objWordDoc.ExportAsFixedFormat "C:\Temp\" & UserComputer & "\ID_" & Forms!Frm_PrintCertificates.TempID & "_" & "Course_" & Forms!Frm_PrintCertificates.TmpCourseNo & " ETS.pdf", wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument
MsgBox "The ETS Certificate has been saved as follows: " & vbCrLf _
& DocName & ".", vbOKOnly, "Saved as PDF"
DocName = "C:\Temp\" & UserComputer & "\ID_" & Forms!Frm_PrintCertificates.TempID & "_" & "Course_" & Forms!Frm_PrintCertificates.TmpCourseNo & " ETS.pdf"
End If
'release all objects
objWord.ActiveDocument.Close wdDoNotSaveChanges
objWord.Application.Quit wdDoNotSaveChanges
Set objWord = Nothing
Set Dbs = Nothing
Rst.Close
Rst1.Close
Rst2.Close
The code runs 100%, but somehow Word generates these messages out of the blu. When this happens, I need the message to be visible to the users.
Thanks