Consulting

Results 1 to 7 of 7

Thread: Add FormField and Text to text that already Exists in a Table Cell

  1. #1

    Add FormField and Text to text that already Exists in a Table Cell

    I have a Word Template that uses VBA to maintain Legacy form fields in over in thousands of documents that are based on that template. The maintenance of the form fields and updating dropdown options works great with this method. However, now I need to add an Active checkbox to the cell in the second table, in the third row, in the second column. I need it to check if the Active checkbox has already been added and only add it if not. I also need it to fill in any value that may be present in the External ID field. The bookmark for the External ID field is "External_ID"

    Here's a screenshot of what the impacted section currently looks like.
    CurrentState.jpg
    This screenshot is what I need it to look like.
    FutureState.jpg

    I keep running into an issue at the FormFields.Add Range. I just can't get it to add it in the right spot. I also don't know how to get it to only add the Active and the checkbox if the check box is missing.
    Please Help.

    Sub AddCheckbox()
    Dim ExtID As String, ExtChk As Integer
    '
    '
    '
    ExtID = ActiveDocument.FormFields("External_ID").Result
    ExtChk = ActiveDocument.FormFields("ExtID_Checkbox").Result
    
    
    ActiveDocument.Unprotect
    
    
    
    
    With ActiveDocument.Tables(2).Cell(Row:=3, Column:=2).Range
        .Delete
        .InsertAfter Text:="External ID: "
        .MoveEnd Unit:=wdCell, Count:=1
        With ActiveDocument.FormFields.Add(Selection.Active, wdFieldFormTextInput)
            .Name = "External_ID"
            .Result = ExtID
        With ActiveDocument.FormFields.Add(Selection.Range, wdFieldFormCheckBox)
            .Name = "ExtID_Checkbox"
        End With
    End With
    
    
    ActiveDocument.Protect wdAllowOnlyFormFields, True
    
    
    
    
    End Sub
    Cheers,
    Thyme2Cook

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You seem to be making this a whole lot harder than it needs to be:
    Sub AddCheckbox()
    Dim Rng As Range, FmFld As FormField
    With ActiveDocument
      .Unprotect
      If .Bookmarks.Exists("ExtID_Checkbox") = False Then
        Set Rng = .Bookmarks("External_ID").Range
        With Rng
          .Collapse wdCollapseEnd
          .Text = " Active: "
          .Collapse wdCollapseEnd
          Set FmFld = .FormFields.Add(.Duplicate, wdFieldFormCheckBox)
          FmFld.Name = "ExtID_Checkbox"
        End With
      End If
      .Protect wdAllowOnlyFormFields, True
    End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Or another way

    Sub Macro1()
    Dim oRng As Range
    Dim oFf As FormField
    Dim bProtected As Boolean
        'Unprotect the file
        If Not ActiveDocument.ProtectionType = wdNoProtection Then
            bProtected = True
            ActiveDocument.Unprotect Password:=""
        End If
        Set oRng = ActiveDocument.Tables(2).Cell(2, 2).Range
        oRng.End = oRng.End - 1
        If oRng.FormFields.Count = 1 Then
            oRng.Collapse 0
            oRng.Text = " Active "
            oRng.Collapse 0
            oRng.FormFields.Add oRng, wdFieldFormCheckBox
        End If
        'Reprotect the document.
        If bProtected = True Then
            ActiveDocument.Protect _
                    Type:=wdAllowOnlyFormFields, _
                    NoReset:=True, _
                    Password:=""
        End If
        Set oRng = Nothing
        Set oFf = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Paul,
    This worked marvelously and I understand it. However, in testing, I ran into an issue where some older documents, that were created from earlier versions of the template do not recognize the "External_ID" as a bookmark. The issue went away simply by opening the form field properties box and visually checked that the correct bookmark name was present (it was) and closing without changing anything. I'm concerned that there could be more documents with that same issue. Any ideas as to how to mitigate such an issue if it does arise again?

    This is the code that I put into place. I added a check for un-named form fields because they are managed and referenced elsewhere in the code (this is a snippet of the whole code). Since I am adding a new form field the index number for all those fields will change. Do you have any thought on what I have below?

    Sub AddCheckbox()
    Dim Rng As Range, FmFld As FormField
    
    
    ' Checks for un-named form fields and renames them if they are only referenced by the index number.
    With ActiveDocument
        If .FormFields.Count = 90 Then
            If .Bookmarks.Exists("POI") = False Then
                .FormFields(42).Name = "POI"
            End If
            If .Bookmarks.Exists("State") = False Then
                .FormFields(43).Name = "State"
            End If
            If .Bookmarks.Exists("RSVPReferral") = False Then
                .FormFields(44).Name = "Referral"
            End If
        End If
        .FormFields("POI").Enabled = True
        .FormFields("Referral").Enabled = True
        If .Bookmarks.Exists("Dropdown1") = True Then
            .FormFields("Dropdown1").Name = "AddressType"
        End If
        If .Bookmarks.Exists("Dropdown4") = True Then
            .FormFields("Dropdown4").Name = "TC_Name"
        End If
        .FormFields("Record_Date").TextInput.EditType wdCurrentTimeText, , "M/dd/yyyy h:mm am/pm", False
    End With
    
    
    ' Checks for the existence of Active checkbox and adds when necessary.
    With ActiveDocument
      .Unprotect
      With .Tables(2).Cell(3, 2)
        .Range.Bold = False
        .SetWidth ColumnWidth:=InchesToPoints(2.3), RulerStyle:=wdAdjustFirstColumn
      End With
      If .Bookmarks.Exists("ExtID_Checkbox") = False Then
        Set Rng = .Bookmarks("External_ID").Range
        With Rng
          .Collapse wdCollapseEnd
          .Text = " Active: "
          .Collapse wdCollapseEnd
          Set FmFld = .FormFields.Add(.Duplicate, wdFieldFormCheckBox)
          FmFld.Name = "ExtID_Checkbox"
        End With
        With .FormFields("ExtID_Checkbox").CheckBox
            .AutoSize = False
            .Size = 8
        End With
      End If
    End With
    
    
    End Sub
    Thanks,
    Thyme2Cook

  5. #5
    Graham,
    If I understand your solution correctly it is looking at that table cell and counting the number of form fields. If it is 1 then it will add "Active" and the checkbox. If you see my response to macropod's solution you can see that Word is occasionally not recognizing the External ID bookmark in earlier iterations of the document. I think your solution may address that issue nicely.

    Thanks,
    Thyme2Cook

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    There is a difference between Word not recognising a bookmark and the bookmark not being present. I cannot see how it is possible for the macro to not recognise the 'External_ID' bookmark if it's present. If the code isn't adding the checkbox to an existing document, the strong implication is that the 'External_ID' is missing. Do note that 'External_ID' is not the same as 'External-ID' or 'ExternalID'.

    It's also possible the code isn't working on some document because they're already unprotected. In a protected document, all your code before:
    .Unprotect
    would fail in a protected document, while the same code would fail at that line in an unprotected document.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    I found out that some of the Form Fields do not have a bookmark and some have the same bookmark. I was able to pull a listing of the form fields with their index number to accurately identify which ones need addressing and put logic to account for the new checkbox.

    Thanks for your help. You're a lifesaver.
    Thyme2Cook

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •