Log in

View Full Version : Mailmerge data in multiple Word documents



Matthias005
05-28-2013, 03:00 AM
Hello,
I'm trying to use a word document as input for project specific information. This information is needed in multiple other Word documents so I am using the Mailmerge function. After you have filled in the word document you'll have to choose the language of the Word documents.I am making this with a document someone else already built. I am getting problems when changing the input fields and declarations of them.
This is my code so far:

Sub Projectplan()


ActiveDocument.SaveAs FileName:="C:\temp\Projectgegevens.txt", FileFormat:= _
wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:= _
False, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=True, _
SaveAsAOCELetter:=False

Application.DisplayStatusBar = True

With ActiveDocument
.ReadOnlyRecommended = False
.EmbedTrueTypeFonts = False
.SaveFormsData = False
.SaveSubsetFonts = False
.Password = ""
.WritePassword = ""
End With

Dim opmdat As String

WordBasic.FileOpen Name:="C:\temp\Projectgegevens.txt"
WordBasic.Insert opmdat + Chr(10)
WordBasic.FileSave
WordBasic.FileClose 1

Keuzescherm.Show

End Sub



Sub Samenvoegen()

ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:= _
"C:\Temp\Projectgegevens.txt", ConfirmConversions:=False, ReadOnly:=False _
, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", WritePasswordDocument:="", WritePasswordTemplate:= _
"", Revert:=False, Format:=wdOpenFormatAuto, Connection:="", SQLStatement _
:="", SQLStatement1:=""

With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument

With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With
einde:
End Sub

macropod
05-28-2013, 03:37 AM
If you want help, it would be useful if you:
a) use the VBA tags so that you code gets displayed with a proper structure;
b) said exactly which lines are generating errors;
c) posted all of the relevant code, not including empty/meaningless subs (e.g. Annuleer_Click, UserForm_Click) and unpopulated/undefined variables (e.g. opmdat, Keuzescherm); and
d) described the data layout you're working with.

You say you are "getting problems when changing the input fields and declarations of them", but I can't see what you mean by this - in all the code you posted, only two variables are declared and only one of those ever gets populated.

FWIW, I can't see why you'd Word as the input 'form' for your data or why you'd use mailmerge at all for this.

Matthias005
05-28-2013, 03:51 AM
I marked the part red which gives the runtime error 5992.
The first sub declares the variables of the input fields in the Word document (at this moment there is only one variable). This information is saved in a .txt file.

Then there is a userform which let the user choose the language of the Word documents the information is going to be in.

The last sub is to mailmerge the information with the chosen documents.

And macropod what would be a better input form or way to do this?
Thanks alot!

macropod
05-28-2013, 04:11 AM
What does the runtime error 5992 actually say (and are you sure it wasn't 5922 - can't open the data source)? FWIW, since your opmdat variable is empty, it's not actually adding anything to the text document. Did you check its contents? I suspect you'll find only an empty line.

As for a different method, why not store a set of custom document properties in each of the child documents, then use DOCPROPERTY fields to reference them? You can then update the child documents directly, by updating the custom document properties. All done via a double loop - the outer one for the documents, the inner one for the custom document properties.

Alternatively, depending on your setup, simply bookmark the values in your input document and use INCLUDETEXT fields in the child documents to reference them.

Matthias005
05-28-2013, 04:23 AM
It is a 5922 error sorry. It say indeed can't open de data source.
What do you mean with checking it's contents?
And I don't think the other method works because I need one main document that fills in other Word templates with general information for a specific project. Those Word templates are going to be used by different people.

macropod
05-28-2013, 04:28 AM
What do you mean with checking it's contents?
have you actually opened the file to see what is in it?

I need one main document that fills in other Word templates with general information for a specific project. Those Word templates are going to be used by different people.
In that case, the custom document properties method is ideally suited. You simply create a new document from the template, update the properties in the document, save & close it. All very simple. It also means the custom document property values remain with the document even if a user later edits the text, and you can add more references to the same property if you need to.

Matthias005
05-28-2013, 04:37 AM
Yes it is a table in the Word template like this:
Fieldcode |description |content
The content field is a {Formtext} field.
But do you think the custom document properties method works with around 20 templates because that is the ammount that needs to be filled in. If so how would that work?

macropod
05-28-2013, 05:10 AM
A text file cannot contain a table...

You can easily populate a custom document property from a formfield's result.

For a discussion and code for document property usage, see: http://word.mvps.org/faqs/MacrosVBA/MixedDocProps.htm

Matthias005
05-28-2013, 05:19 AM
Oh the text file looks like this:
opmdat
" "
Thanks for the information about the custom document properties but it seems very complicated to me:S

macropod
05-28-2013, 11:58 PM
If that's too complicated for you, you could still use INCLUDETEXT fields. Simply break their links when the new documents created from the templates are saved. Again, a whole lot simpler than trying to automate a mailmerge for what is, after all, a single record each time.

Matthias005
05-29-2013, 02:38 AM
But when using includetext fields the user needs to open all the documents and save them instead of doing that automatic or doesn't he?

macropod
05-29-2013, 03:09 AM
You're not paying attention. Read my last post again. If you implement the process I described, there will be no INCLUDETEXT fields in the new document once it's saved.