Consulting

Results 1 to 6 of 6

Thread: Copy Text from Bookmark in SourceDoc to use as Document Variable in NewDoc

  1. #1
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    3
    Location

    Copy Text from Bookmark in SourceDoc to use as Document Variable in NewDoc

    Hi,<br><br>I would like to have a Source Document set up with various bookmarks with text inside the bookmarks.&nbsp; I would then like to have different Templates with DocVariable fields throughout.&nbsp; When I create a new document from the template, I would like to run a macro that will copy the text from a particular bookmark in the SourceDoc and set it as the value for a particular DocVariable in the NewDoc.<br><br>This is what I have come up with, but I cannot get it to work for me, any help would be much appreciated.<br><br>Private Sub Document_New()<br><div><br>&nbsp; &nbsp; Dim oSourceDoc As Document</div><div>&nbsp; &nbsp; Dim oRng As Range</div><div><br>&nbsp; &nbsp; &nbsp; &nbsp; Set oSourceDoc = Documents.Open("SourceDoc")</div><div>&nbsp; &nbsp; &nbsp; &nbsp; oRng.Text = oSourceDoc.Bookmarks("bookmark").Range.Text</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Set oVars = ActiveDocument.Variables</div><div>&nbsp; &nbsp; &nbsp; &nbsp; oVars("doc_variable").Value = oRng.text</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Set oVars = Nothing</div><div><br>End Sub</div>

  2. #2
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    3
    Location
    Not sure why it posted like that, code I tried should be:

    Private Sub Document_New()
    Dim oSourceDoc As Document
        Dim oRng As Range
    Set oSourceDoc = Documents.Open("SourceDoc")
            oRng.Text = oSourceDoc.Bookmarks("bookmark").Range.Text
    Set oVars = ActiveDocument.Variables
    oVars("doc_variable").Value = "test"
    Set oVars = Nothing
    End Sub

  3. #3
    The code should go in the template you are creating the modified documents from. Personally I would populate content controls rather than docvariables as they are more robust. https://www.gmayor.com/insert_content_control_addin.htm may be useful in that regard. The following code will populate docvariables, and if the matching content control titles exist it will populate those also. Change the various variable and content control names and the document path as appropriate.
    Private Sub Document_New()
    Dim oSourceDoc As Document
    Dim oTarget As Document
    Dim sPath As String
    Dim oBM As Bookmark
    Dim oCC As ContentControl
    Dim oVars As Variables
    Dim sText As String
    
        sPath = "C:\My path\Sourcedoc name.docx"
        Set oTarget = ActiveDocument
        Set oSourceDoc = Documents.Open(sPath)
        Set oVars = oTarget.Variables
        For Each oBM In oSourceDoc.Bookmarks
            sText = oBM.Range.Text
            Select Case oBM.Name
                Case "BookmarkName_1"
                    oVars("doc_variable1").value = sText
                    For Each oCC In oTarget.ContentControls
                        If oCC.Title = "CCTitle 1" Then oCC.Range.Text = sText
                    Next oCC
                Case "BookmarkName_2"
                    oVars("doc_variable2").value = sText
                    For Each oCC In oTarget.ContentControls
                        If oCC.Title = "CCTitle 2" Then oCC.Range.Text = sText
                    Next oCC
                'etc
            End Select
        Next oBM
        oSourceDoc.Close 0
        Set oSourceDoc = Nothing
        Set oTarget = Nothing
        Set oCC = Nothing
        Set oVars = 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
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    While a Rich Text Content Control can contain any content that you would find in a bookmark, a document variable can only contain text and is, I believe, limited to 256 characters.

    Also, consider storing as AutoText or another kind of Building Block.

  5. #5
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    3
    Location
    Thank you so much Graham, that worked perfectly. I am looking at whether to switch from DocVariables to Content Controls in the Target, I did already change the bookmarks in the source document to content controls (much better) and changed the code to this as a result:

    Private Sub Document_New()
    Dim oSourceDoc As Document
    Dim oTarget As Document
    Dim sPath As String
    Dim oBM As Bookmark
    Dim oSourceCC As ContentControl
    Dim oTargetCC As ContentControl
    Dim oVars As Variables
    Dim sText As String
    
    
        sPath = "C:\My path\Sourcedoc name.docx"
        Set oTarget = ActiveDocument
        Set oSourceDoc = Documents.Open(sPath)
        Set oVars = oTarget.Variables
        For Each oSourceCC In oSourceDoc.ContentControls
            sText = oSourceCC.Range.Text
            Select Case oSourceCC.Title
                Case "sourceCCTitle 1"
                    oVars("doc_variable_1").Value = sText
                    For Each oTargetCC In oTarget.ContentControls
                        If oTargetCC.Title = "targetCCTitle 1" Then oTargetCC.Range.Text = sText
                    Next oTargetCC
                Case "sourceCCTitle 2"
                    oVars("doc_variable_2").Value = sText
                    For Each oTargetCC In oTarget.ContentControls
                        If oTargetCC.Title = "targetCCTitle 2" Then oTargetCC.Range.Text = sText
                    Next oTargetCC
                'etc
            End Select
        Next oSourceCC
        oSourceDoc.Close 0
        Set oSourceDoc = Nothing
        Set oTarget = Nothing
        Set oSourceCC = Nothing
        Set oTargetCC = Nothing
        Set oVars = Nothing
    End Sub
    If it helps anyone else viewing this, I also wanted to code in the path name for the source document, which I plan to have in the same folder as the template document I would be using to create the new document, ie, "C:\My path\client name\Source Document.docx" and "C:\My path\client name\Template.dotm".

    I added the following code which seems to work:

    Dim sTemplatePath As String

    sTemplatePath = ActiveDocument.AttachedTemplate.Path
    sPath = sTemplatePath & "\Source Document.docx"

    So if I create a new document from "C:\My path\Client 1\Template.dotm" it would give - sPath = "C:\My path\Client 1\Sourcedoc name.docx", and if I create from "C:\My path\Client 2\Template.dotm" it would give - sPath = "C:\My path\Client 2\Sourcedoc name.docx" etc.

    The final challenge, if it is possible, would be to have the code loop through the SourceDoc and find all the content controls and then set the values of the respective DocVariables and Content Controls in the Target, given that the Title of each Content Control in the SourceDoc will always be the exact same as the Name of the respective DocVariable or Title of the respective Content Control in the Target, rather than typing them each one out individually.

    Any idea if this could be done?
    Last edited by Aussiebear; 02-03-2022 at 03:40 PM. Reason: Added code tags to supplied code

  6. #6
    Based on your comments the following appears to do what you requested. The methodology you are employing however eludes me. Couldn't you simply create a new document using the source document as a template?

    Private Sub Document_New()
    Dim oSourceDoc As Document
    Dim oTarget As Document
    Dim sPath As String
    Dim oSourceCC As ContentControl
    Dim oTargetCC As ContentControl
    Dim oVars As Variables
    Dim sText As String
    
        sPath = "C:\My path\Sourcedoc name.docx"
        Set oTarget = ActiveDocument
        Set oSourceDoc = Documents.Open(sPath)
        Set oVars = oTarget.Variables
        For Each oSourceCC In oSourceDoc.ContentControls
            sText = oSourceCC.Range.Text
            oVars(oSourceCC.Title).value = sText
            For Each oTargetCC In oTarget.ContentControls
                If oTargetCC.Title = oSourceCC.Title Then oTargetCC.Range.Text = sText
            Next oTargetCC
        Next oSourceCC
        oSourceDoc.Close 0
        Set oSourceDoc = Nothing
        Set oTarget = Nothing
        Set oSourceCC = Nothing
        Set oTargetCC = Nothing
        Set oVars = 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

Posting Permissions

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