Consulting

Results 1 to 7 of 7

Thread: Create bookmarks automation

  1. #1
    VBAX Regular
    Joined
    Dec 2018
    Posts
    6
    Location

    Create bookmarks automation

    Hello,
    I am a complete newbie with Ms Word VBA, so I would appreciatte your help with this ;

    I do have literaly thousands of word documents to wich I have to insert bookmarks, therefor I would like to automate this procedure as much as posible. I can think of a couple of things
    ns

    -when naming a bookmark, if it has more than one word can I allow the space character and automaticaly replace it by a hifen character (_)
    - after selecting the part of the text where the bookmark js to be created, can I replace that selection with "XXX", and automaticaly open the insert bookmark dialog window

    I dont even know that this posible
    Any clues ?
    Last edited by cpampas; 12-26-2018 at 01:25 AM.

  2. #2
    What is the purpose of this exercise? Without knowing what you are doing and why, it is impossible to make any sensible response to your question.
    If this is to be 'automated' what determines where the bookmarks would go?
    As bookmark names do not contain spaces, why would you try and name them with spaces only to remove the spaces later?
    If you are replacing texts with bookmarks that contain alternative texts, why not use content controls instead? You may then find http://www.gmayor.com/insert_content_control_addin.htm useful.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Dec 2018
    Posts
    6
    Location
    GMayor,
    You are right, I should had explained it better.

    Considering that there are so many documents (thousands) to be bookmarked, I just want to make it easier and faster for the person bookmarking the documents by automating some of the steps. Of course it is up to the user to decide where is the bookmark going to be placed, by selecting the text, let's say the text in my word template is :

    this is a word document created by ________ (name of the person), who is our employee

    1- the user would select the text : ________ (name of the person)
    2- when releasing the mouse button, the text selected would be replaced with "XXX". I will need these characters for later use in my Ms Access database
    3- and automaticaly would open the insert bookmark window
    4- with the focus on the bookmark name control, the user then types the bookmark name as : persons_name (the space key would type "_" ) . it's faster and friendly to the user .


    my bookmark names can contain 4 or 5 words separeted by the hyphen character, that works for me as a delimeter to be read by my MS Access database (already working properly)

    The ms Access part is done but just for the sake of understanding the general idea:

    the MS Access database, stores the path of thousands of word template documents (web hosting), upon selection the document is opened, and all the bookmarks are read into a form
    In this case a text box control label would be "Persons Name", and in the text box I would type "John Lennon", and the same with all the other bookmarks of the document.
    Finaly clicking a button opens the document with the John Lenon name and all the other data.

    In case there is some VBA to achieve this, I guess it should be partof as module in each one of the documents. right ?
    Thanks for your help

  4. #4
    Does the word or phrase (e.g. the name) appear only once in the document? Bookmark names must be unique so only the last found name would be bookmarked. That might not matter if you are collating the bookmarks from the document, but if it does then it's another reason to use content controls instead. However as you appear to be selecting the text to bookmark rather than automating it, the following macro will replace the selected text with XXX and add a bookmark named from the selection (provided the selection does not contain illegal characters). The code goes in the normal template so that it applies to all documents - http://www.gmayor.com/installing_macro.htm
    Option Explicit
    
    Sub ReplaceSelectionWithBookmark()
    'Graham Mayor - https://www.gmayor.com - Last updated - 27 Dec 2018
    Const strBM As String = "XXX"
    Dim oRng As Range
    Dim strText As String
    Dim i As Integer
        Set oRng = Selection.Range
        If Len(oRng) > 4 And Len(oRng) <= 40 Then
            oRng.MoveEndWhile Chr(32), wdBackward
            oRng.MoveStartWhile "0123456789"
            strText = Replace(oRng.Text, Chr(32), Chr(95))
            If strText = "" Then GoTo err_Handler
            oRng.Text = strBM
            oRng.Bookmarks.Add strText
            oRng.Collapse 0
            oRng.Select
        Else
            GoTo err_Handler
        End If
        Set oRng = Nothing
    lbl_Exit:
        Exit Sub
    err_Handler:
        Beep
        MsgBox "Select a non-numeric text between 4 and 40 characters long."
        GoTo lbl_Exit
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Dec 2018
    Posts
    6
    Location
    Graham,
    Thanks a lot, It works great
    I just wonder if something else is posible :
    To select/hightlight the text that will become the new bookmark as it is being written ? thay way I would skip this step and just run your code.
    All the best
    Carlos

  6. #6
    How would the macro know that the text being written is text to be bookmarked and not just an old text?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Dec 2018
    Posts
    6
    Location
    hi,
    I dont think there would be any case where a document would be edited other than to insert a bookmark, so I was hoping there is a way to incrementaly select the characters there are being typed, so that when the typing is finished I would only had to run the macro

    thanks
    Carlos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •