Move placeholder bookmarks to end of paragraph

D

David Turner

In the Word documents I translate, placeholder bookmarks (which appear as a
grey "I") are often inserted randomly right in the middle of a word.
When I import the file into the translation environment tool I use, such
bookmarks appear as a tag {1} in the middle of a word which prevents terms
being recognized (i.e. te{2}rm).

I thus want to move these placeholders to the end of the paragraph they are
located in.

I've spent hours trying to understand how these bookmarks work and looking
at related VBA code samples in the help, and after a lot of trial and error,
I at last came up with the code below.

When you step through it, it looks like its going to work. The first
bookmark is moved to the of the paragraph and the insertion point goes to the
start of the next paragraph. When it gets to the next loop, however, the code
does not go to the next bookmark but instead tries to go back to the first
one just moved. Can anyone put me straight. I guess it's some short of
range/selection collapse problem.

Many thanks in advance.


Sub MoveBkmrk()

Dim r As Range
Dim aBookmark As Bookmark
Dim char

If ActiveDocument.Bookmarks.Count >= 1 Then
ReDim aMarks(ActiveDocument.Bookmarks.Count - 1)
i = 0
For Each aBookmark In ActiveDocument.Bookmarks
If ActiveDocument.Bookmarks(aBookmark).Empty = True Then
aMarks(i) = aBookmark.Name
i = i + 1
aBookmark.Select
char = Selection.EndOf(Unit:=wdParagraph, Extend:=wdMove)
Selection.MoveLeft Unit:=wdCharacter, Count:=1
ActiveDocument.Bookmarks.Add Range:=Selection.Range,
Name:=ActiveDocument.Bookmarks(i).Name
Selection.MoveRight Unit:=wdCharacter, Count:=1
End If
Next aBookmark
End If

End Sub
 
D

Doug Robbins - Word MVP

Use:

Dim abmr As Range

With ActiveDocument
For i = .Bookmarks.Count To 1 Step -1
If .Bookmarks(i).Empty = True Then
Set abmr = .Bookmarks(i).Range.Paragraphs(1).Range
abmr.Collapse wdCollapseEnd
abmr.Move wdCharacter, -1
.Bookmarks.Add .Bookmarks(i).Name, abmr
End If
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

David Turner

Wonderful! Thank you very much Doug. All ranges and no clumsy moving of
selections. And the bookmarks are stepped through backwards. Hadn't thought
of that. Any particular reason for counting down?
Your help is much appreciated.
Regards,
David
 
D

Doug Robbins - Word MVP

You start at the end of the document and count down so that you don't come
across the same bookmark twice.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

David Turner

There's usually only one placeholder in each paragraph but if there happens
to be more than one, they end up superimposed on each other as one "I" at the
end of the paragraph. Great. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top