Like someone given a fine 35mm camera who doesn’t know what a film advance does and therefore can’t make the machine work, I return yet again after another couple of hours of trying to make your undoubtedly perfect your code work, stumbling around without being able to find answers to my searches on VBA syntax. Perhaps I just named things in a bad way that increases my confusion. So, if I may, here is a new attempt with the same example, using a naming system that could work for my project as it expands.
As before, all docs are on my desktop in a folder called Form. I have named the target document Target, the source document Source1, and the two bookmarks in Source1 -- standing, respectively, for Source1 Bookmark1 True and False -- are S1B1T and S1B1F, the latter an empty paragraph to correspond to an unchecked check box.
You said “a macro” is needed for checkboxes, so I assume you refer to Graham Mayor’s at the site I linked. Check1 presumably refers to check box 1 in Target. His macro appears to automatically create a bookmark called Check1, which baffles me, because it seems this would be what I create as the bookmark enclosing the includetext field on page two. I have tried to adapt his example to my situation. It seems to me that the sTrue and sFalse lines need to include the names of the two bookmarks (he uses two separate documents), but I don’t know how to properly insert them (somewhere within the triple quotation marks?). I can only guess that rText (return text?) refers to my includetext field, but I’m lost with the code.
Code:
Sub OnExitCB1()
Dim rText As Range
Dim sTrue As String
Dim sFalse As String
Dim oFld As FormFields
Set oFld = ActiveDocument.FormFields
sTrue = """C:\\Desktop\\Form\\Source1.docx"""
sFalse = """C:\\Desktop\\Form\\Source1.docx"""
Set rText = ActiveDocument.Bookmarks("Check1Result").Range
If oFld("Check1").CheckBox.Value = True Then
rText , wdFieldIncludeText, sTrue, False
rText.MoveEnd wdCharacter, 1
Else
rText.Fields.Add rText, wdFieldIncludeText, sFalse, False
rText.MoveEnd wdCharacter, 1
End If
With ActiveDocument
.Bookmarks.Add "Check1Result", rText
.Fields.Update
End With
End Sub
Then, looking at the code you provided,
Code:
{INCLUDETEXT "{FILENAME \p }/../Text1.docx" "Check1{IF{REF Check1}= TRUE 2 1}"}
I have for clarity changed the first instance of Check1 to Field1, assuming that this refers to the bookmark I assign to the entire field, so that the status of the check box brings in either S1B1T or S1B1F. I don’t understand “TRUE 2 1”. TRUE means, I guess, that the check box is checked;I don’t understand what the numbers do.
I would think that, for starters, it should be, in my case,
Code:
{INCLUDETEXT "{FILENAME \p }/../Source1.docx" "Field1{IF{REF Check1}= TRUE 2 1}"}
but I don’t see how it would choose one of the two bookmarks in the source document.
In case I'm proceeding wrong: After creating Source, my procedure in Target has been to create the include text field and get that to work (at least, bringing in the entirety of Source1). Then I try to modify the field, successively including the additional sub-fields and typed text. This is where I start getting "bookmark not defined" errors. With Mayor's macro already installed, then I try inserting the legacy checkbox, following Mayor's settings.
I am determined to make this work, and repeat my thanks for all your help.