Share via

Add and delete a text using a Check Box (ActiveX Controls)

Anonymous
2021-01-08T15:28:34+00:00

Hi,

I want to use a Check Box (ActiveX Controls) to insert(checked) and delete(unchecked) a specific text within the range of a Bookmark. 

Inserting the text wasn't difficult, but how can I delete(when the check box is unchecked) the text which is inserted?

I hope there is a simple way to do this. 

Private Sub chbTest_Click()

If chbTest.Value = True Then

    Dim Test As Range

    Set Test = ActiveDocument.Bookmarks("bkmTest").Range

    Test.Text = "Succeeded!"

Else

    .........??

End If

End Sub


My second step is to use a self made building block instead of a text within in the script. How can I reach the specific building block from VBA? I also want that the building block is added to the document instead of in the Microsoft folders on the computer. 

Can someone explain to me if and how this is possible? 

Sander

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2021-01-08T16:40:48+00:00

For the second piece, building blocks aren't stored in documents, they are stored in templates.  But the code would be something like this:

Private Sub chbTest_Click()

Dim oRng As Range

  If ActiveDocument.Bookmarks.Exists("bkmTest") Then

    Set oRng = ActiveDocument.Bookmarks("bkmTest").Range

    If chbTest Then

      Set oRng = ActiveDocument.AttachedTemplate.BuildingBlockTypes(wdTypeAutoText).Categories("Signatures"). _

                     BuildingBlocks("John Hancock").Insert(Where:=oRng, RichText:=True)

    Else

      oRng.Text = vbNullString

    End If

     ActiveDocument.Bookmarks.Add "bkmTest", oRng

  End If

lbl_Exit:

  Exit Sub

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Anonymous
2021-01-08T16:33:59+00:00

Anytime you write text to a bookmark range, you destroy the bookmark so you have to recreate it:

Private Sub chbTest_Click()

Dim oRng As Range

  If ActiveDocument.Bookmarks.Exists("bkmTest") Then

    Set oRng = ActiveDocument.Bookmarks("bkmTest").Range

    If chbTest Then

      oRng.Text = "Succeeded!"

    Else

      oRng.Text = vbNullString

    End If

    ActiveDocument.Bookmarks.Add "bkmTest", oRng

  End If

lbl_Exit:

  Exit Sub

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-01-11T09:46:00+00:00

    Dear Greg,

    Thank you very much for your help! It works.

    I have only one question left. 

    At the end of the script you use:

    lbl_Exit:

      Exit Sub

    What does this exactly? I don't see any difference if I add or delete it. 

    Sander

    Was this answer helpful?

    0 comments No comments
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more