Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting bookmarks

Status
Not open for further replies.

Pegasus0990

Technical User
Nov 16, 2006
27
0
0
US
thread707-1009355

the basic solution was the following. it works great but is there a easy way to expand it to satisfy my question below? I modified the original slightly to adjust to my code.

[VBCODE]
Selection.GoTo What:=wdGoToBookmark, Name:="bookmark1"
Selection.Bookmarks("\Line").Range.Delete
[/VBCODE]

//what the file looks like in word,
//note *@ = "bookmark1." *#= "bookmark2." *$ = "bookmark3."

*@1.01a Question 1

*#Answer 1

COA

*$COA1

//
What VB code would help me delete all the above text if i need the bookmark for '1.01a' to be deleted? note there is a double space between each line provided, and "Answer1" and "COA1" are indented.

thanks for all help
Justin

 
Hi Justin,

Try:
Code:
Sub Test()
ActiveDocument.Bookmarks("bookmark1").Range.Select
With Selection
    .MoveEndUntil Cset:=ActiveDocument.Bookmarks("bookmark3").Range, Count:=wdForward
    .MoveEndUntil Cset:=ActiveDocument.Bookmarks("\Line").Range, Count:=wdForward
    .Delete
End With
End Sub

Cheers

[MS MVP - Word]
 
Bookmarks can be nested.

I would have - if I understand this correctly - the whole thing bookmarked.

Make the whole thing a bookmark "BigChunk", and you can have the other bookmarks within it no problem.

Then if you want to delete the whole thing, you can just by using the BigChunk bookmark. It will delete the whole text (and any paces between things), and all the bookmarks (including the BigChunk bookmark).

Gerry
My paintings and sculpture
 
macropod and fumei

when i use your code, it only deletes "bookmark1" and "1" but not the rest. this is what is left.

.01a Question 1

*#Answer 1

COA

*$COA1

i know what something 'nested' is fumei, but could you give me an example of what a nested bookmark looks like/or how to do it?

thanx for your help.
 
Hi Justin,

When I run the code against a document laid out as you described, it deletes everthing from the start of bookmark1 to then end of the line on which bookmark3 appears. Isn't that what you wanted?

Cheers

[MS MVP - Word]
 
that is exactly what i want macropod.

but for some reason, it is only deleting the first character after bookmark1. any guess as to why?
 
Hi Justin,

I'd suggest re-checking your bookmark orders and ranges.

Cheers

[MS MVP - Word]
 
If you turn bookmarks on as visible, they use grey square brackets...so I will use that to indicate.

[highlight][This is some text.[/color green]] This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.

This is some text. [[This is some text.][/color red] ]


This is some text. This is some text. This is some text. This is some text. [This is some text. This is some text.[/color blue]] [/highlight]

Bookmark1 is green text.
Bookmark2 is red text.
Bookmark3 is blue text
Bookmark4 is the whole thing, and is yellow. Bookmark4 contains the other three. If you delete, by name, THAT bookmark range...everything is deleted.

BTW: the numeric order is not relevant. You can mark a large bookmark then make smaller ones inside it after; or bookmark with them already in it. It doesn't matter.

As for you deleting only the character...are the contents in the bookmarks put there by code? If so, I would bet you are doing with something like:
Code:
ActiveDocument.Bookmarks("bookmark1").Text = "yadda yadda yadda"

That does not put the text inside the bookmark range. Even:
Code:
ActiveDocument.Bookmarks("bookmark1").Range.Text = "yadda yadda"
does not put the text inside the range - even though Help says the Range is expanded to include inserted text. In 2002, it does not do this. I am not sure if this was fixed in later versions. There is simple code that WILL always put text inside the actual bookmark range.

In any case, the question is...are the contents you think should be there, put there by code? When you use Insert > Bookmarks and boookmark1, does it select the range of text you are expecting?

If it does not, then as macropod suggests...check your ranges.

Gerry
My paintings and sculpture
 
maybe my inexperienced is the simple issue here. how i made my bookmarks.

