delete a bookmark and then row

M

mark kurten

i apologize in advance for asking so many questions..i'm rewriting this
urgent app (that someone else wrote initially) and need it done by monday
(so no time to research)

in a word doc if i have

[bookmark1]
[bookmark2]
[bookmark3]
[bookmark4]

Dear Someone:

--In my code, i set bookmark1 to some value. how do i delete bookmark2,
bookmark3 and bookmark4 and then delete the actual lines in the word doc, so
that bookmark1 has only 1 blank space in between it and Dear Someone

I've used the delete method of the bookmark object, but that just seems to
remove the bookmark and not the line.

Thanks in advance.
 
D

Doug Robbins - Word MVP

Hi Mark,

I am not sure why you would not just open the template and delete the
bookmarks there, once and for all.

The following code should however do what you want:

With ActiveDocument
.Bookmarks("Bookmark2").Range.Select
.Bookmarks("\line").Range.Delete
.Bookmarks("Bookmark3").Range.Select
.Bookmarks("\line").Range.Delete
.Bookmarks("Bookmark4").Range.Select
.Bookmarks("\line").Range.Delete
End With


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi mark kurten

It depends on just how each line is terminated. Lets assume each line is in fact a
paragraph (if you turn on Show/Hide all) you can see the symbol "¶" at the end of each
line. You can use the following code:

Public Sub DeleteParaGraphContainingBookmark()

With ActiveDocument.Bookmarks("BM1").Range
.Expand wdParagraph
.Text = vbNullString
End With
End Sub

This will delete the entire "line" containing the bookmark including any other text on
that "line".

HTH + Cheers - Peter
 
M

mark kurten

to answer your question:

This template is populated with data which is very dynamic. One generation
of the .doc i might need to use bookmark2, bookmark3 and bookmark4. The
very next generation of the doc, I might not need them and therefore I would
like to delete them.

Thank you for the help.
also,
where can i find documentation regarding the \line parameter?

thanks again.



Doug Robbins - Word MVP said:
Hi Mark,

I am not sure why you would not just open the template and delete the
bookmarks there, once and for all.

The following code should however do what you want:

With ActiveDocument
.Bookmarks("Bookmark2").Range.Select
.Bookmarks("\line").Range.Delete
.Bookmarks("Bookmark3").Range.Select
.Bookmarks("\line").Range.Delete
.Bookmarks("Bookmark4").Range.Select
.Bookmarks("\line").Range.Delete
End With


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
mark kurten said:
i apologize in advance for asking so many questions..i'm rewriting this
urgent app (that someone else wrote initially) and need it done by monday
(so no time to research)

in a word doc if i have

[bookmark1]
[bookmark2]
[bookmark3]
[bookmark4]

Dear Someone:

--In my code, i set bookmark1 to some value. how do i delete bookmark2,
bookmark3 and bookmark4 and then delete the actual lines in the word
doc,
so
that bookmark1 has only 1 blank space in between it and Dear Someone

I've used the delete method of the bookmark object, but that just seems to
remove the bookmark and not the line.

Thanks in advance.
 
P

Peter Hewett

Hi Doug Robbins - Word MVP

I frequently use code like that for filling out address blocks where not all of the
parameters are always present, but neither do you want blanks in it.

Cheers - Peter


Hi Mark,

I am not sure why you would not just open the template and delete the
bookmarks there, once and for all.

The following code should however do what you want:

With ActiveDocument
.Bookmarks("Bookmark2").Range.Select
.Bookmarks("\line").Range.Delete
.Bookmarks("Bookmark3").Range.Select
.Bookmarks("\line").Range.Delete
.Bookmarks("Bookmark4").Range.Select
.Bookmarks("\line").Range.Delete
End With

HTH + Cheers - Peter
 
J

Jay Freedman

If you can assume each line is actually a paragraph, you can simplify
that to

Public Sub DeleteParaContainingBookmark(BkNm As String)
ActiveDocument.Bookmarks(BkNm).Range _
.Paragraphs(1).Range.Delete
End Sub

and call it with

DeleteParaContainingBookmark "bm2"
DeleteParaContainingBookmark "bm3"
DeleteParaContainingBookmark "bm4"


Peter Hewett said:
Hi mark kurten

It depends on just how each line is terminated. Lets assume each line is in fact a
paragraph (if you turn on Show/Hide all) you can see the symbol "¶" at the end of each
line. You can use the following code:

Public Sub DeleteParaGraphContainingBookmark()

With ActiveDocument.Bookmarks("BM1").Range
.Expand wdParagraph
.Text = vbNullString
End With
End Sub

This will delete the entire "line" containing the bookmark including any other text on
that "line".

HTH + Cheers - Peter


i apologize in advance for asking so many questions..i'm rewriting this
urgent app (that someone else wrote initially) and need it done by monday
(so no time to research)

in a word doc if i have

[bookmark1]
[bookmark2]
[bookmark3]
[bookmark4]

Dear Someone:

--In my code, i set bookmark1 to some value. how do i delete bookmark2,
bookmark3 and bookmark4 and then delete the actual lines in the word doc, so
that bookmark1 has only 1 blank space in between it and Dear Someone

