简体   繁体   中英

How do I open Microsoft Word and display a specific .doc containing a mail-merge by using Visual C# Express?

My application updates items on a database and then opens mail-merge documents which are populated with the fields from a table on the database.

I have the .docs saved to a network drive, but my two previous attempts to open them have returned different errors; either word tells me that there was an error trying to open the file, or it tells me that the file is in use by myself and is locked for editing.

Here are the two methods, respectively:

//method1
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "WINWORD.EXE";
startInfo.Arguments = fileName;
Process.Start(startInfo);

//method 2 (a simplified version of the first)
Process.Start(fileName);

Is there another way to open these Microsoft Word documents using C#, or anything clearly wrong with the methods above?

Both your methods should work... but here is another one to try. Maybe you will get a better error message.

var type = Type.GetTypeFromProgID("Word.Application");
dynamic word = Activator.CreateInstance(type);
word.Visible = true;
word.Documents.Open(@"C:\test.docx");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM