Background: When you open Excel through C # code contained within Excel macros can not be loaded normally, but when the mouse is opened, the macro load properly and therefore can not expect to open the file by WinApi.
Official website address: https://docs.microsoft.com/zh-cn/windows/win32/api/shellapi/nf-shellapi-shellexecutea
WinApi in ShellExecute function is to run an external program (or open a file registered to open a directory, print a file, etc.), and external programs have some control. Function is as follows:
IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
Parameter Description:
HWND: Specifies the parent window handle, is not specified may be null or 0
lpOperation: the specified operation, the value may be [open], [print], [explore], the following interpretation:
open: lpFile parameters specified by the execution program, or open lpFile parameters specified by the file or folder; Print: Print the file specified by the parameters lpFile; Explore: Browse the file specified by the parameters lpFile folder.
When the parameter is set to null , the default is open.
lpFile: Specifies the file to open or a program
lpParameters: To open the program to the specified parameters; If you open a file, is null
lpDirectory: default directory
nShowCmd: open settings, specific meanings are as follows:
= SW_HIDE 0 ; // hides SW_SHOWNORMAL = . 1 ; // with the display size and position of the latest activation SW_NORMAL = . 1 ; // with SW_SHOWNORMAL SW_SHOWMINIMIZED = 2 ; // minimized activation SW_SHOWMAXIMIZED = . 3 ; // maximized, activated = SW_MAXIMIZE . 3 ; // with SW_SHOWMAXIMIZED SW_SHOWNOACTIVATE = . 4 ; // with the latest display size and position is not activated SW_SHOW = . 5 ; // same SW_SHOWNORMAL = The SW_MINIMIZE . 6 ; // minimized without activating SW_SHOWMINNOACTIVE = . 7 ; // with the SW_MINIMIZE SW_SHOWNA = . 8 ; // with SW_SHOWNOACTIVATE SW_RESTORE = . 9 ; // with SW_SHOWNORMAL SW_SHOWDEFAULT = 10 ; // with SW_SHOWNORMAL sw - max = 10 ; // the same SW_SHOWNORMAL
Return Value:
When the return value is greater than 32, i.e. successful. Failure to execute the return value of specific significance as follows:
0 = 0 // Insufficient memory ERROR_FILE_NOT_FOUND = 2 ; // filename error ERROR_PATH_NOT_FOUND = 3 ; // pathname error ERROR_BAD_FORMAT = 11 ; // EXE file is invalid SE_ERR_SHARE = 26 ; // occur shared error SE_ERR_ASSOCINCOMPLETE = 27 ; // file were incomplete or invalid SE_ERR_DDETIMEOUT = 28 ; // timeout SE_ERR_DDEFAIL = 29 ; // DDE transaction failed SE_ERR_DDEBUSY = 30; // are dealing with other matters and DDE DDE transaction could not be completed SE_ERR_NOASSOC = 31 ; // no application associated
C # code to achieve the following:
1 class Program 2 { 3 /// <summary> 4 /// 显示方式 5 /// </summary> 6 public enum ShowCommands : int 7 { 8 SW_HIDE = 0, 9 SW_SHOWNORMAL = 1, 10 SW_NORMAL = 1, 11 SW_SHOWMINIMIZED = 2, 12 SW_SHOWMAXIMIZED = 3, 13 SW_MAXIMIZE = 3, 14 SW_SHOWNOACTIVATE = 4, 15 SW_SHOW = 5, 16 SW_MINIMIZE = 6, 17 SW_SHOWMINNOACTIVE = 7, 18 SW_SHOWNA = 8, 19 SW_RESTORE = 9, 20 SW_SHOWDEFAULT = 10, 21 SW_FORCEMINIMIZE = 11, 22 SW_MAX = 11 23 } 24 25 [DllImport("shell32.dll")] 26 static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd); 27 28 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] 29 private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);// 30 31 [DllImport("user32", CharSet = CharSet.Ansi, EntryPoint = "FindWindowA", ExactSpelling = false, SetLastError = true)] 32 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 33 34 [DllImport("Oleacc.dll")] 35 public static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, ref Microsoft.Office.Interop.Excel.Window ptr); 36 37 static void Main(string[] args) 38 { 39 string filepath = @"C:\Users\Administrator\Desktop\ddd.txt"; 40 IntPtr result = ShellExecute(IntPtr.Zero, "open", filepath, "", "", ShowCommands.SW_SHOWNORMAL); 41 42 if (result.ToInt32() <= 32) 43 { 44 Console.WriteLine ( " open failure " ); 45 } 46 is 47 the Console.ReadKey (); 48 } 49 }