i have a small application developed in C# .NET
that manipulate excel sheets, I don't know why some users keep telling me that when they open the excel file the window doesn't appear on front/top although I set the visible to true and the window state on maximized.
This is the function that reads the excel file:
public static void OpenExcel(string fileName, bool visibility, FunctionToExecute fn = null)
{
string addInPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft\\AddIns\\mDF_XLcalendar.xla");
deleg = fn;
app = new Excel.Application();
app.Workbooks.Open(addInPath);
app.Workbooks.Open(fileName);
app.ScreenUpdating = true;
app.DisplayAlerts = true;
app.Visible = visibility;
app.UserControl = true;
app.WindowState = Excel.XlWindowState.xlMaximized;
EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(application_WorkbookBeforeClose);
EventSave_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Open_ExcelApp_WorkbookBeforeSave);
app.WorkbookBeforeClose += EventDel_BeforeBookClose;
app.WorkbookBeforeSave += EventSave_BeforeBookClose;
}
Any ideas ?
I found this to work. How to bring an Excel app to the front
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static void BringExcelWindowToFront(Application xlApp)
{
string caption = xlApp.Caption;
IntPtr handler = FindWindow(null, caption);
SetForegroundWindow(handler);
}
I would try to activate the excel window by
app.ActiveWindow.Activate();
If this doesn't work you might find other solutions (involving Native WinAPI calls) at this thread at http://social.msdn.microsoft.com/
some magic, that work for me:
app.WindowState = XlWindowState.xlMinimized; // -4140
app.WindowState = XlWindowState.xlMaximized; // -4137
Sometimes if there is more than one file open at same time in Excel, ActiveWindow.Activate()
will not work.
A successful trick is to do minimize then maximize commands.
A little late I know, but why the need to use FindWindow, the Windows handle is accessible via the Application :
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public static void BringExcelWindowToFront(Application xlApp)
{
SetForegroundWindow((IntPtr)xlApp.Hwnd); // Note Hwnd is declared as int
}
There will be no problems if more than one window has the same caption.
To activate a specific workbook:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public static void BringExcelWindowToFront(Excel._Workbook xlBook)
{
var windows = xlBook.Windows;
foreach (Excel.Window window in windows)
{
IntPtr handler = (IntPtr)window.Hwnd;
SetForegroundWindow(handler);
return;
}
}
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.