View Single Post
 
Old 10-12-2018, 06:56 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You can save yourself a lot of code if you use a common naming convention for the form checkboxes, source and target bookmarks:

Code:
Private Sub CommandButton1_Click()
Dim oCtr As Control
Dim oDocSrc As Document
Dim oDocTarget As Document
Dim strBMName As String
Dim oBM As Bookmark
Dim oRng As Range
  'Assumes this form is in the target document.
  Set oDocTarget = ActiveDocument
  'Assumes source document is in the same folder and named Doc A.docm
  Set oDocSrc = Documents.Open(oDocTarget.Path & "\Doc A.docm", , , , , , , , , , , False)
  'Assumes that the associated checkbox, source and target bookmarks have a common name e.g.,
     'checkbox - chkClientAddress, Source bookmark - ClientAddress, Target bookmark - ClientAddress
  For Each oCtr In Me.Controls
    If TypeName(oCtr) = "CheckBox" Then
      strBMName = Mid(oCtr.Name, 4, Len(oCtr.Name) - 3)
      Set oBM = oDocTarget.Bookmarks(strBMName)
      If oCtr = True Then
        Set oRng = oBM.Range
        oRng.Text = oDocSrc.Bookmarks(strBMName).Range.Text
        oDocTarget.Bookmarks.Add strBMName, oRng
      Else
        oRng.Text = vbNullString
        oRng.Paragraphs(1).Range.Delete
      End If
    End If
  Next
End Sub
As stated earlier. If you actually delete the empty paragraph associated with the empty bookmarks you will delete the bookmark.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote