Bookmarks.exists always return false

novtalath

Registered User.
Local time
Today, 23:25
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:
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
And this line:
Code:
If .Bookmarks.Exists("VAR_ReferenceNo") = True Then
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 see some issue with your code.

1) avoid using GoTo.

2) Your GoTo skips past all the house keeping code. It really is not the proper way to exit the If statement. They general introduces more issues.

3) You have

Code:
With WordApp.Selection

but it is not preceded by any type if selection.

I would remove the GoTo and move the End if like this:

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
        
    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))

 
    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

    End If


    'WordApp.Quit False
    Set WordApp = Nothing

PreviewEnd:
    Exit Sub
 
PreviewError:
    MsgBox Err.Description
    Resume PreviewEnd
 
Well, I have made advised changes, but it still didn't solve the problem. VB in Access can not find (or finds it but still don't give a damn) a bookmark/s in word and returns 'false'. It's starting to drive me crazy as by my understanding the code is right just it doesn't do what I'm asking it to do. Maybe there is another way to check if such word document has said template?
I need it for filling it up with data if such data exsist (ReferenceNo is NOT required) that's why I want to check if bookmark exist and if there are any data to fill it with.
 
What code did you add to make a selection?

Will you post your updated VBA code?
 
Err... None?
I took that off MS pages and there was nothing about making any selections. Also I have tried .ActiveDocument method returns error "This object doesn't support this method or property". I'm not too good if it is about coding (self learner) so it comes a bit harder to me. Most of my code is based on help files and msoffice pages (and of course this forum :D).
That code I have posted is everything apart from 'private sub' and 'end sub' bits.
 
I took that off MS pages and ...

So you took code from different pages at MS and are trying to combine the code.

Will you post the links to the MS pages where you got the code?
 
Last edited:
This part does not look correct.

Code:
    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

This logic seams backwards:

Code:
            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

Do you really want it to say ("None given" whan varReference is Null?



Try Thisd:

Code:
    With WordApp.ActiveDocument

        If .Bookmarks.Exists("VAR_ReferenceNo") = True Then

            .Goto what:=wdGoToBookmark, Name:="VAR_ReferenceNo"

            If Not IsNull(varReference) Then
                .TypeText (varReference)
            Else
                .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
 
So you took code from diffrent pages at MS and are trying to combine the code?

Will you post the links to the MS pages where you got the code?
Heh... pages and helpfiles.
Im not completly daft. I do combine code but in a way it SHOULD work. On this particular occasion it does not. I may not be VB guru, but I'm not a complete idiot as well. It may be my fault NOT creating a selection but then again. How do you select whole document?
And yes - it has to say "None given" as it will mean that there were no ref. umber provided by other person.

[Edit]
Now it doesn't do anything. Bookmarks are NOT changed at all.
 
Also - If I understand ms support pages if range (selection) is not defined it selects a whole document as default. But... I may be wrong - after all my langauge is polish not english.
 
And yes - it has to say "None given" as it will mean that there were no ref. umber provided by other person.
But your original could would print "None Given" if the was something and blank where the was nothing.

Code:
            If IsNull(varReference) = True Then
               ' if varReference is NULL then show the Null vaule 
                .Goto what:=wdGoToBookmark, Name:="VAR_ReferenceNo"
                .TypeText (varReference)
            Else
             ' IsNull(varReference) = False or has somwething     
                .Goto what:=wdGoToBookmark, Name:="VAR_ReferenceNo"
                .TypeText ("None given")
            End If
 
Last edited:
I guess I'm trying to be too smart for it :o
Good idea. After all, it is printed anyways, and no one will see bookmark :)
 

Users who are viewing this thread

Back
Top Bottom