Additional erroneous book mark created when adding bookmark

D

David Cuthill

I am using the following code to locate and place a bookmark - the
problem is after the code executes I also end up with a bookmark with
a name like _1123334 that happens to be at the same range location.
Any idea why this is taking place?


Set oWordDoc = ActiveDocument
TabCnt = oWordDoc.Tables.Count

For i = 1 To TabCnt Step 4
z = CStr(i)
Set Rng = ActiveDocument.Tables(i).Range
Rng.Copy
Rng.Collapse wdCollapseEnd
Rng.InsertAfter "" & vbCrLf
Rng.Collapse wdCollapseEnd
ActiveDocument.Bookmarks.Add Name:="BarChart" & z, Range:=Rng
...
...
Next i

Thanks for any help.
 
J

Jean-Guy Marcil

David Cuthill was telling us:
David Cuthill nous racontait que :
I am using the following code to locate and place a bookmark - the
problem is after the code executes I also end up with a bookmark with
a name like _1123334 that happens to be at the same range location.
Any idea why this is taking place?


Set oWordDoc = ActiveDocument
TabCnt = oWordDoc.Tables.Count

For i = 1 To TabCnt Step 4
z = CStr(i)
Set Rng = ActiveDocument.Tables(i).Range
Rng.Copy
Rng.Collapse wdCollapseEnd
Rng.InsertAfter "" & vbCrLf
Rng.Collapse wdCollapseEnd
ActiveDocument.Bookmarks.Add Name:="BarChart" & z, Range:=Rng
...
...
Next i

These hidden (hidden because they start with an underscore) bookmarks are
usually added when cross references, links or TOC are added to the document.
Sometimes when you copy/paste between applications, the source or the
destination is marked with a bookmark. I haven't had the inclination to
study this, so I do not know why, but it has never caused me, or anybody I
have heard of, any problems.

I used your code on my Word 2003, and I did not see those hidden bookmarks.
As a suggestion, try to use With ... End With Statements in your code. It
executes faster and makes it lighter to read. For example, your code could
be written as:

'Do not forget to Dim and type your variables...
Dim oWordDoc As Document
Dim TabCnt As Long
Dim Rng As Range

Set oWordDoc = ActiveDocument
With oWordDoc
TabCnt = .Tables.Count

For i = 1 To TabCnt Step 4
z = CStr(i)
Set Rng = .Tables(i).Range
With Rng

'Why Copy? You are not pasting it anywhere, are you?
'Maybe the answer to your question is in the code you
'obviously snipped out... Are you pasting?
'If not, what are you doing?
'If so, see my comments above
' .Copy
.Collapse wdCollapseEnd
' .InsertAfter "" & vbCrLf
'Why the "" ? It does not do anything..., I replaced this by this line:
.InsertParagraphAfter
.Collapse wdCollapseEnd
End With
.Bookmarks.Add Name:="BarChart" & z, Range:=Rng
Next i
End With

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

dcuthill

After sending this post I made one change that seemed to resolve the issue I
was having.

dim BMName as string
BMName = "BarChart" & z
ActiveDocument.Bookmarks.Add Name:=BMName, Range:=Rng

Thank you for the other suggestions. I think the copy command was left over
from something else I had been working on awhile ago.
 

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