Solved

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

Posted on 2015-01-08
8
238 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 48

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 48

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 48

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 48

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now