Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Document Assembly in Word 2

Status
Not open for further replies.

CantDo

Technical User
Jun 5, 2005
48
0
0
CA
I have a Word 2003 SP2 template document that has boilerplate language. I have coded popup Message Boxes to gather information from the user and, dependent on that information, I need to choose from alternate clauses to insert into the template.

I have set bookmarked fields to act as placeholders for the alternative text that might result from the user input.

Currently, the alternative text is located in one external file called "VariableData.doc", each marked with its own bookmarked fieldnames.

I can't for the life of me figure out how to bring the applicable external bookmarked field text into the target template document. I've tried InsertFile, GetText, etc (all to no avail).

An example may help. For example, if the user states that the document is to be signed by a corporation, a particular signature block is to be inserted; however if it is a partnership, a different signature block is to be inserted.

Can anyone help with this? Any help is most welcome at this point! Thank you in advance.
 
Oh boy.

There are a multitude of options/routes here. Going with what you have - and there could a variety of ways to do this differently - here is an example.
Code:
   Selection.Fields.Add Range:=Selection.Range, _
      Type:=wdFieldEmpty, _
      Text:="INCLUDETEXT ""c:\\myfiles\\myvaliddata.doc"" ""yadda1""", _
      PreserveFormatting:=True
VERY important. You can NOT use the underscore character to break up the line of the text string, or at least I think not. macropod will correct me if I am wrong. Adding & _ will add THAT to the INCLUDETEXT which will fail.

Also notice the added path separators in the path.

The file MyValidData.Doc has a bookmark named "yadda1".

The document that runs this will INCLUDETEXT with the contents of that bookmark. Which is, I believe, what you are asking for.

You just have to be very careful with your string literals. I am not sure about multiple "popup" message boxes, but whatever.

In any case, get the string value from the user (by whatever method you are using), and write it into the text for an INCLUDETEXT field.

faq219-2884

Gerry
My paintings and sculpture
 
fumei:

Thank you very much and Happy New Year! This approach works fine and accomplishes what I need. However, it has led to another problem (why is that always the case with programming?) ;)

In order to use the code you suggested, I needed to first unprotect and then protect the document. This is necessary as I don't want any users to be able to change any underlying text in the document, but to be able to fill in form fields as required.

However, now all the form field information entered 'resets' or updates due to the unprotect/reprotect code. Is there a solution for this? I looked at the 'locked' property for fields, but can't seem to make it work. I have set out a part of some of my (modified) code below as an example of what I'm now trying.

Code:
ActiveDocument.Fields.Locked = True

ActiveDocument.Bookmarks("MyMark").Select
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect
    End If

ActiveDocument.Fields.Locked = False

vAnswer = MsgBox(Prompt:="Do you want to include text A?  If not, Text B will be included", Buttons:=vbYesNo)

    If vAnswer = 6 Then
    Selection.Fields.Add Range:=Selection.Range, _
         Type:=wdFieldEmpty, _
         Text:="INCLUDETEXT ""c:\\myfiles\\myvaliddata.doc"" ""yadda1""", _
         PreserveFormatting:=True
    Else
    Selection.Fields.Add Range:=Selection.Range, _
         Type:=wdFieldEmpty, _
         Text:="INCLUDETEXT c:\\myfiles\\myvaliddata.doc"" ""yadda2""", _
         PreserveFormatting:=True
    End If
    
ActiveDocument.Fields.Locked = True
    
    If ActiveDocument.ProtectionType = wdNoProtection Then
        ActiveDocument.Protect Type:=wdAllowOnlyFormFields
    End If

ActiveDocument.Fields.Locked = False

Thanks, once again, in advance for any help!

 
Use NoReSet:=True with your reprotection instruction.
Code:
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, [b]NoReset:=True[/b]


faq219-2884

Gerry
My paintings and sculpture
 
Gerry,

Thank you once again! Works like a charm!

I tried giving you another star, but I don't think the system lets me do that more than once per user for each thread. Sorry. You deserve two.
 



...leaving no tern unstoned, for one good tern deserved another...

==> *

Skip,
[sub]
[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top