?
Solved

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

Posted on 2015-01-08
8
Medium Priority
?
309 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 54

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 54

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 54

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 54

Accepted Solution

by:
Rgonzo1971 earned 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Currently, there is an issue with being able to copy values from an external application to a dropdown list in Project Web Access (PWA).  The standard copy and paste methods don't seem to work properly. Here is a way to accomplish this task to s…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Suggested Courses

755 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