using System; using System.Collections.Generic; using System.Linq; using System.Text; using Foo.Platform.Win32; using System.Runtime.InteropServices; namespace Foo.WorkspaceMgr.Hiders { internal class ShowNHide_Office : IShowNHide { #region IShowNHide Members public FuncDepBoolType IQueryShow(string strArtifactLocation) { switch (Win32_WrapperFunc.GetRegisteredExeForFile(strArtifactLocation)) { case "WINWORD.EXE": return QueryShow_MSWord(strArtifactLocation); case "EXCEL.EXE": return QueryShow_MSExcel(strArtifactLocation); case "POWERPNT.EXE": return QueryShow_MSPowerPoint(strArtifactLocation); case "MSPUB.EXE": return QueryShow_MSPublisher(strArtifactLocation); case "VISIO.EXE": return QueryShow_MSVisio(strArtifactLocation); //case "ACCESS.EXE": // return QueryClose_MSAccess(strArtifactLocation); } return FuncDepBoolType.FunctionallityNotSupported; } public FuncDepBoolType IShow(string strArtifactLocation) { switch (Win32_WrapperFunc.GetRegisteredExeForFile(strArtifactLocation)) { case "WINWORD.EXE": return Show_MSWord(strArtifactLocation); case "EXCEL.EXE": return Show_MSExcel(strArtifactLocation); case "POWERPNT.EXE": return Show_MSPowerPoint(strArtifactLocation); case "MSPUB.EXE": return Show_MSPublisher(strArtifactLocation); case "VISIO.EXE": return Show_MSVisio(strArtifactLocation); //case "ACCESS.EXE": // return QueryClose_MSAccess(strArtifactLocation); } return FuncDepBoolType.FunctionallityNotSupported; } public FuncDepBoolType IQueryHide(string strArtifactLocation) { switch (Win32_WrapperFunc.GetRegisteredExeForFile(strArtifactLocation)) { case "WINWORD.EXE": return QueryHide_MSWord(strArtifactLocation); case "EXCEL.EXE": return QueryHide_MSExcel(strArtifactLocation); case "POWERPNT.EXE": return QueryHide_MSPowerPoint(strArtifactLocation); case "MSPUB.EXE": return QueryHide_MSPublisher(strArtifactLocation); case "VISIO.EXE": return QueryHide_MSVisio(strArtifactLocation); //case "ACCESS.EXE": // return QueryClose_MSAccess(strArtifactLocation); } return FuncDepBoolType.FunctionallityNotSupported; } public FuncDepBoolType IHide(string strArtifactLocation) { switch (Win32_WrapperFunc.GetRegisteredExeForFile(strArtifactLocation)) { case "WINWORD.EXE": return Hide_MSWord(strArtifactLocation); case "EXCEL.EXE": return Hide_MSExcel(strArtifactLocation); case "POWERPNT.EXE": return Hide_MSPowerPoint(strArtifactLocation); case "MSPUB.EXE": return Hide_MSPublisher(strArtifactLocation); case "VISIO.EXE": return Hide_MSVisio(strArtifactLocation); //case "ACCESS.EXE": // return QueryClose_MSAccess(strArtifactLocation); } return FuncDepBoolType.FunctionallityNotSupported; } #endregion #region Microsoft Word IShowNHide Members FuncDepBoolType QueryShow_MSWord(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningWordDocuments = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Word.Document doc in RunningWordDocuments) { foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows) { if(window.Document.FullName.ToLower() == strArtifactLocation.ToLower()) { if(!window.Visible) return FuncDepBoolType.Success; else return FuncDepBoolType.Failed; } } } return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Show_MSWord(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningWordDocuments = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Word.Document doc in RunningWordDocuments) { foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows) { if (window.Document.FullName.ToLower() == strArtifactLocation.ToLower()) { if (!window.Visible) { window.Visible = true; return FuncDepBoolType.Success; } else { return FuncDepBoolType.Failed; } } } } return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType QueryHide_MSWord(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningWordDocuments = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Word.Document doc in RunningWordDocuments) { foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows) { if (window.Document.FullName.ToLower() == strArtifactLocation.ToLower()) { if (window.Visible) return FuncDepBoolType.Success; else return FuncDepBoolType.Failed; } } } return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Hide_MSWord(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningWordDocuments = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Word.Document doc in RunningWordDocuments) { foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows) { if (window.Document.FullName.ToLower() == strArtifactLocation.ToLower()) { if (window.Visible) { window.Visible = false; return FuncDepBoolType.Success; } else { return FuncDepBoolType.Failed; } } } } return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } #endregion #region Microsoft Excel IShowNHide Members FuncDepBoolType QueryShow_MSExcel(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningExcelWorkbooks = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Excel.Workbook book in RunningExcelWorkbooks) { if (book.FullName.ToLower() == strArtifactLocation.ToLower()) { var app = book.Application; bool bIsTheOnlyDocumentInApp = (app.Workbooks.Count == 1); if (!app.Visible && bIsTheOnlyDocumentInApp) return FuncDepBoolType.Success; else return FuncDepBoolType.Failed; } } return FuncDepBoolType.ArtifactLocationUnavailable; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Show_MSExcel(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningExcelWorkbooks = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Excel.Workbook book in RunningExcelWorkbooks) { if (book.FullName.ToLower() == strArtifactLocation.ToLower()) { var app = book.Application; bool bIsTheOnlyDocumentInApp = (app.Workbooks.Count == 1); if (!app.Visible && bIsTheOnlyDocumentInApp) { app.Visible = true; return FuncDepBoolType.Success; } else return FuncDepBoolType.Failed; } } return FuncDepBoolType.ArtifactLocationUnavailable; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType QueryHide_MSExcel(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningExcelWorkbooks = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Excel.Workbook book in RunningExcelWorkbooks) { if (book.FullName.ToLower() == strArtifactLocation.ToLower()) { var app = book.Application; bool bIsTheOnlyDocumentInApp = (app.Workbooks.Count == 1); if (app.Visible && bIsTheOnlyDocumentInApp) { return FuncDepBoolType.Success; } else return FuncDepBoolType.Failed; } } return FuncDepBoolType.ArtifactLocationUnavailable; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Hide_MSExcel(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningExcelWorkbooks = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Excel.Workbook book in RunningExcelWorkbooks) { if (book.FullName.ToLower() == strArtifactLocation.ToLower()) { var app = book.Application; bool bIsTheOnlyDocumentInApp = (app.Workbooks.Count == 1); // We could potentially not hide the window but instead just minimize the selected workbook // ~have to learn //if (!bIsTheOnlyDocumentInApp) //{ // foreach (Microsoft.Office.Interop.Excel.Window window in book.Windows) // { // window.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized; // } //} if (app.Visible && bIsTheOnlyDocumentInApp) { app.Visible = false; return FuncDepBoolType.Success; } else return FuncDepBoolType.Failed; } } return FuncDepBoolType.ArtifactLocationUnavailable; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } #endregion #region Microsoft PowerPoint IShowNHide Members FuncDepBoolType QueryShow_MSPowerPoint(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningPowerPoints = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.PowerPoint.Presentation presentation in RunningPowerPoints) { if (presentation.FullName.ToLower() == strArtifactLocation.ToLower()) { switch (presentation.Application.Visible) { case Microsoft.Office.Core.MsoTriState.msoTrue: return FuncDepBoolType.Failed; case Microsoft.Office.Core.MsoTriState.msoFalse: return FuncDepBoolType.Success; } } } } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Show_MSPowerPoint(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningPowerPoints = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.PowerPoint.Presentation presentation in RunningPowerPoints) { if (presentation.FullName.ToLower() == strArtifactLocation.ToLower()) { switch (presentation.Application.Visible) { case Microsoft.Office.Core.MsoTriState.msoTrue: return FuncDepBoolType.Failed; case Microsoft.Office.Core.MsoTriState.msoFalse: { //presentation.Application.Visible = Microsoft.Office.Core.MsoTriState.msoTrue; Win32Functions.ShowWindow((IntPtr)presentation.Application.HWND, (int)WindowAction.SW_SHOW); return FuncDepBoolType.Success; } } } } } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType QueryHide_MSPowerPoint(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningPowerPoints = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.PowerPoint.Presentation presentation in RunningPowerPoints) { if (presentation.FullName.ToLower() == strArtifactLocation.ToLower()) { switch (presentation.Application.Visible) { case Microsoft.Office.Core.MsoTriState.msoTrue: return FuncDepBoolType.Success; case Microsoft.Office.Core.MsoTriState.msoFalse: return FuncDepBoolType.Failed; } } } } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Hide_MSPowerPoint(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { try { var RunningPowerPoints = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.PowerPoint.Presentation presentation in RunningPowerPoints) { if (presentation.FullName.ToLower() == strArtifactLocation.ToLower()) { switch (presentation.Application.Visible) { case Microsoft.Office.Core.MsoTriState.msoTrue: { //presentation.Application.Visible = Microsoft.Office.Core.MsoTriState.msoFalse; //foreach (Microsoft.Office.Interop.PowerPoint.DocumentWindow docWindow in presentation.Windows) //{ // //docWindow. //} Win32Functions.ShowWindow((IntPtr) presentation.Application.HWND, (int)WindowAction.SW_HIDE); return FuncDepBoolType.Success; } case Microsoft.Office.Core.MsoTriState.msoFalse: { return FuncDepBoolType.Failed; } } } } } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here return FuncDepBoolType.ErrorThrown; } } return FuncDepBoolType.ParametersInvalid; } #endregion #region Microsoft Publisher IShowNHide Members FuncDepBoolType QueryShow_MSPublisher(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { IntPtr hWnd = IntPtr.Zero; try { var RunningPublisherApps = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Publisher.Application app in RunningPublisherApps) { foreach (Microsoft.Office.Interop.Publisher.Document pubDoc in app.Documents) { if (pubDoc.FullName.ToLower() == strArtifactLocation.ToLower()) { // Get the Handle hWnd = (IntPtr)pubDoc.ActiveWindow.Hwnd; goto FOUND_DOCUMENT_HWND; } } } return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here } FOUND_DOCUMENT_HWND: return FuncDepBoolType.Success; // Check Window State... - TO DO Later } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Show_MSPublisher(string strArtifactLocation) { IntPtr hWnd = IntPtr.Zero; try { var RunningPublisherApps = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Publisher.Application app in RunningPublisherApps) { foreach (Microsoft.Office.Interop.Publisher.Document pubDoc in app.Documents) { if (pubDoc.FullName.ToLower() == strArtifactLocation.ToLower()) { // Save the Document pubDoc.Save(); // Get the Handle hWnd = (IntPtr)pubDoc.ActiveWindow.Hwnd; goto FOUND_DOCUMENT_HWND; } } } return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here } FOUND_DOCUMENT_HWND: // Send Show Message Win32Functions.ShowWindow(hWnd, (int)WindowAction.SW_SHOW); return FuncDepBoolType.Success; } FuncDepBoolType QueryHide_MSPublisher(string strArtifactLocation) { if (!String.IsNullOrEmpty(strArtifactLocation)) { IntPtr hWnd = IntPtr.Zero; try { var RunningPublisherApps = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Publisher.Application app in RunningPublisherApps) { foreach (Microsoft.Office.Interop.Publisher.Document pubDoc in app.Documents) { if (pubDoc.FullName.ToLower() == strArtifactLocation.ToLower()) { // Get the Handle hWnd = (IntPtr)pubDoc.ActiveWindow.Hwnd; goto FOUND_DOCUMENT_HWND; } } } return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here } FOUND_DOCUMENT_HWND: return FuncDepBoolType.Success; // Check Window State... - TO DO Later } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Hide_MSPublisher(string strArtifactLocation) { IntPtr hWnd = IntPtr.Zero; try { var RunningPublisherApps = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Publisher.Application app in RunningPublisherApps) { foreach (Microsoft.Office.Interop.Publisher.Document pubDoc in app.Documents) { if (pubDoc.FullName.ToLower() == strArtifactLocation.ToLower()) { // Save the Document pubDoc.Save(); // Get the Handle hWnd = (IntPtr)pubDoc.ActiveWindow.Hwnd; goto FOUND_DOCUMENT_HWND; } } } return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here } FOUND_DOCUMENT_HWND: // Send Show Message Win32Functions.ShowWindow(hWnd, (int)WindowAction.SW_HIDE); return FuncDepBoolType.Success; } #endregion #region Microsoft Visio IShowNHide Members FuncDepBoolType QueryShow_MSVisio(string strArtifactLocation) { var RunningVisioDocs = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs) { if (doc.FullName.ToLower() == strArtifactLocation.ToLower()) { try { bool bIsTheOnlyDocumentInApp = true; // TO DO: (doc.Application.Documents.Count == 1); not working if (!doc.Application.Visible && bIsTheOnlyDocumentInApp) return FuncDepBoolType.Success; else return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here } break; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Show_MSVisio(string strArtifactLocation) { var RunningVisioDocs = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs) { if (doc.FullName.ToLower() == strArtifactLocation.ToLower()) { try { bool bIsTheOnlyDocumentInApp = true; // TO DO - (doc.Application.Documents.Count == 1); not working if (!doc.Application.Visible && bIsTheOnlyDocumentInApp) { doc.Application.Visible = true; return FuncDepBoolType.Success; } else return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here } break; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType QueryHide_MSVisio(string strArtifactLocation) { var RunningVisioDocs = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs) { if (doc.FullName.ToLower() == strArtifactLocation.ToLower()) { try { bool bIsTheOnlyDocumentInApp = true; // (doc.Application.Documents.Count == 1); // TO DO - not working if (doc.Application.Visible && bIsTheOnlyDocumentInApp) { return FuncDepBoolType.Success; } else return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here } break; } } return FuncDepBoolType.ParametersInvalid; } FuncDepBoolType Hide_MSVisio(string strArtifactLocation) { var RunningVisioDocs = Win32Functions.GetRunningObjectsOfType(); foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs) { if (doc.FullName.ToLower() == strArtifactLocation.ToLower()) { try { bool bIsTheOnlyDocumentInApp = true; // (doc.Application.Documents.Count == 1); // TO DO - not working if (doc.Application.Visible && bIsTheOnlyDocumentInApp) { doc.Application.Visible = false; return FuncDepBoolType.Success; } else return FuncDepBoolType.Failed; } catch (Exception e) { string message = e.Message; message = message + ""; // TODO: Log Something here } break; } } return FuncDepBoolType.ParametersInvalid; } #endregion } }