I've used the delete method of the bookmark object, but that just seems to
remove the bookmark and not the line.

Thanks in advance.
 
M

mark kurten

Peter,

Where would the blanks appear?

I'm not sure I understand....
Are you referencing the suggestion from Doug "I am not sure why you would
not just open the template and delete the bookmarks there, once and for
all."

If so, I understand why i would not delete the bookmark solely. This would
produce just a blank line, correct?

thanks.
 
M

mark kurten

I'm not sure how this is different from Doug's suggestion. Won't they both
do the same thing. My objective is to delete the "line" which is a bookmark
is on.

Or is this just another way to do it?

thanks.


Peter Hewett said:
Hi mark kurten

It depends on just how each line is terminated. Lets assume each line is in fact a
paragraph (if you turn on Show/Hide all) you can see the symbol "¶" at the end of each
line. You can use the following code:

Public Sub DeleteParaGraphContainingBookmark()

With ActiveDocument.Bookmarks("BM1").Range
.Expand wdParagraph
.Text = vbNullString
End With
End Sub

This will delete the entire "line" containing the bookmark including any other text on
that "line".

HTH + Cheers - Peter


i apologize in advance for asking so many questions..i'm rewriting this
urgent app (that someone else wrote initially) and need it done by monday
(so no time to research)

in a word doc if i have

[bookmark1]
[bookmark2]
[bookmark3]
[bookmark4]

Dear Someone:

--In my code, i set bookmark1 to some value. how do i delete bookmark2,
bookmark3 and bookmark4 and then delete the actual lines in the word doc, so
that bookmark1 has only 1 blank space in between it and Dear Someone

I've used the delete method of the bookmark object, but that just seems to
remove the bookmark and not the line.

Thanks in advance.
 
D

Doug Robbins - Word MVP

Hi Mark,

The code that I gave you will work whether each bookmark is in a separate
paragraph or on a separate line with a manual line feed (Shift+Enter)
separating the lnes.

The \line bookmark is one of a number of pre-defined bookmarks in Word.

Check out that subject in the VBA Help File for the detals.
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
mark kurten said:
I'm not sure how this is different from Doug's suggestion. Won't they both
do the same thing. My objective is to delete the "line" which is a bookmark
is on.

Or is this just another way to do it?

thanks.


Peter Hewett said:
Hi mark kurten

It depends on just how each line is terminated. Lets assume each line
is
in fact a
paragraph (if you turn on Show/Hide all) you can see the symbol "¶" at
the
end of each
line. You can use the following code:

Public Sub DeleteParaGraphContainingBookmark()

With ActiveDocument.Bookmarks("BM1").Range
.Expand wdParagraph
.Text = vbNullString
End With
End Sub

This will delete the entire "line" containing the bookmark including any other text on
that "line".

HTH + Cheers - Peter


i apologize in advance for asking so many questions..i'm rewriting this
urgent app (that someone else wrote initially) and need it done by monday
(so no time to research)

in a word doc if i have

[bookmark1]
[bookmark2]
[bookmark3]
[bookmark4]

Dear Someone:

--In my code, i set bookmark1 to some value. how do i delete bookmark2,
bookmark3 and bookmark4 and then delete the actual lines in the word
doc,
 
P

Peter Hewett

Hi mark kurten

Take the situation where you have an address block like this in your template:

[First_Name] [Last_Name]
[Position]
[Company]
[Street_Address]
[Town]
[City]
[Post_Code]
[Country]

If the correspondence is to a private individual, or an individual operating in a private
capacity - you would *not* want the address block in your letter to look like this:

<Peter Hewett


216b Anywhere Street

Any City


So the easy way out is to delete the lines containing empty bookmarks, so the text looks
like this:

<Peter Hewett
216b Anywhere Street
Any City>

As an alternative you could always programmatically concatenate the address elements and
insert them into just one bookmark, but sometimes other constraints predicate the delete
line containing bookmark method.

HTH + Cheers - Peter
 
M

mark kurten

you've described my situation perfectly, so both your suggestion and dougs
will work the same?

thanks to both for the help.



Peter Hewett said:
Hi mark kurten

Take the situation where you have an address block like this in your template:

[First_Name] [Last_Name]
[Position]
[Company]
[Street_Address]
[Town]
[City]
[Post_Code]
[Country]

If the correspondence is to a private individual, or an individual operating in a private
capacity - you would *not* want the address block in your letter to look like this:

<Peter Hewett


216b Anywhere Street

Any City


So the easy way out is to delete the lines containing empty bookmarks, so the text looks
like this:

<Peter Hewett
216b Anywhere Street
Any City>

As an alternative you could always programmatically concatenate the address elements and
insert them into just one bookmark, but sometimes other constraints predicate the delete
line containing bookmark method.

HTH + Cheers - Peter


Peter,

Where would the blanks appear?

I'm not sure I understand....
Are you referencing the suggestion from Doug "I am not sure why you would
not just open the template and delete the bookmarks there, once and for
all."

If so, I understand why i would not delete the bookmark solely. This would
produce just a blank line, correct?

thanks.



not
all of the
 

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