Solved

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

Posted on 2015-01-08
8
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 51

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 51

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 51

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 51

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

724 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