Solved

VBA - Word, How to delete a bookmark if checkbox is not checked

Posted on 2015-01-08
8
256 Views
Last Modified: 2015-01-08
Hi guys,

I'm very novice in VBA so please be easy on me :P I need some help with figuring out how to delete a bookmark if a certain checkbox is NOT checked.

I know how to delete a bookmark by its name but unsure of how to do it with a checkbox.

kindly advise

this is the code I have:

'Delete choosen bookmark
    ActiveDocument.Bookmarks("test").Range.Delete

Open in new window


here is the Doc I'm working with
list080115.doc
0
Comment
Question by:Hakum
  • 4
  • 4
8 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40537352
Hi,

pls try

Public Sub Deletebookmark()

'Delete choosen bookmark
If CheckBoxXX.Value = False Then
    ActiveDocument.Bookmarks("test").Range.Delete
End If
End Sub

Open in new window

Regards
0
 
LVL 1

Author Comment

by:Hakum
ID: 40537360
would it be practical to put the code into the userform or into a module for itself? i will have a ton of bookmarks and a ton of codes like this, in other words i will have a code like this for each checkbox
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40537403
You should use a reusable sub with arguments

Public Sub Deletebookmark(strBookmark as String, cbkResult as Boolean)

'Delete choosen bookmark
If cbkResult = False Then
    ActiveDocument.Bookmarks(strBookmark ).Range.Delete
End If
End Sub

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Hakum
ID: 40537408
I'm sorry i do not follow, could you kindly elaborate how to use this? and where to put it?

This is how i have used the code you have provided and it works as intended but would be nice if i could make it more flexible instead of typing in the code for each checkbox.. is that what you mean or have i misunderstood?

Private Sub cmdOK_Click()

' Delete all empty tables/rows in table

    Dim oTable As Table, oRow As Range, oCell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean

    ' Specify which table you want to work on.
For Each oTable In ActiveDocument.Tables
    
    ' Set a range variable to the first row's range
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
Application.ScreenUpdating = False

For Counter = 1 To NumRows

    StatusBar = "Row " & Counter
    TextInRow = False

    For Each oCell In oRow.Rows(1).Cells
        If Len(oCell.Range.Text) > 2 Then
            'end of cell marker is actually 2 characters
            TextInRow = True
            Exit For
        End If
    Next oCell

    If TextInRow Then
        Set oRow = oRow.Next(wdRow)
    Else
        oRow.Rows(1).Delete
    End If

Next Counter

Next oTable

Application.ScreenUpdating = True


'Delete selected bookmark
    If CheckBox14.Value = False Then
        ActiveDocument.Bookmarks("test").Range.Delete

    End If

    ActiveDocument.Fields.Update

    UserForm1.Hide

End Sub

Open in new window

0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40537413
pls try

Private Sub cmdOK_Click()

' Delete all empty tables/rows in table

    Dim oTable As Table, oRow As Range, oCell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean

    ' Specify which table you want to work on.
For Each oTable In ActiveDocument.Tables
    
    ' Set a range variable to the first row's range
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
Application.ScreenUpdating = False

For Counter = 1 To NumRows

    StatusBar = "Row " & Counter
    TextInRow = False

    For Each oCell In oRow.Rows(1).Cells
        If Len(oCell.Range.Text) > 2 Then
            'end of cell marker is actually 2 characters
            TextInRow = True
            Exit For
        End If
    Next oCell

    If TextInRow Then
        Set oRow = oRow.Next(wdRow)
    Else
        oRow.Rows(1).Delete
    End If



Next Counter

Next oTable

Application.ScreenUpdating = True


'Delete selected bookmark
'     If CheckBox14.Value = False Then
'        ActiveDocument.Bookmarks("test").Range.Delete
'
'    End If
    Deletebookmark("test", CheckBox14.Value)
    ActiveDocument.Fields.Update

    UserForm1.Hide

End Sub

Public Sub Deletebookmark(strBookmark as String, cbkResult as Boolean)

'Delete choosen bookmark
If cbkResult = False Then
    ActiveDocument.Bookmarks(strBookmark ).Range.Delete
End If
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Hakum
ID: 40537420
its highlighted red and give a Syntax error when i try to run it now

error
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40537433
Sorry

Deletebookmark "test", CheckBox14.Value
0
 
LVL 1

Author Closing Comment

by:Hakum
ID: 40537436
AWESOME!!! thanks a ton!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question