View Single Post
 
Old 12-19-2018, 09:43 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your use of terminology is confusing. It seems that you are inserting files into a document that have common bookmark names. Bookmark names must be unique so if you insert a second document with the same bookmarks as the first, something has to give. The only way to do this is to delete the bookmarks in the target document before inserting the next document.

It would be better to use content controls than bookmarks as these can have duplicated names, then loop through each content control with the same name and apply the values The following will loop through all the controls in the document body (for controls in other story ranges you will have to loop through those also):
Code:
Dim OCC As ContentControl
    For Each OCC In ActiveDocument.ContentControls
        Select Case OCC.Title
            Case "Address"
                OCC.Range.Text = TextBox1.Text
            Case "Location"
                OCC.Range.Text = TextBox2.Text
            Case "HT_Type"
                OCC.Range.Text = TextBox3.Text
            Case "Time"
                OCC.Range.Text = TextBox4.value
            Case "Weather"
                OCC.Range.Text = TextBox5.Text
            Case "TestDate"
                OCC.Range.Text = TextBox6.value
        End Select
    Next OCC
     Set OCC = Nothing
You may find http://www.gmayor.com/insert_content_control_addin.htm useful for inserting and maintaining the controls.
Or you could use the following macro to convert your bookmarks to controls (with the same proviso about story ranges - I may add this to the aforementioned add-in later)
Code:
Sub ReplaceBookmarkWithCC()
'Graham Mayor - https://www.gmayor.com - Last updated - 20 Dec 2018
Dim oBM As Bookmark
Dim oRng As Range
Dim oCC As ContentControl
Dim strTitle As String
    For Each oBM In ActiveDocument.Bookmarks
        strTitle = oBM.Name
        Set oRng = oBM.Range
        oBM.Delete
        Set oCC = oRng.ContentControls.Add(wdContentControlText)
        With oCC
            .Range.Text = oRng.Text
            .Tag = strTitle
            .Title = .Tag
            .MultiLine = True
            .SetPlaceholderText , , "Click or tap here to enter text."
            .LockContentControl = True
        End With
    Next oBM
    MsgBox "Replacements complete"
lbl_Exit:
    Set oCC = Nothing
    Set oRng = Nothing
    Set oBM = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote