PDA

View Full Version : Copy Text from Bookmark in SourceDoc to use as Document Variable in NewDoc



j_b_c
02-02-2022, 02:57 PM
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>

j_b_c
02-02-2022, 02:59 PM
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

gmayor
02-02-2022, 10:49 PM
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

Chas Kenyon
02-03-2022, 10:27 AM
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.

j_b_c
02-03-2022, 02:46 PM
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?

gmayor
02-03-2022, 10:28 PM
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