Word Bookmark as a string

  • Hi. I have some buttons on a userform which all relate to bookmarks in a word document. The code I have works fine. Here it is.


    However, there is a potential for there to be hundreds and hundreds of bookmarks in the document. Each of these will require buttons on a multipage form. What I'm trying to do is on clicking the button enter the name of the bookmark ( in the case above "A02") and then send it to the rest of the code as a sub. The string name I'm going with at the moment is bmRef (ie bookmark reference). So the code should look something like this. However this code below would now be a subroutine the button only sending bmRef to the public variable


    The problem I'm having is that I get an error whenever I run this.
    I have declared bmRef as a public string but I can't seem to get it to work. Any ideas?

  • Hi,


    Without doing any testing I would hazard a guess that your problem is that you may need to surround bmRef with double quotes for it to work ?


    Maybe try...


    Code
    Const vbDoubleQuote As String = """"
    
    
    wrdApp.ActiveDocument.Bookmarks(vbDoubleQuote & bmRef & vbDoubleQuote).Select


    Or alternatively...


    Code
    wrdApp.ActiveDocument.Bookmarks(Chr(34) & bmRef & Chr(34)).Select


    Regards,


    Tom Rowe

  • Hi. Thanks for that but I still can't get it to work. Here are the three bits of code I currently have in the Form code


    Code
    Public bmRef As String


    Should this be outside of the form code?



    2nd bit

    Code
    Private Sub CMB_01_DFTR_Click()
    
    
        bmRef = "A02"
        Call openword
    
    
       
    End Sub


    3rd bit

  • Finally got it working. It was a double quote issue. These do tend to mash my mind a bit. Here is the solution

    Code
    wrdApp.ActiveDocument.Bookmarks("" & bmRef & "").Select
  • Hi,


    Yes indeed double quotes in strings can be a bit of a pain, which is why I define a vbDoubleQuote constant to make code more readable.


    As it's raining here in the UK and so nothing better to do I looked at this again this morning here is my solution. It's more elegant to pass the bookmark to the NavigateToWordBookmark function rather than setting a Public Variable.




    I have attached a working spreadsheet and the Word Document I am navigating.


    Regards,


    Tom Rowe

  • Thanks Tom. I'll have a look at it later. I have encountered another problem with my solution since. Whilst it navigates to the bookmark fine, it's leaving it at the bottom of the visual area (ie the bookmark may be the top of the page but the bottom area of the previous page is showing mainly), so I'm currently looking for a way to find the bookmark and then scroll the bookmark to the top of the visual area.

  • Hi,


    To scroll the bookmark into view try using...


    Code
    objWordApplication.Selection.GoTo What:=wdGoToBookmark, Name:=strBookmarkName



    Code
    Private Sub NavigateToWordBookmark(strBookmarkName As String)
    
    
    objWordDocument.Bookmarks(strBookmarkName).Select
    ' Or you can use objWordApplication.ActiveDocument.Bookmarks(strBookmarkName).Select
    objWordApplication.Selection.GoTo What:=wdGoToBookmark, Name:=strBookmarkName
    objWordApplication.Activate
    
    
    End Sub


    However it doesn't scroll perfectly to the top don't know why but think it should be good enough.


    See updated spreadsheet and word document 'Why You Need A Better Data Quality Control' Button for a demo.


    Regards,


    Tom Rowe..

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!