Hi all
Iam stuck on how to reference a bookmark in word from access, where the book mark is on a text box within the word document.
The code below is what i have to date, but it errors with a message Type MisMatch. Any ideas on how I can resolve this?
I have included the code below, and higlighted the problem area.
Iam stuck on how to reference a bookmark in word from access, where the book mark is on a text box within the word document.
The code below is what i have to date, but it errors with a message Type MisMatch. Any ideas on how I can resolve this?
I have included the code below, and higlighted the problem area.
Code:
Private Sub cmdMerge_Click()
'Stop
Dim WordApp As Object 'Word.Application
Dim strTemplateLocation As String, strFormat As String, strValue As String
Dim RS As DAO.Recordset
Dim strSQL As String, strFileLoc As String, strFolder As String, strFileName As String
Dim boolWord As Boolean
Dim stBox As Shape
Dim wdWord As Object
strFileLoc = "C:\Users\Jan\Documents\House of Colour\Booking Forms\"
'C:\Users\Jan\Documents\House of Colour\Booking Forms
strFormat = vbLongDate
Select Case Me.fraBkForm
Case 1:
strFolder = "Colour Class\"
strFileName = "Colour Analysis Template Marbella2.dot"
'strDocName = "rptWomanColourDoc"
'strFile = strFile & " Colour Class"
Case 2:
'strFileName = strFileName & "Make Up\"
'strDocName = "rptMakeUpDoc"
'strFile = strFile & " Make Up"
Case 3:
'strFileName = strFileName & "Image\"
'strDocName = "rptWomensImage"
'strFile = strFile & " Image"
Case 4:
'strFileName = strFileName & "Mens Colour Class\"
'strDocName = "rptMensColourDoc"
'strFile = strFile & " Mens Colour Class"
End Select
strSQL = "SELECT t1.Title, t1.FirstName, t1.LastName, t1.AddressLine2, t1.Addressline3, t1.Addressline3, "
strSQL = strSQL & "t1.Addressline4, t1.City, t1.County, t1.Postcode, t1.Country, t1.HomePhone, "
strSQL = strSQL & "t1.Mobile, t1.EmailAddress, t3.CourseDate, t3.Start, t3.Finish, t3.EURPrice "
strSQL = strSQL & "FROM tblCustomers t1 INNER JOIN (tblInvoices t2 INNER JOIN tblInvCourses t3 ON t2.InvoiceId = t3.InvoiceId) ON t1.CustomerID = t2.CustomerId"
strSQL = strSQL & " WHERE t1.CustomerID = " & Me.CustomerID
strSQL = strSQL & " AND t3.CourseId = 1"
Set RS = CurrentDb.OpenRecordset(strSQL)
RS.MoveLast
RS.MoveFirst
strValue = RS!EURPrice
' Specify location of template
strTemplateLocation = strFileLoc & strFolder & strFileName
If fIsAppRunning("Word") Then
Set WordApp = CreateObject("Word.Application")
boolWord = False
Else
Set WordApp = GetObject(, "Word.Application")
boolWord = True
End If
On Error GoTo ErrHandler
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
' Replace each bookmark with field contents.
With WordApp.Selection
.Goto what:=wdGoToBookmark, Name:="classdate"
.TypeText Trim(RS!CourseDate)
.Goto what:=wdGoToBookmark, Name:="recipient"
.TypeText " " & RS!Title & " " & RS!FirstName & " " & RS!LastName
.Goto what:=wdGoToBookmark, Name:="Invest"
.TypeText strValue
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
'THE PROBLEM IS HERE
[b]
[COLOR=red]
'Make the text box the active section
Set stBox = WordApp.ActiveDocument.Shapes(1)
With stBox
.Select
Selection.Goto what:=wdGoToBookmark, Name:="StartTime"
Selection.TypeText RS!Start
Selection.Goto what:=wdGoToBookmark, Name:="FinishTime"
Selection.TypeText RS!Finish
Selection.Goto what:=wdGoToBookmark, Name:="closingdate"
Selection.TypeText Trim(RS!CourseDate)
End With
[/color]
[/b]
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
End With
DoEvents
WordApp.Activate
Set WordApp = Nothing
Exit Sub
ErrHandler:
Set WordApp = Nothing
MsgBox Err.Description
End Sub