View Single Post
  #5   Report Post  
MCubitt
 
Posts: n/a
Default

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
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
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