How to populate fields/form in word document from other word document? - ms-word

I have a Word document that has fields where information is populated by the user manually. There are couple of other documents with same fields and the user would like to populate those automatically, based on information in first document. How to achieve that? Documents are in .doc format.
I've tried looking around before coming here, but every site I've managed to find (that has title of "Filling fill-in forms in Word" and similar) actually describes how to prepare a document for such action, not how to insert the data in from other document. I'm maybe missing something very obvious, but can't figure it out.
All documents have "Allow only this type of editing in the document:" ticket and "Filling in forms" selected under "2. Editing restrictions" in Restrict Editing menu.

This is a borderline question, as it technically falls in the end-user area. But it could also be an interesting question for developers as there is no direct way to achieve what is required using the object model, except by following the steps below (i.e. inserting IncludeText fields).
In the source document, it's necessary to use content controls for text input (form field dropdowns work fine) and bookmark them. The content of a legacy form field textbox will not come through.
In Word, in the target document, go to Insert->Object->Text from file
in the dialog box click the Range button, type in the bookmark name
select the option to "Insert as Link"
This creates an IncludeText field in the document that references the file path to the source document and the bookmark content:
{ INCLUDETEXT "C:\\Test\\TestFormsProtection.docx" Text1 }
Note that there is no reliable (sometimes it might work, but other times not) way to use relative file paths with Word field codes.
The relevant VBA code to generate this field at the end of the active document:
Dim doc as Word.Document
Dim rng as Word.Range
Set doc = ActiveDocument
Set rng = rng.Content
rng.Collapse wdCollapseEnd
doc.Fields.Add rng, , "IncludeText " & Chr(34) & "C:\\Test\\TestFormsProtection.docx" & Chr(34) _
& " Text2", false

Related

Dynamic display text MS Word Mail Merge

Is there a way to make the Hyperlink field have dynamic display text as well as dynamic URL? So far, I have the following merge tag, which is correctly pulling the URL from my CSV data source
{ HYPERLINK "{ MERGEFIELD URL}"}
I hit Alt+F9 to toggle between field source and display preview - I see I can set static text as the URL's display text, but I need to use a merge code as the display test.
The hyperlink documentation just indicates static text, I don't see a way to add a merge field. A couple of place I've seen indicate you can insert a merge tag when editing he display text, but it doesn't save correctly (on Save, it just drops the hyperlink entirely).
By default, if you insert a mailmerge field into a hyperlink field, the hyperlinks will all show the first record’s address as the 'Text to display' text. Here's how you can do get a mailmerge to display your preferred default 'Text to display' text instead:
Disregarding mergefield issues for the moment, insert a hyperlink
into the document in the normal way, choosing whatever 'Click Here'
text you want in the 'Text to display' box.
Select the inserted hyperlink and press Shift-F9 to expose its field code.
Replace everything in the field after 'HYPERLINK' with your mergefield.
Select the field and press F9 to update the display.
In Word 2007 & later, you can make the display text variable also, by following these additional steps:
Position the cursor anywhere within the display text.
Insert a mergefield pointing to whatever data field you want to use for the display text (this could even be the same field as used at step 3 above).
Delete all of the previous display text either side of your last-inserted mergefield (note that this field will likely have updated already).
Execute the merge.
After merging to a new document, use Ctrl-A, F9 to update all fields. Without this, the mergefield hover text won’t update to the correct targets.
Note 1: The above is only for merged output sent to a new document; it does not work with merges to email or print. For merges to email, see: https://support.microsoft.com/en-us/kb/912679
Note 2: Hyperlink fields modified this way are liable to cease functioning once the merge has been executed. Accordingly, it's best to save mailmerge main document before doing the merge and not re-save it afterwards. If you need to make changes to the mailmerge main document, don't make/save them after doing a merge; make/save them beforehand.

Is There a Way to do a Spanish/English MailMerge?

