View Single Post
  #6   Report Post  
Peter Jamieson
 
Posts: n/a
Default

Your suggestion is to use
Code:
ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument
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...b;en-us;825765

Peter Jamieson


"MCubitt" wrote in message
...
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