PDA

View Full Version : Bookmarked CheckBox



klm1780
02-27-2015, 11:24 AM
I have a very old Word document that has a broken macro that I am trying to fix but I have limited VBA knowledge.
Currently there are Legacy Forms checkmark boxes used in the following macro:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
Private Sub showBookmarks()
Dim d As Document
Set d = Documents.Add("C:\Users\Owner-PC\Desktop\Test1.docm")
'Dim b As Bookmark
'Set b = d.Bookmarks(




'Deck Preparation
If (ActiveDocument.FormFields("deckscope2c").CheckBox.Value = False) Then
ActiveDocument.Bookmarks("deckscope2").Range.Delete
End If

The problem is that I can't check those checkmark boxes without the properties box coming up and I also can't tab through the boxes so I would like to change them to Command (Content) Control checkmark boxes which I can tab to and check off. Unfortunately when I tried to change the macro to read:

If (ActiveDocument.CommandControls("deckscope2c").CheckBox.Value = False) Then
ActiveDocument.Bookmarks("deckscope2").Range.Delete
End If

It gives me a runtime error 438: Object doesn't support this property or method.

Any suggestions would be appreciated :)

gmayor
03-04-2015, 07:43 AM
The reason you cannot tab through the legacy form is that it is not protected for forms. Legacy forms must be protected for forms.

With that protection in place you could use something like the following to delete the checkbox if it is unchecked.


Dim bProtected As Boolean
If ActiveDocument.FormFields("deckscope2c").CheckBox.Value = False Then
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
ActiveDocument.FormFields("deckscope2c").Range.Delete
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
End If
End If

If you have changed the form fields to content controls and have titled the content controls then to delete the "deckscope2c" content control if it is unchecked



Dim oCC As ContentControl
Dim oRng As Range
For Each oCC In ActiveDocument.ContentControls
If oCC.Title = "deckscope2c" Then
If oCC.Checked = False Then
Set oRng = oCC.Range
oCC.Delete
oRng.Delete
End If
End If
Next oCC

gmaxey
03-04-2015, 06:08 PM
To delete a titled CC you can address it explicitly:


Sub ScratchMacro()
Dim oRng As Word.Range
'A basic Word macro coded by Greg Maxey
If Not ActiveDocument.SelectContentControlsByTitle("deckscope2c").Item(1).Checked Then
Set oRng = ActiveDocument.SelectContentControlsByTitle("deckscope2c").Item(1).Range
ActiveDocument.SelectContentControlsByTitle("deckscope2c").Item(1).Delete
oRng.Delete
End If
lbl_Exit:
Exit Sub
End Sub