自1995年以来我一直在使用Office,每次有新版本时,从其他应用程序激活文档都会出现问题。现在我又在那里。
我正在使用VS 2017 C#Office 2016 64位
这是一个与SharePoint一起用于templatemanager的C#Windows窗体应用程序。我从ex开始模板。
bool YesNo = IsExcelFileOpened();
Microsoft.Office.Interop.Excel.Application ExcelObj;
Microsoft.Office.Interop.Excel.Workbook ExcelVbookObj;
if (YesNo==false)
{
ExcelObj = new App();
}
else
{
ExcelObj = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
ExcelObj.Visible = true;
public bool IsExcelFileOpened()
{
bool isExist = false;
System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses();
foreach (Process pr in prs)
{
if (pr.ProcessName == "EXCEL")
{
isExist = true;
break;
}
}
return isExist;
}
我尝试过;
1像Visisble Activate这样的常规办公命令
2 [DllImport(“ user32.dll”)]的使用
public static void FocusWindow(IntPtr focusOnWindowHandle)
{
int style = GetWindowLong(focusOnWindowHandle, GWL_STYLE);
// Minimize and restore to be able to make it active.
if ((style & WS_MINIMIZE) == WS_MINIMIZE)
{
ShowWindow(focusOnWindowHandle, SW_RESTORE);
}
uint currentlyFocusedWindowProcessId = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero);
int appThreadint = GetCurrentThreadId();
uint appThread = Convert.ToUInt32(appThreadint);
if (currentlyFocusedWindowProcessId != appThread)
{
AttachThreadInput(currentlyFocusedWindowProcessId, appThread, true);
BringWindowToTop(focusOnWindowHandle);
ShowWindow(focusOnWindowHandle, SW_SHOW);
AttachThreadInput(currentlyFocusedWindowProcessId, appThread, false);
}
else
{
BringWindowToTop(focusOnWindowHandle);
ShowWindow(focusOnWindowHandle, SW_SHOW);
}
}
如果我从ex Word启动应用程序,然后从应用程序启动Excel模板,则在打开Exel文档时,Word仍位于顶部。
请给我一些帮助。
Ola