![]() |
#1
|
|||
|
|||
![]()
I am having problems with a macro from Greg Maxey's site. I can't get the text to come into the document. The requirement is simple. I'm creating a document which will have a number of items listed, each with a check box. The idea is that when a checkbox is checked, the exit macro grabs text from a corresponding document and inserts it at a book mark on a subsequent page.
I created two word docs, Text1 and Text2 on the desktop. Each has just a paragraph or two in it. I copied Greg's macro into my normal template,and replaced his document location and names with mine. I created a trial check box, created a bookmark on the next page, and restricted editing to filling out forms, according, I thought, to his directions on the site page I have now spent 30 minutes looking for and cannot find; it's not in my browser history and no search turns it up. Anyway, the text is not being brought in. I am attaching the trial document and the code is below. I have with heavy irony, inserted the name Genius instead of the real user name in the path. I hope someone can quickly spot my error. 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:\\Users\\Genius\\Desktop\\Text1.docx""" sFalse = """C:\\Users\\Genius\\Desktop\\Text2.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 |
#2
|
||||
|
||||
![]()
Personally, I'd have the INCLUDETEXT fields in the document already, and use a DOCPROPERTY field to return a value from a custom document property to determine the output. If the source documents have two bookmarked ranges - one containing the TRUE content and the other containing the FALSE content - all that is needed is to toggle the bookmark reference. For example:
{INCLUDETEXT "C:\\Users\\Genius\\Desktop\\Text1.docx" "{DOCPROPERTY BkMk}"} Alternatively, if you want two separate documents, use the DOCPROPERTY field to toggle the 1/2 in the document names. For example: {INCLUDETEXT "C:\\Users\\Genius\\Desktop\\Text{DOCPROPERTY DocID}.docx"} Either way, you don't need to rely on updating bookmarked ranges in your form document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Gentlemen, thank you. I am getting nowhere, through no fault of yours, of course. I am so far unable simply to get the Includetext field to do anything but return an "invalid file name" message. So far, my research on the field syntax is completely unproductive, so I'll probably have to wait till the weekend.
Just to explain briefly, I tried the code Paul indicated, with the full path name, and with various attempts, based on that and MS support pages, to include or not include a bookmark in various ways. At present, I have, on my desktop, a folder, "Form", in which are my "form" document and Text1.docx, the latter with two bookmarks, Check1 and Check 2, the first a paragraph, the second an empty paragraph, for the unchecked box return as I think I understood your suggestion. The MS page, perhaps outdated, indicates no need for a full document path in the include text field, but full or not, as I said, and with as much careful scrutiny of this unfamiliar syntax as I can give, no luck. I'll get back to you once I have done some more homework. Best, Philip |
#4
|
||||
|
||||
![]()
If you're referring to either of these:
{INCLUDETEXT "C:\\Users\\Genius\\Desktop\\Text1.docx" "{DOCPROPERTY BkMk}"} {INCLUDETEXT "C:\\Users\\Genius\\Desktop\\Text{DOCPROPERTY DocID}.docx"} then: 1. The field brace pairs (ie '{ }') are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from a post. 2. Your document would need the Custom Document Property named BkMk or DocID, as appropriate, to exist and have a valid value.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Thank you. I do realize that field braces cannot be typed. For the basic field, I tried just
{INCLUDETEXT "C:\\Users\\Genius\\Desktop\\Text1.docx"} as well as {INCLUDETEXT "C:\\Desktop\\Text1.docx"} with the same failure result. I would also note that in some instructions, I see the bookmark in the source doc enclosed in regular, not curly braces. These are aspects of syntax that I think I need to research, rather than simply troubling you with a linear series of questions; I need to get a better overall grasp of fields first. Cordially, Philip |
#6
|
||||
|
||||
![]()
Well, that has nothing to do with the approach I suggested, but with the fact your INCLUDETEXT field points to a path and/or document that doesn't exist. The string
Code:
"C:\\Users\\Genius\\Desktop\\Text1.docx" Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
Gents,
I am back again, having failed to find any tutorials addressing my issue, and being unable to understand your instructions, due to my lack of knowledge of fields. There must be somewhere to learn about this, but I have been unable in hours of searching to find it. I meant to check the local (real) library tonight for a copy of a recent Office Bible, but didn't make it. Again, if you have links to the information I need, I am glad to do my own homework. That said, I am attaching a source and a target document, the latter with notes about its content. The source is "Text1", the target is "Form page". I explain their location on my machine, below. I haven’t even gotten to trying the checkbox, as explained in my original post, again. I am simply trying to get an INCLUDETEXT field to work with a bookmark. I have not found a single source among pages of searches that explains how the bookmark is to be included. The source and target docs reside in the same folder on my desktop. By using Word's Insert/QuickParts/Field, I finally got a field that would bring in the text from the source. All the text. However, as you'll see in the source doc, I have created two bookmarks: Check1 comprising the first two paragraphs, Check2 the empty paragraph which follows. The final paragraph is not bookmarked. The Check1 and 2 would presumably correspond to a checked and unchecked legacy form tools check box, respectively — when I get that far. Meanwhile, I cannot find a way to include the name Check1 in the INCLUDETEXT field and get it to work, i.e., to bring in only the bookmarked text. At one point, I managed to include it and the field worked, rahter than returning an "invalid filename" message, but it ignored the bookmark and simply brought in all the text from the source as if it had not been there at all. I have been simply typing the name Check1 in the field, for instance: Code:
{ INCLUDETEXT "Text1.docx" Check1 } I apologize for belaboring this. It is perhaps an attempt to say, I'm not a complete incompetent at Word, and this surely is not a complex issue, but it surely has me baffled! Cordially, Philip |
#8
|
||||
|
||||
![]()
Here's a copy of your 'Form page' document back, with:
1. a custom document property named 'CheckNo'; and 2. the INCLUDETEXT field with a: (a) FILENAME field being used to generate a relative path to the Test1.docx file. Simply omitting the path is not reliable; (b) bookmark reference to your Test1.docx file's Test bookmarks; and (c) DOCPROPERTY field referencing the 'CheckNo' custom document property to vary the bookmark number. Currently it refers to Test bookmark #1. Simply save the file to the same folder as your Test1.docx file and everything should work fine. If you change the 'CheckNo' custom document property value from 1 to 2 and update the INCLUDETEXT field (e.g. via Alt-A, F9), the document content will change to reflect whatever is in the Test2 bookmark. If you edit the content of either bookmark and update the INCLUDETEXT field the reference will also update. To change the 'CheckNo' custom document property value, go to File>Properties>Advanced properties>Custom.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]()
Thank you very much, Paul. I have studied the complex field you returned, found the advanced document properties, and have successfully changed the value of the custom property as well as the source document; that is, I know how now. I have learned a good deal in the process and I am very grateful! But still stuck.
What I am now stumped on is how to use this with a checkbox. I am attaching a copy of the document you provided, with a better indication of what I would like to end up with, in case I have not communicated this aspect clearly. A corresponding source doc (Text1) is also attached. I have cast it in terms of computer help desk sorts of issues, with two examples and the rest left as generic labels. I have set this up in a table in the target doc; I believe tables cause no problems in this regard but can lay out the page otherwise. The idea is simply that on page one, I have a list of issues I recognize by a label. The source document contains the solutions to every issue, each one separately bookmarked. Let's say, for example, that I have a client who needs solutions for issues 1,4,7, and 13. I open my target doc, and as I check the relevant check boxes, those selected solutions are brought in on page two, which I then print out and hand to my client. That's the principle. I remain confused about how this would work in your plan. In your early response above, you said Quote:
The Graham Mayor page I had lost, that goes through his solution, is here: http://www.gmayor.com/selectfile.htm About halfway down the page, where it says INCLUDETEXT field. His uses a macro, which he provides, on exit from checking a box to insert the desired text at a bookmark. Presumably the bookmark stays empty until a box is checked. Though I haven't gotten there yet, I would think that then I could create macro to delete any empty paragraphs or something like that, to avoid extra space between solution paragraphs for the printed copy. Paul, I'm not trying to suggest reversion to an approach you have replaced; I just don't grasp the connections here. I had no idea this would require so much time. Cordially, Philip |
#10
|
||||
|
||||
![]()
If you're using checkboxes, you don't need either custom document properties or DOCPROPERTY fields, though they can still be used. For example, instead of using either custom document properties or DOCPROPERTY fields, you could use
{INCLUDETEXT "{FILENAME \p }/../Text1.docx" "Check{IF{REF Check1}= TRUE 2 1}"} Using checkboxes does required a macro but, given you document is using formfields, you could use dropdown formfields for the same purpose and that would not require a macro. Quote:
{INCLUDETEXT "{FILENAME \p }/../Text1.docx" "Check1{IF{REF Check1}= TRUE 2 1}"} {INCLUDETEXT "{FILENAME \p }/../Text1.docx" "Check2{IF{REF Check2}= TRUE 2 1}"} {INCLUDETEXT "{FILENAME \p }/../Text1.docx" "Check3{IF{REF Check3}= TRUE 2 1}"} (Note the additional number in the bookmarks to cater for the additional checkboxes; you could just as easily use letters or something else - you could even replace the outer 'Check1', 'Check2', etc with meaningful names). The important thing to remember is that you need two bookmarks per checkbox - one for whatever is to be output for the True value and one for the False value, even if one of those is empty. You can even add more logic for what happens if two particular checkbox values are true, for example. [quote]I can find "toggle field codes" but not "toggle bookmark references". The toggling referred to consisted of changing the value between 1 & 2 (though you could use 0 & 1 to 'equate' to False & True); this has nothing to do with toggling the field code display. Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
![]()
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 Code:
{INCLUDETEXT "{FILENAME \p }/../Text1.docx" "Check1{IF{REF Check1}= TRUE 2 1}"} 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}"} 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. |
#12
|
||||
|
||||
![]()
See attached. I've reverted to using the custom document property in conjunction with the checkbox, as that's simpler to implement with the required macro. As you'll see, the macro is pretty simple; it's also easily converted to use with a checkbox content control, for example.
Simply extract both files to a common folder.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
![]()
Paul, thank you. I can imagine you are tired of hearing about this one, and only wish I could give something in return.
I see that your Update macro is specific to the Form page.docm, that is, it is in a different module from the rest of my macros. It is not running; that is, whether I check the check box or uncheck it, nothing appears to happen; no text from Text1 comes in, nothing moves. If I unprotect the document temporarily, I can run the macros I have in my normal.dotm. Could there be some setting I have that's preventing yours from running? Cordially, Philip |
#14
|
||||
|
||||
![]()
The running of the Update macro is controlled by the Checkbox - it's properties show the macro is assigned to the formfield's Exit event.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
![]()
Thank you again.
After a half hour of trial and error, I discovered that the way to have the bookmarked text from Text1 come in when the checkbox is checked, rather than unchecked, was to remove from the macro the " + 1" at the end of the line Code:
.CustomDocumentProperties("CheckNo").Value = .FormFields("Check1").Result + 1 Error! Bookmark not defined. when the box is unchecked, but I can live with that. I had just thought that it would return nothing, i.e., the empty paragraph of the second bookmark. I realize and respect that code is as plain as English to you, but since the mechanics of all this still remain obscure to me, an hour of attempts has failed in creating a first additional check box and includetext field to add additional bookmarked text from the source doc. I simply don’t know what to change, though I thought I had grasped the process. I have, strangely, been able to repeat exactly what the field you created does, with the same text brought in, even though I created a CheckNo2 custom document property, and two more bookmarks with another paragraph and another empty one. I tried duplicating the Update macro as Update2 and changing the line shown above to Code:
.CustomDocumentProperties("CheckNo2").Value = .FormFields("Check2").Result If I may ask you to once again correct my error, perhaps I can see where the difference lies and use that for all the additional check boxes and future documents I need to create. Best, Philip |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
mortcr | Word VBA | 1 | 10-22-2013 02:02 PM |
![]() |
DBrooks | Word | 1 | 02-16-2013 01:32 PM |
![]() |
David C | Word | 1 | 10-24-2012 04:53 AM |
![]() |
kellyjb27 | Word | 1 | 05-26-2012 11:08 AM |
![]() |
LAssist2011 | Word | 5 | 12-14-2011 03:02 PM |