How to get mailmerge data from a word document using interop assemblies
Posted on 2008-11-06
I have a word document (.doc), a merge file (.txt) and a header source file (.txt).
The word document is merged with the data from the merge file while the header source file tells about columns names.
Classic word mail merge.
I use word 2002 and the developed application is to be used where word 2002 is present. Furthermore I use Visual Studio, C# with
framework 2.0 and the Microsoft Interop Assemblies for Office XP.
When I open the word file in microsoft word 2002, Word ask if I want to merge with the merge data file. If i rename the header source file
it tells me that it can't be found. So, this information must be included in the word document.
I'm trying to developing an application that finds the path of the mergefile and the header source file. But everytime I open
the word file and get the data from the mailmerge API both the connectionstring ned headersource fields are empty. Also word
doesn't ask me if I want to merge the data if i open the file from within my application.
Here's the simplicied code:
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
app.Visible = true;
Document doc = null;
Object missing = Type.Missing;
Object trueValue = true;
Object falseValue = false;
Object path = "C:\\test\\test.doc";
doc = app.Documents.Open(ref path, ref missing, ref falseValue, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
string s = doc.MailMerge.DataSource.ConnectString;
It seems like the information is somehow not loaded together with the word document. But I really can't figure out why.
Thanks in advance