Thanks Peter. In fact I already use VBA to obtain current docpath and then
dictate the source, but Word errors before VBA is reached, since the merge
seems to be validated upon opening.
You say save without a connection to the data source, how do I do that? If
I remove the data source won't I lose the merge definition, what goes where?
Thanks
> You say save without a connection to the data source, how do I do that?
In VBA, use
ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
> If
> I remove the data source won't I lose the merge definition, what goes
> where?
You will lose the information about the source file or database, and any
sort or filter information, and propably the "current preview record" which
Word seems to save when it is connected to a data source.
To apply sorts and filters programmatically, you have to work out what SQL
is needed, e.g.
SELECT * FROM something WHERE x = 'abc' ORDER BY 1 ASC
or whatever. The only thing this misses is that there is another mechanism
for filtering records in Word 2002/2003 which is to select/reject individual
records in the Merge Recipients dialog box. That kind of filtering is not
implemented using SQL - Word just tries to remember enough info. about each
record to be able to apply the criteria. I recommend that you stay away from
that stuff.
Peter Jamieson
> Thanks Peter. In fact I already use VBA to obtain current docpath and
> then
[quoted text clipped - 59 lines]
>> >
>> > Is this possible please?
MCubitt - 24 Jun 2005 09:03 GMT
Thanks again Peter.
Now I could just be not too bright, but would you mind clarifying a few
things please?
First of all, the mail merge is very very simple. It uses an ASCII text
(csv) file as its data source. There is no filtering or sorting, that's been
done in the text file already. So all I do is map a few of the fields to
places on the Word document.
The data source is fixed at c:\folder1\data.txt but is actually generated by
a job which runs in Excel and then open this (merge) document. So the point
is that these files could be anywhere so the fixed data source is an issue.
(if the files end up on i:\temp\ then of course opening the word file fails)
There is a VBA script using document_open() which does the following:
[code]
' On opening this word doc, merge then close
Sub Document_Open()
' Filename
rightnow = Now()
newdocfilename = "AddressBook " & Year(rightnow) & "-" &
Format(Month(rightnow), "00") & "-" & Format(Day(rightnow), "00") & " " &
Format(Hour(rightnow), "00") & "." & Format(Minute(rightnow), "00") & "." &
Format(Second(rightnow), "00") & ".doc"
newdocfilename = ActiveDocument.Path & "\" & newdocfilename
datasourcefilename = "data.txt"
datasourcefilename = ActiveDocument.Path & "\" & datasourcefilename
ActiveDocument.MailMerge.OpenDataSource Name:=datasourcefilename
ActiveDocument.MailMerge.Execute
ActiveDocument.SaveAs filename:=newdocfilename
ActiveDocument.Close SaveChanges:=False
ActiveDocument.Close SaveChanges:=False
End Sub
[/code]
But the problem is that when Word opens the document, before reaching the
VBA is tries to connect to the data source.
Your suggestion is to use
[code]ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
[/code] in VBA.
But where & when?
If I run the document up and enable macros it will run my VBA script and
auto close. if I disable macros I cannot run the command you rpvided (tried
in the Immediate window).
So I am not sure if I misunderstood or my execution of the method was wrong!
Thanks again
Peter Jamieson - 24 Jun 2005 10:10 GMT
> Your suggestion is to use
> [code]ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
> [/code] in VBA.
> But where & when?
What you need to do once and once only, before you deploy your solution, is
to open your mail merge main document and run that line of code, either in
the Immediate window in the VBA Editor, or as, e.g.
Sub x()
ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
End Sub
then save the document. (In fact you can just use the Mailmerge
toolbar/wizard/mailmergehelper to switch the document back to "Normal" but
for people who are already using VBA it is simpler to give the one-liner.)
After you have made the document a normal document and saved it, when Word
opens the document you will not see the message about not being able to find
the data source. When your macro executes
ActiveDocument.MailMerge.OpenDataSource Name:=datasourcefilename
the document will become a mail merge main document again - I think it
defaults to type Form Letters, but you can set
ActiveDocument.MailMerge.MainDocumentType
to the value you want. The other thing you may have to consider if you
distribute your solution to users of recent versions and SPs of Word is that
a security issue may prevent Word from opening the data source from code
unless you apply the information given in
http://support.microsoft.com/default.aspx?scid=kb;en-us;825765
Peter Jamieson
> Thanks again Peter.
>
[quoted text clipped - 59 lines]
>
> Thanks again
MCubitt - 24 Jun 2005 10:33 GMT
Thank you very much Peter. In the end I manually removed the merge factor,
returning it back to a "normal" document. Of course the field references
remained <<...>> which was initially my worry they'd vanish!
Anyway, it worked and I can finally relase the document to a user base.
Thanks again