Solved

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

Posted on 2015-01-08
8
247 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 49

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 49

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 49

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 49

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How Resolve Word 2011 Office-Database Error in Mac OS X 10 57
Excel Automation VBA 19 68
stuck in average 16 47
Password protect a PDF document created in Word - can it be done? 6 18
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 viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

786 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