Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Macro to delete empty bookmarks and space it occupies.

675 views
Skip to first unread message

durmalbs

unread,
Oct 29, 2009, 4:56:01 PM10/29/09
to
I have a document that once I run the macro to insert text from combo box
choices it still has some blank bookmarks that I would like to have a macro
to delete the bookmarks and the line that was marked ( hope this is not
confusing).
so the document looks like this,

3.42 run packer
3.43 (Bookmark "TextP1")
3.44 Set packer

So I need the macro to delete the whole line 3.43 and move 3.44 up.

Any help would be appreciated I am very new to macros so be gentle if
possible.

macropod

unread,
Oct 29, 2009, 7:00:36 PM10/29/09
to

Hi durmalbs,

Try:
Sub KillEmptyBkMrkParas()
Dim oBkMk As Bookmark, bBkState As Boolean
With ActiveDocument
bBkState = .Bookmarks.ShowHidden
.Bookmarks.ShowHidden = False
For Each oBkMk In .Bookmarks
If .Bookmarks(oBkMk).Empty = True Then oBkMk.Range.Paragraphs(1).Range.Delete
Next
.Bookmarks.ShowHidden = bBkState
End With
End Sub

Note: I've taken your reference to 'blank bookmarks' to mean they're empty.

--
Cheers
macropod
[Microsoft MVP - Word]


"durmalbs" <durm...@discussions.microsoft.com> wrote in message news:6D44034D-3E64-463B...@microsoft.com...

durmalbs

unread,
Oct 30, 2009, 3:41:01 PM10/30/09
to
This deletes the bookmarks,but it does all of them. When I hit my command
button it inserts the text into the appropriate bookmark, then there are some
bookmarks left I need to be able to delete those left. When I insert this
then pick my combo box choices the hit my command button to run it adds the
text to the appropriate bookmark, but when I then run this Sub it removes the
bookmarks where the text was inserted along with the blanks.

Any suggestions?

"macropod" wrote:

> .
>

macropod

unread,
Oct 30, 2009, 9:31:47 PM10/30/09
to
Hi durmalbs,

The problem is that whatever your code does, it doesn't populate the bookmarks. As requested, though, the macro deletes the
paragraphs containing empty bookmarks. You have two choices:
1. populate the bookmarks correctly; or
2. modify the macro to delete empty paragrahs containing bookmarks.

--
Cheers
macropod
[Microsoft MVP - Word]


"durmalbs" <durm...@discussions.microsoft.com> wrote in message news:E15A24DD-28D9-4822...@microsoft.com...

durmalbs

unread,
Nov 2, 2009, 9:04:44 AM11/2/09
to
Here is an example of how the bookmark is populated by the command button
from info entered in Combo Box "Sleeve". The bookmark is named "TextSleeve",
is this not correct?

Private Sub CommandButton1_Click()
If cboSleeve > "0" Then
ActiveDocument.Bookmarks("TextSleeve").Select
Selection.TypeText Text:="Packer Setting"
End If
End Sub


"macropod" wrote:

> .
>

macropod

unread,
Nov 2, 2009, 2:43:00 PM11/2/09
to
Hi durmalbs,

That code doesn't update the bookmarks - it inserts text next to them. Since it seems you're updating numerous bookmarks, try:

Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
With ActiveDocument
If.Bookmarks.Exists(BmkNm) Then
Set BmkRng =.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
End if
End With
Set BmkRng = Nothing
End Sub

which you can call from your command buttons like:

Private Sub CommandButton1_Click()
If cboSleeve > "0" Then

Call UpdateBookmark("TextSleeve", "Packer Setting")
End If
End Sub

--
Cheers
macropod
[Microsoft MVP - Word]


"durmalbs" <durm...@discussions.microsoft.com> wrote in message news:45FADDC1-1650-4973...@microsoft.com...

durmalbs

unread,
Nov 2, 2009, 4:49:01 PM11/2/09
to
I read your last post about it putting text beside instead of updating the
bookmark.
I appreciate your help alot, but I am not really understanding the
code(nothing new). But if I want to update the bookmark with the appropriate
text how would I load this do i need to change the reference in the below sub
to anything

The bookmark is "TextSleeve" and the text to insert is "Packer Setting"
would these be entered below where you show BmkNm and NewTxt.

Sorry for the stupidity

Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
With ActiveDocument
If.Bookmarks.Exists(BmkNm) Then
Set BmkRng =.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
End if
End With
Set BmkRng = Nothing
End Sub


which you can call from your command buttons like:

Private Sub CommandButton1_Click()
If cboSleeve > "0" Then

Call UpdateBookmark("TextSleeve", "Packer Setting")
End If
End Sub

macropod

unread,
Nov 3, 2009, 1:36:04 AM11/3/09
to
Hi durmalbs,

Simple: Delete your existing CommandButton1_Click sub and replace it with the one I posted, plus the UpdateBookmark sub.

If, as I understand it, you have other subs like your CommandButton1_Click sub, simply modify them along the lines of the changes I
made to your CommandButton1_Click sub.

--
Cheers
macropod
[Microsoft MVP - Word]


"durmalbs" <durm...@discussions.microsoft.com> wrote in message news:EA43FC85-9C89-4303...@microsoft.com...

durmalbs

unread,
Nov 3, 2009, 9:14:01 AM11/3/09
to
I must not be loading right here is what I installed and now when I hit the
command button after choosing my option in cboSleeve it input s nothing, here
is how I loaded the macro. I 'm sure I've done something wrong with how I
loaded it or loaded in the wrong place.

Sub UpdateBookMark(BmkNm As String, NewTxt As String)
Dim BmkRng As Range
With ActiveDocument
If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.Text = NeTxt
.Bookmarks.Add BmkNm, BmkRng
End If
End With
Set NmkRng = Nothing
End Sub

Private Sub CommandButton1_Click()
If cboSleeve > "0" Then

Call UpdateBookMark("TextSLEEVE", "Packer Setting")
End If
End Sub

"macropod" wrote:

> .
>

macropod

unread,
Nov 3, 2009, 4:49:57 PM11/3/09
to
Hi durmalbs,

I suspect you haven't got 'Option Explicit' set, as there's a typo in your rendition of the UpdateBookMark sub.

--
Cheers
macropod
[Microsoft MVP - Word]


"durmalbs" <durm...@discussions.microsoft.com> wrote in message news:DB6D1B2D-FCCB-4DEC...@microsoft.com...

durmalbs

unread,
Nov 3, 2009, 5:14:01 PM11/3/09
to

I finally got the Subs to work I really appreciate it. Now another question
is there a way to tell it to not remove certain bookmarks?

"macropod" wrote:

> .
>

macropod

unread,
Nov 4, 2009, 5:43:46 AM11/4/09
to
Hi durmalbs,

Sure, just modify the code at:


If .Bookmarks(oBkMk).Empty = True Then oBkMk.Range.Paragraphs(1).Range.Delete

to test against the relevant set of bookmark names. For a single bookmark name, you might use:
If .Bookmarks(oBkMk).Empty = True Then _
If .Bookmarks(oBkMk).Name <> "MyName" Then oBkMk.Range.Paragraphs(1).Range.Delete
For multiple bookmarks, I'd be inclined to create another string variable, insert the list in comma-separated format then, after the
'Empty' test, use a loop in conjunction with Split to check though the list entries. If not found in the list, then delete. There's
plenty of code snippets about to show you how to do that.

--
Cheers
macropod
[Microsoft MVP - Word]


"durmalbs" <durm...@discussions.microsoft.com> wrote in message news:6E43EDAB-CA12-422F...@microsoft.com...

0 new messages