1. insert bookmark, ie 'bookmark1'
2. set up code inside of Microsoft Word VB. this code pulls infomation from Microsof Excel. this works great. but when the code should not be pulled (if-then) statement, it is suppose to delete the bookmarks and the word template i displayed in my first post. however, instead it just deletes one character when i use the code that macropod suggested. and it does not even delete the bookmark.

now when i turn my bookmarks to visable i only see one half of the square brackets, "[" it sounds like you suggest i should also see a "]" and if that were the case i would understand how the bookmarks are being nested. am i missing something?

the actual code i am using to get Word to pull info from Excel is follows (note myWB.Sheets("Facility") is my excel, and the 'if' half works perfect):
[VBCODE]
If Worksheets("Facility").Cells(a, i) = "No" Then
oDoc.Bookmarks("bookmark2").Range.Text = myWB.Sheets
("Facility").Cells(a, j)
oDoc.Bookmarks("bookmark3").Range.Text = myWB.Sheets
("Facility").Cells(a, l)
Else
delete bookmark1, 2, and 3
[/VBCODE]

just rereading your post fumei, i wonder if it is as simple as to insert the other half of the bracket, hmmm.

thanx for all help

 
Hi Justin,

There was an error in the previous code - it would work only if there was 1 character in each bookmark.

Here's a revised version that should work in all cases.
Code:
Sub Test()
Dim oRange As Range
Dim sRange As Range
Dim eRange As Range
With ActiveDocument
    Set sRange = .Bookmarks("bookmark1").Range
    Set eRange = .Bookmarks("bookmark3").Range
    With eRange
        .MoveEndUntil Cset:=ActiveDocument.Bookmarks("\Line").Range, Count:=wdForward
        .MoveEnd Unit:=wdWord, Count:=1
    End With
    Set oRange = .Range(Start:=sRange.Start, End:=eRange.End)
    oRange.Delete
End With
End Sub

Cheers

[MS MVP - Word]
 
wow, macropod, awesome, thanx a lot.

fumei, i believe the nesting bookmarks you mention are actually 'fields.' i was looking at this, but didnt actually understand how to set it up. CTRL-F9 brings it up but then it suggests entering something like

{SET bookmark1 "blah blah"}

but, bookmark1 does not exist in the list of bookmarks. so how do you actually delete a field in VB code?

again, many thanx for your two's expertise and willingness to assist me.
 
Hi Justin,

Regarding the idea Gerry proposed, if you were to bookmark the whole range with a single bookmark, then you could simply delete that range, rather than having to concatenate them as I did in code.
For example, taking your initial definitions:
*@1.01a Question 1

*#Answer 1

COA

*$COA1
you could wrap all three existing bookmarks and the rest of the 'COA1' line in a single bookmark.

This has nothing to do with fields - although SET and ASK fields can be used to create bookmarks.

There also seems to be some confusion on your part abut the bookmark brackets. If a bookmark contains no characters, then it will appear as a grey bar (ie '|') but, if some characters are included, they'll be enclosed in gey square brackets (ie '[string]'). The bar and brackets are both non-printing.

Cheers

[MS MVP - Word]
 
The reason I mentioned showing the bookmark as visible, was to see if you actually HAD text within the ranges. Macropod is right (again) that if the range only has a location - the Start and End numbers are the same - then there is no visible "end" bracket. If there IS text, then you see opening and closing brackets.

I am still a bit confused as to how you have this set up, but the point (or at least my point) is that you can nest bookmarks in bookmarks.

You can make the whole thing one bookmark, and delete the whole thing with one instruction. That whole (BIG) bookmark can contain the three other ones.

They are not fields.



Gerry
My paintings and sculpture
 
i 'now' fully understand the difference between a bookmark by location vs one that includes text. i had not realized how to make one which included text. simply a matter of highlighting what i want bookmarked, and clicking 'insert bookmark,' vs clicking on a location (no highlighting) and inserting a bookmark (which i had been doing).

i had all of my bookmarks set up by location, and all looked liked "I" vs the "[ ]."

my program has about 1500 bookmarks, let me tell you, not so fun when i had not programed in some 4 years.

thanx a lot macropod and fumei. you two have answered all of my questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top