Free Support Forum - aspose.com

Opening in Word?

I’m very, VERY new to these tools so forgive me if this is a painfully simple questions, but I’m making a Windows Application and am trying to get this report to open in word after the menu option is clicked and I just can’t figure out how. Here is the code…

private void Generate_Needs_Report()
{
string licenseFile = “Aspose.Component.lic”;
Aspose.Word.Word.SetLicense(licenseFile);

Word word = new Word();
Document doc = word.Open(“Needs.doc”);

doc.MailMerge.Execute(
new string[] {lotsofvariables},
new object[] {lots of text boxes});

doc.Save(
“PersonalizedLetter1.doc”,
SaveFormat.FormatDocument);
}

This saves the document perfectly but it does not open the document for the person to view/edit.

Now, there’s an argument to “openinword” in the doc.save but that also requires an httpresponse and since this is a windows application I don’t think that applies here. Can you give me any help?

Thanks in advance,

Tom

Hi Tom,

“OpenInWord” is only suitable when you are in an ASP.NET application and want to send the document to the client browser (Internet Explorer) and instruct it to open the document in MS Word on the user’s machine.

If you want to start MS Word executable from within a windows application you need look for a function called something like ShellExecute or whatever. You will be able to pass the full name of the .doc file to it and because .doc extension is normally registered to fire MS Word (like double clicking on a file) it will start. I cannot give you direct instructions because I’m not sure how this is done in .NET now, but there certainly must be a way. See the Process class in MSDN.

Thanks, that was enough to point me in the right direction. I appreciate the help.

Tom

P.S. For anyone who might stumble on this thread looking for the same answer, it’s essentially three lines of code. First, put this line in:

using System.Runtime.InteropServices;

Then, at the beginning of your class in the declarations:

[DllImport(“shell32.dll”, EntryPoint=“ShellExecute”)] public static extern int ShellExecuteA(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);

Finally, this line in the function:

ShellExecuteA(0, “open”, “PersonalizedLetter1.doc”, null, null, 1);