Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #11  
Old 10-12-2018, 06:56 AM
gmaxey gmaxey is offline Auto-populating a document pulling data from external doc using a Userform with Checkboxes Windows 7 32bit Auto-populating a document pulling data from external doc using a Userform with Checkboxes Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
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
 

Tags
vba, word-vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Userform with Multiple Cascading Drop Down Lists Populated with External Source Data venganewt Word VBA 21 05-16-2018 02:05 PM
Auto Populating Data with Drop Down Menu vmangano Excel 4 12-20-2016 09:03 AM
Auto-populating a document pulling data from external doc using a Userform with Checkboxes UserForm Dropdown List Not Populating highrise955 Word VBA 14 04-10-2016 12:10 PM
Lack of email addresses auto-populating LarryK Outlook 0 10-10-2012 08:09 AM
Auto-populating a document pulling data from external doc using a Userform with Checkboxes "Auto-populating" data-worksheet to worksheet. meggenm Excel 4 02-04-2012 02:04 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft