Results 1 to 6 of 6

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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; 03-24-2025 at 05:36 AM. Reason: Added code tags to supplied code

Posting Permissions

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