PDA

View Full Version : Create bookmarks automation



cpampas
12-26-2018, 12:24 AM
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 ?

gmayor
12-26-2018, 03:29 AM
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 (http://www.gmayor.com/insert_content_control_addin.htm) useful.

cpampas
12-26-2018, 07:27 AM
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

gmayor
12-26-2018, 09:42 PM
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

cpampas
12-26-2018, 11:34 PM
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

gmayor
12-27-2018, 07:07 AM
How would the macro know that the text being written is text to be bookmarked and not just an old text?

cpampas
12-27-2018, 08:06 AM
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