I want to open MS Word docs from my app. I use ShellExecute function to do the job. It does open a word doc that I specified, but it restored a doc that I opened before and minimised, that is not what I want. Also the ShellExecute returns 42 that I don't know the meaning. Does I used the ShellExecute incorrectly?
using System;
using System.Windows.Forms;
namespace Test_Doc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport("Shell32.dll")]
public static extern int ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirecotry, int nShowCmd);
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
private void button1_Click(object sender, EventArgs e)
{
string filePath = System.Reflection.Assembly.GetEntryAssembly().Location;
string fileName = System.IO.Path.Combine(filePath, "2.docx");
int i = ShellExecute(this.Handle, "Edit", fileName, null, null, SW_SHOWNORMAL);
MessageBox.Show(i.ToString());
}
}
}
CodePudding user response:
ShellExecute doesn't let you control the application. However, you may automate Word from a C# application where you could manage the application afterwards. For example, see How to automate Microsoft Word to create a new document by using Visual C# for more information.