novtalath
Registered User.
- Local time
- Today, 21:11
- Joined
- Dec 30, 2008
- Messages
- 19
Hi,
I got that problem and I can't work my head around it. I have my MS Access database (MS Office 2k) linked with word documents (work as templates to be filled up with data from database). And before you say do it in report - I will say NO. Word has better word processing abilities (after all it is word processor) and I need these. So I have those bookmarks on word document which are being replaced with data from database (like name and surname, job title, reference No etc), and this works fine. But if I run a check to see if such biookmark exists then it always comes back as 'false'. Funny enough next line replaces such bookmark woth some data without any problems finding it. Here's how the code look like:
And this line:
ALWAYS returns 'False'. I'm really struggling now as I can't see any flaws in the code. Bookmark DOES exist as I was testing it with some freshly created and named bookmarks.
I got that problem and I can't work my head around it. I have my MS Access database (MS Office 2k) linked with word documents (work as templates to be filled up with data from database). And before you say do it in report - I will say NO. Word has better word processing abilities (after all it is word processor) and I need these. So I have those bookmarks on word document which are being replaced with data from database (like name and surname, job title, reference No etc), and this works fine. But if I run a check to see if such biookmark exists then it always comes back as 'false'. Funny enough next line replaces such bookmark woth some data without any problems finding it. Here's how the code look like:
Code:
Dim WordApp As Word.Application
Dim strTemplateLocation, varUsername, varJobTitle, varReference, varUser, varBookmark As String
Dim FSys As Object
On Error GoTo PreviewError
If IsNull(Me.comboReRouteTemplates.Value) = True Then
MsgBox "No letter selected", vbExclamation
GoTo PreviewEnd
Else
strTemplateLocation = DLookup("[LetterText]", "tblReRouteTemplates", "[RecordNo] = " & Me.comboReRouteTemplates.Column(0))
End If
GetSignature
' ** Initiate Word **
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordApp = CreateObject("Word.Application")
End If
On Error GoTo PreviewError
Set FSys = CreateObject("Scripting.FileSystemObject")
WordApp.Visible = True
WordApp.WindowState = wdWindowStateMaximize
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False
' Replace each bookmark with field contents.
If IsNull(varUsername = DLookup("[Name_Surname]", "tblLUsers", "[CollarNo] = '" & Environ("username") & "'")) = True Then
MsgBox "You are not in database of users. Please contact administrator.", vbExclamation
GoTo PreviewEnd
Else
varUsername = DLookup("[Name_Surname]", "tblLUsers", "[CollarNo] = '" & Environ("username") & "'")
varJobTitle = DLookup("[Job_Title]", "tblLUsers", "[CollarNo] = '" & Environ("username") & "'")
varReference = DLookup("[ReferenceNo]", "tblReRouteLetters", "[RecordNo] = " & Me.lstReRoutes.Column(6))
End If
With WordApp.Selection
If .Bookmarks.Exists("VAR_ReferenceNo") = True Then
If IsNull(varReference) = True Then
.Goto what:=wdGoToBookmark, Name:="VAR_ReferenceNo"
.TypeText (varReference)
Else
.Goto what:=wdGoToBookmark, Name:="VAR_ReferenceNo"
.TypeText ("None given")
End If
Else
MsgBox "No bookmark?!"
End If
.Goto what:=wdGoToBookmark, Name:="VAR_Username"
.TypeText (varUsername)
.Goto what:=wdGoToBookmark, Name:="VAR_JobTitle"
.TypeText (varJobTitle)
End With
DoEvents
WordApp.Activate
'WordApp.PrintOut
'WordApp.Quit False
Set WordApp = Nothing
PreviewEnd:
Exit Sub
PreviewError:
MsgBox Err.Description
Resume PreviewEnd
Code:
If .Bookmarks.Exists("VAR_ReferenceNo") = True Then