I have an SSRS report that has Spanish and English text boxes. If the dataset row is a Spanish speaking person, an expression in each Spanish textbox shows that and hides the English textbox. These textboxes are exactly placed over each other.
My boss wants me to use SSRS to generate an Excel spreadsheet from the dataset(this is not hard) and use Word template for a mail merge. However, I am having trouble trying to figure out if I can hide all English when row is a Spanish row and vice versa. These are health clients of Spanish and English nationality.
I can do mail merges attached to a Recordset, I can do one in English, one in Spanish. I am trying to avoid this and have it all in one Mail Merge.
Areas marked in red will change to Spanish translation and/or date format. The dates are a no-brainer I can use a conditional IIF, however the formatted body I have no solution for, based on value in Field "CL_Language" which is either "Spanish" or "English".
====================================
The merge fields for dates and greeting are easy. There is no merge field for the text. And yes, only option might be for 2 separate reports with different Recordsets.
It's not clear what the actual issue is but...
Instead of hiding textboxes, which could cause problems when exporting etc., why not set a single textbox to the correct language text using an expression?
Something along the lines of
=IIF(Fields!Language.Value = "English", Fields!MyEnglishText.Value, FieldsMySpanishText.Value)
I found a solution. But it could be very difficult for the client to create. It involves hitting Ctrl + F9 which will create curly brackets {}.
Inside those curly brackets an IF statement is placed and I just pasted the whole Spanish formatted body in the true area, and the whole English body in the false area.
{IF "CL_Language" = "Spanish" "spanish body text here" "english body text here"}
Very strange syntax and you need to right click on the area to see choices like "Toggle Field Codes" (IF statement get's hidden), "Edit Field", and "Update Field". With Edit Field and Update Field you get a popup with the fields in your recordset.
If you saw the examples in my question, you can see that is some big clunky text AND . . .inside of it is a merge field that works! The Excel recordset comes already with the month name in correct language for each row.
Since it is not smart to include links that might expire, I am including the Google text I used to find this solution. Then I took a chance on a huge formatted chunk of text with a merge field inside of it.
Google this: "If Merge Field then"
Now is this a viable solution for the client versus just having a Word template for each language?
I think this is too difficult and I even duck when running it. Also, once it's working, if I look at the toggled code, the Conditional field no longer says the field name, but the value in the field, go figure.
{IF "Spanish" = "Spanish" or {IF "English" = "Spanish" instead of {IF "CL_Language" = "Spanish" or {IF "CL_Language" = "English"
Here is how to access the fields using right click. (remember, your curly brackets HAVE to be created with Control + F9).

Applescript for inserting hyperlink into MSWord comment

I'm trying to add a hyperlink object inside a Word comment.
To create a new comment in the active document I'm using this piece of script:
tell application "Microsoft Word"
set tempString to "lorem ipsum"
make new Word comment at selection with properties {comment text:tempString}
end tell
but now I'm not able to get a reference to the new created comment for use it with the command "make new hyperlink object".
Thanks for any suggestions.
Riccardo
I don't think you can work with the object returned by make new Word comment (at least not in this case), and you have to insert a unique, findable string then iterate through the comments:
tell application "Microsoft Word"
-- insert a unique string
set tempString to (ASCII character 127)
set theComments to the Word comments of the active document
repeat with theComment in theComments
if the content of the comment text of theComment = tempString then
set theRange to the comment text of theComment
-- you do not have to "set theHyperlink". "make new" is enough
set theHyperlink to make new hyperlink object at theRange with properties {text range:theRange, hyperlink address:"http://www.google.com", text to display:"HERE", screen tip:"click to search Google"}
insert text "You can search the web " at theRange
exit repeat
end if
end repeat
end tell
(edited to insert some text before the Hyperlink. If you want to insert text after the hyperlink, you can also use 'insert text "the text" at end of theRange.).
So for adding text, it was enough to use "the obvious" after all.
[
For anyone else finding this Answer. The basic problem with working with Word ranges in Applescript is that every attempt to redefine a range in the Comments story results in a range that is in the main document story. OK, I may not have tried every possible method, but e.g., collapsing the range, moving the start of range and so on cause that problem. In the past, I have noticed that with other story ranges as well, but have not investigated as far as this.
Also, I suspect that the reason why you cannot set a range to the Word comment that you just created is because the properties of the Comment specify a range object of some kind that I think is a temporary object that may be destroyed immediately after creation. SO trying to reference the object that you just created just doesn't work.
This part of the Answer is modified...
Finally, the only other way I found to populate a Comment with "rich content" was to insert the content in a document at a known place, then copy its formatted text to the comment, e.g. if the "known place is the selection, you can set the content of theComment via
set the formatted text of the comment text of theComment to the formatted text of the text object of the selection
If you are using a version of Word that supports VBA as well as Applescript, I don't really see any technical reason why you shouldn't invoke VBA to do some of these trickier things, even if you need the main code to be Applescript.
]
Finally I got a solution here:
https://discussions.apple.com/message/24628799#24628799
that allowed me to insert the hyperlink in reference with part of the comment text, with the following lines, if somebody in the future will search for the same:
tell application "Microsoft Word"
set wc to make new Word comment at end of document 1 with properties {comment text:"some text"}
set ct to comment text of wc
set lastChar to last character of ct
make new hyperlink object at end of document 1 with properties {hyperlink address:"http://www.example.com", text object:lastChar}
end tell

Add Variable Hyperlink in Mail Merge in Word 2013

I'm trying to add a hyperlink to a mail merge field. So something like this:
{HYPERLINK "{MERGEFIELD "Links" }"}
So I create a field like this.
{ MERGEFIELD Links }
And the above works (it displays different links for different recipients).
However, when I go to Edit Field, and then attempt to add HYPERLINK to { MERGEFIELD Links}, Word will not allow me. Meaning that when I type HYPERLINK followed by a space, the "variable" field is gone. Instead of word displaying...
LINK
it displays...
{HYPERLINK "{MERGEFIELD "Links" }"}
even after I press "update field". So Word is not letting me enter HYPERLINK for some reason.
How do I solve this?
From the Microsoft Web Site,
On the Insert menu, click Field (under Quick Parts in later office versions).
In the Field names list, click Hyperlink,
and then click OK. The text Error! Hyperlink reference not valid
appears in the document.
Press ALT+F9 to open the { HYPERLINK \* MERGEFORMAT } field code.
Put the insertion point after HYPERLINK and then add a space.
On the Insert menu, click Field. In the Field names list, click MergeField. In the Field name text box, type the name of the data source field that contains the hyperlink, and then click OK. For example, if the name of the data source field is "Address1," the field code appears as follows: { HYPERLINK { MERGEFIELD "Address1" } \* MERGEFORMAT }
Press ALT+F9 to close the field code. You now have Error! Hyperlink reference not valid text.
Put the insertion point at some arbitrary place in the middle of the Error! Hyperlink reference not valid text, and insert the text you want to appear. If you would like a merge field, insert that from the "Insert Merge Field" option on the menu.
Delete the remaining text of Error! ... before and after the text you want to keep.
Note: If your merge field only contains an identifier, with the URL to be provided as a static part of the merge document, this can work also. On Step 5, you will need to insert the URL text as well as the MergeField, for example:
{ HYPERLINK "https://www.myurl.com/EditForm.aspx?ID={ MERGEFIELD ID }" \* MERGEFORMAT }
I also have been struggling with this. What I found to be the key is creating the document from SCRATCH and not saving it before executing the mail merge. Here are the steps I used:
open a new document and click on the step by step mail merge wizard; add your generic text.
To add the variable hyperlink go to insert>quick parts>fields
select hyperlink on the left and click okay
Use alt + F9 to see the hyperlink field code
Type “” and between them insert your merged field so that {HYPERLINK \* MERGEFORMAT} -> becomes {HYPERLINK"{MERGEFIELD"Constructed_URL"}"\*MERGEFORMAT}
Press ALT+F9 to hide the field code
Click in the text "Error! Hyperlink reference not valid" and replace with something generic like click here.
Complete the mail merge BEFORE you save the document.
Hope this helps and good luck!
Start with the process here:
http://support.microsoft.com/kb/912679
However, like user1867326 indicated, the hyperlink (which should be variable with the mail merge) is converted into a static link when the Word file is reopened.
A solution that seems to work is adding a bookmark within the hyperlink field code immediatly after the mergefield.
{ HYPERLINK { MERGEFIELD "Link" } \* MERGEFORMAT }
Click between the } and \ and go to INSERT > Bookmark, give it a name, and this seems to prevent Word from replacing the mergefield with a static hyperlink.
This answer is adapted and hopefully clarified from the clever solution described here:
Mailmerge dynamic hyperlink fields lost after save/reload of document - Word 2010
This is what worked for me for an email mail merge with variable hyperlinks that all display the same text. I am using Office 2016 Word.
Put the insertion point where you would like the link display text to be in the document.
On the Insert menu, click Quick Parts, and select Field.
In the Field Names list, click Hyperlink, and then click OK. The text Error! Hyperlink reference not valid appears in the document.
Press ALT+F9 to open the { HYPERLINK * MERGEFORMAT } field code. 5. Put the insertion point after HYPERLINK.
On the Insert menu, click Quick Parts, and select Field. In the Field Names list, click MergeField.
In the Field name text box, type the name of the data source field that contains the hyperlink, and then click OK. For example, if the name of the data source field is "Address1," the field code appears as follows: { HYPERLINK { MERGEFIELD Address1 } * MERGEFORMAT }
Press ALT+F9 to close the field code. You now have Error! Hyperlink reference not valid text.
Put the insertion point just before the period in ‘Error! Hyperlink reference not valid text.’ and delete all the text except for the period. Write in the link display text. Make sure not to delete the period (this is important).
To check that the hyperlinks are mapping, right click on the link display text and click edit hyperlink. You will see the correct link in the address field for each record.
I like to style the period text color to white so it looks invisible.
I have had limited success with the other suggestions - basically, Word is buggy in this area. There is a simple alternative - indeed the only alternative AKAIK if you want the hyperlink to vary AND the text that is displayed to vary too (not an unreasonable requirement).
Note that this only works for DOCUMENT MERGES, not for EMAIL MERGES, since it relies on processesing the output document.
The trick is to add a place marker (text that serves as an ID) wherever you want a hyperlink. Insert this via a regular mergefield. In your table of source data, you need columns
place marker, text_for_display, hyperlink
You then need to put your source data into Excel (if that isn't where you have it already) and put this formula
="Set Rng = ActiveDocument.Content: Rng.Find.Execute FindText:="""&[place marker]&""": ActiveDocument.Hyperlinks.Add Anchor:=Rng, Address:="""&[hyperlink]&""", TextToDisplay:="""&[text_for_display]&""""
into a blank column in the first row, and fill down.
You need to amend [place marker],[hyperlink] and [text_for_display] to the appropriate cell references.
If the [hyperlink] includes a query string, you may find you want to build it using a formula, based on other data in the source.
The resulting formulae can then be pasted into a macro and run on the output document.
You may then want to use the "Robbins/Mayor" macro http://www.wordbanter.com/showthread.php?t=18346 to split the generated documents.
I got this working thanks to this thread, but then it stopped working and I figured out why so I thought I'd say thanks by posting what I discovered here.
The issue is that word has a special type of curly brace. Copying the code text from this thread may not work for that reason. To get the curly braces Word wants, I used Fn + Command + F9 on my Mac running Catalina and Office 365. Apparently some Mac users may find Command + F9 works -- just test to find out. I'm sure Windows has a similar keystroke set. I found the Mac solution in this post.
Also, the syntax that works for me is: { HYPERLINK { MERGEFIELD "url" } * MERGEFORMAT } where "url" is the label of the column with the link in my Excel file. Note the position of the quotes. This differs from other posts here (I tried the other combinations) but I wouldn't be surprised if the syntax may vary across Word versions and operating systems.
To get this method to work in Mac Office 365 (Big Sur), you need to know about the Toggle Field Codes in the context menu. The shortcut above to replicate the Alt-F9 behavior will create a new field, instead of allowing you to edit the field that exists.
At these steps:
Press ALT+F9 to open the { HYPERLINK * MERGEFORMAT } field code.
OR
Press ALT+F9 to close the field code. You now have Error! Hyperlink reference not valid text.
Instead of the keyboard shortcut, right click on the error message and select Toggle Field Codes from the context menu.
Answer worked great for me with this adjustment! Thank you!

Word 2007 password protection for single words

is it possible to put a password on a single word inside a word document?
What I want is to write a system documentation from our IT structure. I want to put the passwords in the document as well. Now I want that you have to enter a "MASTER" password in order to unlock to passwords inside the document. If you do not enter the password, the passwords inside the document should be invisible or something like that.
Is that possible?
Yes, this is possible. To make this work we have to use a combination of Word's inherent document protection, its hidden text attribute, and a couple of VBA tricks.
First, format all the passwords in the document as hidden text. (For those who are unfamiliar, hidden text is only visible when the Show/Hide function is set to true.)
Then add code to make sure that the hidden text will not display and also to protect the document from being edited whenever the document is opened:
Private Sub Document_Open()
AddProtection
End Sub
Sub AddProtection()
With ActiveDocument
.ActiveWindow.View.ShowAll = False
.ActiveWindow.View.ShowHiddenText = False
.Application.Options.PrintHiddenText = False
.Protect Type:=wdAllowOnlyReading, NoReset:=True, Password:="DesiredPassword"
End With
End Sub
Because Word users can normally display hidden text at any given time, we also need to take control of this function. Most menu and Ribbon commands can be intercepted by creating a module containing subroutines named for the intercepted commands. Naming a Sub ShowAll will allow us to control this function and only display hidden text when a password is entered:
Sub ShowAll()
If ActiveDocument.ProtectionType = wdAllowOnlyReading Then
'Do nothing
Else 'restore the Ribbon's toggle function
If ActiveDocument.ActiveWindow.View.ShowAll = True Then
ActiveDocument.ActiveWindow.View.ShowAll = False
Else
ActiveDocument.ActiveWindow.View.ShowAll = True
End If
End If
End Sub
Finally, we add some code to prompt the user for a password and, if entered correctly, display the text that was formerly hidden:
Sub RemoveProtection()
Dim strPassword As String
strPassword = InputBox("Enter document password.")
ActiveDocument.Unprotect Password:=strPassword
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.ActiveWindow.View.ShowHiddenText = True
End If
End Sub
Once all the VBA code is entered, right-click on the module in the IDE, select Project Properties, and assign a separate password on the Protection tab. This will stop power users from getting to the embedded password or changing the code.
All that's left to do is create a QAT button in Word (that is only visible for this document) and assign the RemoveProtection sub to it. Whenever the document is opened the passwords will be hidden and protected from editing, but can then be revealed by clicking on the QAT button and entering the correct password.
EDIT
When I first answered this question, I failed to consider that Word has a hidden text option that can be turned on separateley from the Show All option. Additionally, hidden text can be printed via a printing option. I have added code in AddProtection above to turn off these settings.
Further testing also revealed that a user in Word 2007 could manually go into Office Orb|Options|Display to reveal the hidden text by changing the Show all formatting marks or Hidden text options manually. To avoid this, a Ribbon customization would need to be created and loaded with the document.
Finally, it is worth noting that although it is great fun to bend Word to one's will in order to make it accomplish tasks like this, the level of protection is not as good as encrypting the passwords separately and then unecrypting before revealing the contents or even using Word's document password feature to encrypt the document's entire contents.