initial oogynize check in _ this actually used to work!

This commit is contained in:
2016-02-14 21:16:31 -08:00
parent b183af5d55
commit 532ea133bc
337 changed files with 30692 additions and 0 deletions

View File

@@ -0,0 +1,324 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foo.AddIn.Common;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;
// Ignore 'Ambiguity' warning message, seems like no way around those
#pragma warning disable 0467
namespace Foo.Addin.Office
{
public class Access_Workspace : IWorkspace
{
public const string Access_ProgId = "Access.Application";
public FuncRetVal Launch(string strArtifactLocation, int WindowHeight, int WindowWidth, int WindowTop, int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Access.Application app = new Microsoft.Office.Interop.Access.Application();
app.Visible = true;
// Must test the following here:
// - open both .mdb and .adp (access project files with this)
// - how does this behave when db is password protected?
app.OpenCurrentDatabase(strArtifactLocation, true, "");
// ToDo: Check if a new process got created? / or if hWnd exists???
// - Also PERFORM Window Positioning
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Launch(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Access.Application app = new Microsoft.Office.Interop.Access.Application();
app.Visible = true;
// Must test the following here:
// - open both .mdb and .adp (access project files with this)
// - how does this behave when db is password protected?
app.OpenCurrentDatabase(strArtifactLocation, true, "");
// ToDo: Check if a new process got created? / or if hWnd exists???
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Show(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningAccessApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Access.Application>();
foreach (Microsoft.Office.Interop.Access.Application app in RunningAccessApps)
{
if (app.CurrentProject.FullName.ToLower() == strArtifactLocation.ToLower())
{
IntPtr hWnd = (IntPtr)app.hWndAccessApp();
if (!Functions.IsWindowVisible(hWnd))
{
Functions.ShowWindow(hWnd, Functions.FuncEnum.WindowAction.SW_SHOW);
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Hide(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningAccessApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Access.Application>();
foreach (Microsoft.Office.Interop.Access.Application app in RunningAccessApps)
{
if (app.CurrentProject.FullName.ToLower() == strArtifactLocation.ToLower())
{
IntPtr hWnd = (IntPtr)app.hWndAccessApp();
if (Functions.IsWindowVisible(hWnd))
{
Functions.ShowWindow(hWnd, Functions.FuncEnum.WindowAction.SW_HIDE);
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal QueryClose(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningAccessApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Access.Application>();
foreach (Microsoft.Office.Interop.Access.Application app in RunningAccessApps)
{
if (app.CurrentProject.FullName.ToLower() == strArtifactLocation.ToLower())
{
IntPtr hWnd = (IntPtr) app.hWndAccessApp();
if (Functions.SendMessage(hWnd, Functions.FuncEnum.WindowMessage.WM_QUERYENDSESSION, IntPtr.Zero, IntPtr.Zero) != 0)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact, out int WindowHeight, out int WindowWidth, out int WindowTop, out int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
WindowTop = 0;
WindowLeft = 0;
WindowWidth = 0;
WindowHeight = 0;
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningAccessApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Access.Application>();
foreach (Microsoft.Office.Interop.Access.Application app in RunningAccessApps)
{
if (app.CurrentProject.FullName.ToLower() == strArtifactLocation.ToLower())
{
// Get Handle and Process
IntPtr hWnd = (IntPtr)app.hWndAccessApp();
Process process = Functions.GetProcessFromHandle((IntPtr)hWnd);
// Get the Window properties
Rectangle rect = Functions.GetWindowRect(hWnd);
WindowHeight = rect.Height;
WindowWidth = rect.Width;
WindowLeft = rect.Left;
WindowTop = rect.Top;
// Close the database
//app.CloseCurrentDatabase();
if (bAutoSaveArtifact)
app.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll);
else
app.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveNone);
Marshal.FinalReleaseComObject(app); // force clean-up
// Kill the process
process.Kill();
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningAccessApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Access.Application>();
foreach (Microsoft.Office.Interop.Access.Application app in RunningAccessApps)
{
if (app.CurrentProject.FullName.ToLower() == strArtifactLocation.ToLower())
{
// Get Handle and Process
IntPtr hWnd = (IntPtr)app.hWndAccessApp();
Process process = Functions.GetProcessFromHandle((IntPtr)hWnd);
// Close the database
//app.CloseCurrentDatabase();
if(bAutoSaveArtifact)
app.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll);
else
app.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveNone);
Marshal.FinalReleaseComObject(app); // force clean-up
// Kill the process
process.Kill();
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningAccessApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Access.Application>();
foreach (Microsoft.Office.Interop.Access.Application app in RunningAccessApps)
{
if (app.CurrentProject.FullName.ToLower() == strArtifactLocation.ToLower())
{
// Get Handle and Process
IntPtr hWnd = (IntPtr)app.hWndAccessApp();
Process process = Functions.GetProcessFromHandle((IntPtr)hWnd);
// Close the database
//app.CloseCurrentDatabase();
app.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll);
Marshal.FinalReleaseComObject(app); // force clean-up
// Kill the process
process.Kill();
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foo.AddIn.Common;
namespace Foo.Addin.Office
{
public class AddIn : IAddIn
{
}
}

View File

@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{0CA1DD2E-2752-4587-ADB4-77194411648B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Foo.Addin.Office</RootNamespace>
<AssemblyName>Foo.Addin.Office</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\Target\Debug\AddIns\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\Target\Release\AddIns\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Office.Interop.Access, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Office.Interop.PowerPoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Office.Interop.Publisher, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Office.Interop.Visio, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Office.Interop.Word, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Access.cs" />
<Compile Include="AddIn.cs" />
<Compile Include="Excel.cs" />
<Compile Include="PowerPoint.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Publisher.cs" />
<Compile Include="Visio.cs" />
<Compile Include="Word.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AddIn.Common\AddIn.Common.csproj">
<Project>{D32C4454-9334-47AA-9A3F-456B8B12220A}</Project>
<Name>AddIn.Common</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<COMReference Include="Microsoft.Office.Core">
<Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
<VersionMajor>2</VersionMajor>
<VersionMinor>3</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>False</Isolated>
</COMReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,383 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
using Foo.AddIn.Common;
using System.Drawing;
// Ignore 'Ambiguity' warning message, seems like no way around those
#pragma warning disable 0467
namespace Foo.Addin.Office
{
public class Excel_Workspace : IWorkspace
{
public const string Excel_ProgId = "Excel.Application";
public FuncRetVal Launch(string strArtifactLocation, int WindowHeight, int WindowWidth, int WindowTop, int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Excel.Application app = null;
app = new Microsoft.Office.Interop.Excel.Application();
// Mark the Application as visible
app.Visible = true;
// Position the Window
IntPtr hWnd = (IntPtr)app.Hwnd;
Functions.SetWindowPos(hWnd, IntPtr.Zero, WindowLeft, WindowTop, WindowHeight, WindowWidth, 0);
// Open/Load the Document
Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Open(strArtifactLocation, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
if (workbook != null)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
}
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Launch(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Excel.Application app = null;
app = new Microsoft.Office.Interop.Excel.Application();
// Mark the Application as visible
app.Visible = true;
Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Open(strArtifactLocation, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
if (workbook != null)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
}
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Show(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningExcelWorkbooks = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Excel.Workbook>();
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 For some reason the App is visible
if (app.Visible)
{
// Make Sure that this Workbook is activated
if (!bIsTheOnlyDocumentInApp)
{
book.Activate();
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
}
return retVal;
}
app.Visible = true;
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Hide(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningExcelWorkbooks = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Excel.Workbook>();
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)
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
// If this is NOT the Only open workbook in this App
// ~Activate the other workbook
if (!bIsTheOnlyDocumentInApp)
{
foreach (Microsoft.Office.Interop.Excel.Workbook book2 in app.Workbooks)
{
if (book2.FullName.ToLower() != strArtifactLocation.ToLower())
{
book2.Activate();
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
}
else
{
app.Visible = false;
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal QueryClose(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningExcelWorkbooks = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Excel.Workbook>();
foreach (Microsoft.Office.Interop.Excel.Workbook book in RunningExcelWorkbooks)
{
if (book.FullName.ToLower() == strArtifactLocation.ToLower())
{
if (book.Saved)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact, out int WindowHeight, out int WindowWidth, out int WindowTop, out int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
WindowTop = 0;
WindowLeft = 0;
WindowWidth = 0;
WindowHeight = 0;
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningExcelWorkbooks = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Excel.Workbook>();
foreach (Microsoft.Office.Interop.Excel.Workbook book in RunningExcelWorkbooks)
{
if (book.FullName.ToLower() == strArtifactLocation.ToLower())
{
// In case there is a running rogue excel process left open,
// we can always force it to close by closing the process
// int hWnd = app.Hwnd;
// Process process = Win32Functions.GetProcessFromHandle((IntPtr) hWnd);
var app = book.Application;
// Get the Window properties
IntPtr hWnd = (IntPtr)app.Hwnd;
Rectangle rect = Functions.GetWindowRect(hWnd);
WindowHeight = rect.Height;
WindowWidth = rect.Width;
WindowLeft = rect.Left;
WindowTop = rect.Top;
// Close the Workbook
book.Close(bAutoSaveArtifact, strArtifactLocation, false);
Marshal.FinalReleaseComObject(book); // force clean-up
// Close Excel if this is the last Workbook
if (app.Workbooks.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up, should clean up excel process
//process.Close();
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningExcelWorkbooks = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Excel.Workbook>();
foreach (Microsoft.Office.Interop.Excel.Workbook book in RunningExcelWorkbooks)
{
if (book.FullName.ToLower() == strArtifactLocation.ToLower())
{
// In case there is a running rogue excel process left open,
// we can always force it to close by closing the process
// int hWnd = app.Hwnd;
// Process process = Win32Functions.GetProcessFromHandle((IntPtr) hWnd);
var app = book.Application;
// Close the Workbook
book.Close(bAutoSaveArtifact, strArtifactLocation, false);
Marshal.FinalReleaseComObject(book); // force clean-up
// Close Excel if this is the last Workbook
if (app.Workbooks.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up, should clean up excel process
//process.Close();
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningExcelWorkbooks = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Excel.Workbook>();
foreach (Microsoft.Office.Interop.Excel.Workbook book in RunningExcelWorkbooks)
{
if (book.FullName.ToLower() == strArtifactLocation.ToLower())
{
// In case there is a running rogue excel process left open,
// we can always force it to close by closing the process
// int hWnd = app.Hwnd;
// Process process = Win32Functions.GetProcessFromHandle((IntPtr) hWnd);
var app = book.Application;
// Close the Workbook
book.Close(true, strArtifactLocation, false);
Marshal.FinalReleaseComObject(book); // force clean-up
// Close Excel if this is the last Workbook
if (app.Workbooks.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up, should clean up excel process
//process.Close();
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
}
}

View File

@@ -0,0 +1,379 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foo.AddIn.Common;
using System.Runtime.InteropServices;
// Ignore 'Ambiguity' warning message, seems like no way around those
#pragma warning disable 0467
namespace Foo.Addin.Office
{
public class PowerPoint_Workspace : IWorkspace
{
public const string PowerPoint_ProgId = "PowerPoint.Application";
public FuncRetVal Launch(string strArtifactLocation, int WindowHeight, int WindowWidth, int WindowTop, int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.PowerPoint.Application app = null;
app = Functions.GetCOMObject(PowerPoint_ProgId) as Microsoft.Office.Interop.PowerPoint.Application;
// If no existing PowerPoint App is open, open a new one
if (app == null)
{
app = new Microsoft.Office.Interop.PowerPoint.Application();
app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
}
// If there is an existing presentation, don't position the window
bool bThereIsAnExistingPresentation = (app.Presentations.Count > 0);
// Open the PowerPoint Presentation
Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
presentation = app.Presentations.Open(strArtifactLocation, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue);
if (presentation != null)
{
// Position the Window
if (!bThereIsAnExistingPresentation)
{
IntPtr hWnd = (IntPtr) app.HWND;
Functions.SetWindowPos(hWnd, IntPtr.Zero, WindowLeft, WindowTop, WindowHeight, WindowWidth, 0);
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
}
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Launch(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.PowerPoint.Application app = null;
app = Functions.GetCOMObject(PowerPoint_ProgId) as Microsoft.Office.Interop.PowerPoint.Application;
// If no existing PowerPoint App is open, open a new one
if (app == null)
{
app = new Microsoft.Office.Interop.PowerPoint.Application();
app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
}
// Open the PowerPoint Presentation
Microsoft.Office.Interop.PowerPoint.Presentation presentation = null;
presentation = app.Presentations.Open(strArtifactLocation, Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue);
if (presentation != null)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
}
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Show(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPowerPoints = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.PowerPoint.Presentation>();
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:
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
case Microsoft.Office.Core.MsoTriState.msoFalse:
{
presentation.Application.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
//Functions.ShowWindow((IntPtr)presentation.Application.HWND, (int)Functions.WindowAction.SW_SHOW);
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Hide(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPowerPoints = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.PowerPoint.Presentation>();
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;
//Functions.ShowWindow((IntPtr)presentation.Application.HWND, (int)Functions.WindowAction.SW_HIDE);
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
case Microsoft.Office.Core.MsoTriState.msoFalse:
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal QueryClose(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPowerPoints = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.PowerPoint.Presentation>();
foreach (Microsoft.Office.Interop.PowerPoint.Presentation presentation in RunningPowerPoints)
{
if (presentation.FullName.ToLower() == strArtifactLocation.ToLower())
{
switch (presentation.Saved)
{
case Microsoft.Office.Core.MsoTriState.msoTrue:
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
case Microsoft.Office.Core.MsoTriState.msoFalse:
{
retVal.Type = FuncRetValEnum.Action_Failed;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact, out int WindowHeight, out int WindowWidth, out int WindowTop, out int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
WindowTop = 0;
WindowLeft = 0;
WindowWidth = 0;
WindowHeight = 0;
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPowerPoints = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.PowerPoint.Presentation>();
foreach (Microsoft.Office.Interop.PowerPoint.Presentation presentation in RunningPowerPoints)
{
if (presentation.FullName.ToLower() == strArtifactLocation.ToLower())
{
var app = presentation.Application;
// Save this Presentation
if (bAutoSaveArtifact)
presentation.Save();
// Pass out the Window Information
WindowTop = (int) presentation.Application.Top;
WindowLeft = (int) presentation.Application.Left;
WindowHeight = (int) presentation.Application.Height;
WindowWidth = (int)presentation.Application.Width;
// Close this Presentation
presentation.Close();
Marshal.FinalReleaseComObject(presentation); // force clean-up
// Close PowerPoint if there are no more Presentations
if (app.Presentations.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up, kill application for sure
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPowerPoints = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.PowerPoint.Presentation>();
foreach (Microsoft.Office.Interop.PowerPoint.Presentation presentation in RunningPowerPoints)
{
if (presentation.FullName.ToLower() == strArtifactLocation.ToLower())
{
var app = presentation.Application;
// Save this Presentation
if(bAutoSaveArtifact)
presentation.Save();
// Close this Presentation
presentation.Close();
Marshal.FinalReleaseComObject(presentation); // force clean-up
// Close PowerPoint if there are no more Presentations
if (app.Presentations.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up, kill application for sure
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPowerPoints = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.PowerPoint.Presentation>();
foreach (Microsoft.Office.Interop.PowerPoint.Presentation presentation in RunningPowerPoints)
{
if (presentation.FullName.ToLower() == strArtifactLocation.ToLower())
{
var app = presentation.Application;
// Save this Presentation
presentation.Save();
// Close this Presentation
presentation.Close();
Marshal.FinalReleaseComObject(presentation); // force clean-up
// Close PowerPoint if there are no more Presentations
if (app.Presentations.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up, kill application for sure
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Addin.Office")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Addin.Office")]
[assembly: AssemblyCopyright("Copyright © 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("79a22796-d344-456e-90a3-9e28c0f31d31")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,361 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foo.AddIn.Common;
using System.Diagnostics;
// Ignore 'Ambiguity' warning message, seems like no way around those
#pragma warning disable 0467
namespace Foo.Addin.Office
{
public class Publisher_Workspace : IWorkspace
{
public const string Publisher_ProgId = "Publisher.Application";
public FuncRetVal Launch(string strArtifactLocation, int WindowHeight, int WindowWidth, int WindowTop, int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Publisher.Application app = new Microsoft.Office.Interop.Publisher.Application();
// Opens Publisher
Microsoft.Office.Interop.Publisher.Document doc = null;
doc = app.Open(strArtifactLocation, false, false, Microsoft.Office.Interop.Publisher.PbSaveOptions.pbSaveChanges);
// Position the Window
IntPtr hWnd = (IntPtr)doc.ActiveWindow.Hwnd;
Functions.SetWindowPos(hWnd, IntPtr.Zero, WindowLeft, WindowTop, WindowHeight, WindowWidth, 0);
if (doc != null)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
}
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Launch(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Publisher.Application app = new Microsoft.Office.Interop.Publisher.Application();
// Opens Publisher
Microsoft.Office.Interop.Publisher.Document doc = null;
doc = app.Open(strArtifactLocation, false, false, Microsoft.Office.Interop.Publisher.PbSaveOptions.pbSaveChanges);
if (doc != null)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
}
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Show(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPublisherApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Publisher.Application>();
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())
{
if (pubDoc.ActiveWindow.Visible)
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
else
{
pubDoc.ActiveWindow.Visible = true;
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Hide(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPublisherApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Publisher.Application>();
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())
{
if (!pubDoc.ActiveWindow.Visible)
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
else
{
pubDoc.ActiveWindow.Visible = false;
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal QueryClose(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPublisherApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Publisher.Application>();
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())
{
if (pubDoc.Saved)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact, out int WindowHeight, out int WindowWidth, out int WindowTop, out int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
WindowTop = 0;
WindowLeft = 0;
WindowWidth = 0;
WindowHeight = 0;
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPublisherApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Publisher.Application>();
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
if (bAutoSaveArtifact)
pubDoc.Save();
// Get the Window properties
WindowTop = pubDoc.ActiveWindow.Top;
WindowLeft = pubDoc.ActiveWindow.Left;
WindowWidth = pubDoc.ActiveWindow.Width;
WindowHeight = pubDoc.ActiveWindow.Height;
// Get the Handle
IntPtr hWnd = (IntPtr)pubDoc.ActiveWindow.Hwnd;
Process process = Functions.GetProcessFromHandle(hWnd);
// Send Close Message
if (Functions.SendMessage((IntPtr)hWnd, Functions.FuncEnum.WindowMessage.WM_CLOSE, IntPtr.Zero, IntPtr.Zero) == 0)
{
process.Kill();
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPublisherApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Publisher.Application>();
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
if(bAutoSaveArtifact)
pubDoc.Save();
// Get the Handle
IntPtr hWnd = (IntPtr)pubDoc.ActiveWindow.Hwnd;
Process process = Functions.GetProcessFromHandle(hWnd);
// Send Close Message
if (Functions.SendMessage((IntPtr)hWnd, Functions.FuncEnum.WindowMessage.WM_CLOSE, IntPtr.Zero, IntPtr.Zero) == 0)
{
process.Kill();
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningPublisherApps = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Publisher.Application>();
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
IntPtr hWnd = (IntPtr)pubDoc.ActiveWindow.Hwnd;
Process process = Functions.GetProcessFromHandle(hWnd);
// Send Close Message
if (Functions.SendMessage((IntPtr)hWnd, Functions.FuncEnum.WindowMessage.WM_CLOSE, IntPtr.Zero, IntPtr.Zero) == 0)
{
process.Kill();
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
}
}

View File

@@ -0,0 +1,320 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foo.AddIn.Common;
using System.Runtime.InteropServices;
// Ignore 'Ambiguity' warning message, seems like no way around those
#pragma warning disable 0467
namespace Foo.Addin.Office
{
public class Visio_Workspace : IWorkspace
{
public const string Visio_ProgId = "Visio.Application";
public FuncRetVal Launch(string strArtifactLocation, int WindowHeight, int WindowWidth, int WindowTop, int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Visio.Application app = null;
app = new Microsoft.Office.Interop.Visio.Application();
// Mark the Application as visible
app.Visible = true;
app.Documents.Open(strArtifactLocation);
// ToDo: Check if a new process got created? / or if hWnd exists???
// - Also PERFORM Window Positioning
app.ActiveWindow.SetWindowRect(WindowLeft, WindowTop, WindowWidth, WindowHeight);
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Launch(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Visio.Application app = null;
app = new Microsoft.Office.Interop.Visio.Application();
// Mark the Application as visible
app.Visible = true;
app.Documents.Open(strArtifactLocation);
// Keep Track of all our Excel Instances -- not needed in visio?
///WorkspaceState.Launched_ExcelInstances.Add(app);
retVal.Type = FuncRetValEnum.Action_Succeeded;
//if (!String.IsNullOrEmpty(strArtifactLocation) && (strArtifactLocation.Length > 3))
//{
// Process.Start(strArtifactLocation);
//}
//return FuncDepBoolType.ParametersInvalid;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Show(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningVisioDocs = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Visio.Document>();
foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
// TO DO - (doc.Application.Documents.Count == 1); not working, why?!?!? odd
bool bIsTrue = (doc.Application.Documents.Count == 1); // not working, why?!?!? odd
//bool bIsTheOnlyDocumentInApp = true;
if (!doc.Application.Visible) // && bIsTheOnlyDocumentInApp)
{
doc.Application.Visible = true;
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Hide(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningVisioDocs = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Visio.Document>();
foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
// TO DO - (doc.Application.Documents.Count == 1); not working, why?!?!? odd
bool bIsTrue = (doc.Application.Documents.Count == 1); // not working, why?!?!? odd
//bool bIsTheOnlyDocumentInApp = true;
if (doc.Application.Visible) // && bIsTheOnlyDocumentInApp)
{
doc.Application.Visible = false;
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal QueryClose(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningVisioDocs = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Visio.Document>();
foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
if (doc.Saved)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact, out int WindowHeight, out int WindowWidth, out int WindowTop, out int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
WindowTop = 0;
WindowLeft = 0;
WindowWidth = 0;
WindowHeight = 0;
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningVisioDocs = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Visio.Document>();
foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
if (bAutoSaveArtifact)
doc.Save();
var app = doc.Application;
// Get the Window properties
app.ActiveWindow.GetWindowRect(out WindowLeft, out WindowTop, out WindowWidth, out WindowHeight);
doc.Close();
Marshal.FinalReleaseComObject(doc); // Force Clean-up
if (app.Documents.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningVisioDocs = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Visio.Document>();
foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
if(bAutoSaveArtifact)
doc.Save();
var app = doc.Application;
doc.Close();
Marshal.FinalReleaseComObject(doc); // Force Clean-up
if (app.Documents.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningVisioDocs = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Visio.Document>();
foreach (Microsoft.Office.Interop.Visio.Document doc in RunningVisioDocs)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
doc.Save();
var app = doc.Application;
doc.Close();
Marshal.FinalReleaseComObject(doc); // Force Clean-up
if (app.Documents.Count == 0)
{
app.Quit();
Marshal.FinalReleaseComObject(app); // force clean-up
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
}
}

424
AddIns/Addin.Office/Word.cs Normal file
View File

@@ -0,0 +1,424 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foo.AddIn.Common;
using System.Runtime.InteropServices;
using System.Reflection;
// Ignore 'Ambiguity' warning message, seems like no way around those
#pragma warning disable 0467
namespace Foo.Addin.Office
{
public class Word_Workspace : IWorkspace
{
public const string Word_ProgId = "Word.Application";
public FuncRetVal Launch(string strArtifactLocation, int WindowHeight, int WindowWidth, int WindowTop, int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Word.Application app = null;
app = Functions.GetCOMObject(Word_ProgId) as Microsoft.Office.Interop.Word.Application;
// If no existing Word App is open, open a new one
if (app == null)
{
app = new Microsoft.Office.Interop.Word.Application();
app.Visible = true;
}
object fileName = strArtifactLocation;
object missing = Missing.Value;
Microsoft.Office.Interop.Word.Document doc = null;
doc = app.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (doc != null)
{
// Find the correct Window and position it
foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
{
if (window.Document.FullName.ToLower() == strArtifactLocation.ToLower())
{
window.Top = WindowTop;
window.Left = WindowLeft;
window.Height = WindowHeight;
window.Width = WindowWidth;
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
}
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Launch(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
Microsoft.Office.Interop.Word.Application app = null;
app = Functions.GetCOMObject(Word_ProgId) as Microsoft.Office.Interop.Word.Application;
// If no existing Word App is open, open a new one
if (app == null)
{
app = new Microsoft.Office.Interop.Word.Application();
app.Visible = true;
}
object fileName = strArtifactLocation;
object missing = Missing.Value;
Microsoft.Office.Interop.Word.Document doc = null;
doc = app.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (doc != null)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
}
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Show(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningWordDocuments = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Word.Document>();
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;
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Hide(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningWordDocuments = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Word.Document>();
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;
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.NoAction_Needed;
return retVal;
}
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal QueryClose(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningWordDocuments = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Word.Document>();
foreach (Microsoft.Office.Interop.Word.Document doc in RunningWordDocuments)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
if (doc.Saved)
{
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
else
{
retVal.Type = FuncRetValEnum.Action_Failed;
return retVal;
}
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact, out int WindowHeight, out int WindowWidth, out int WindowTop, out int WindowLeft)
{
FuncRetVal retVal = new FuncRetVal();
WindowTop = 0;
WindowLeft = 0;
WindowWidth = 0;
WindowHeight = 0;
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningWordDocuments = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Word.Document>();
foreach (Microsoft.Office.Interop.Word.Document doc in RunningWordDocuments)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
// Found the document to close * Close it Without Prompting *
object saveOption = null;
if (bAutoSaveArtifact)
saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
else
saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
var app = doc.Application;
// Let's get the Window that belongs to this document
Microsoft.Office.Interop.Word.Window docWindow = null;
foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
{
if (window.Document.FullName.ToLower() == strArtifactLocation.ToLower())
docWindow = window;
}
// Get the Window properties
WindowTop = docWindow.Top;
WindowLeft = docWindow.Left;
WindowWidth = docWindow.Width;
WindowHeight = docWindow.Height;
//Object missing = Missing.Value;
doc.Close(ref saveOption, ref originalFormat, ref routeDocument);
Marshal.FinalReleaseComObject(doc); // force clean-up
// Close the Window
if (docWindow != null)
docWindow.Close(ref saveOption, ref routeDocument);
// Close Word if this is the Last Document Instance
if (app.Documents.Count == 0)
{
app.Quit(ref saveOption, ref originalFormat, ref routeDocument);
Marshal.FinalReleaseComObject(docWindow);
Marshal.FinalReleaseComObject(app); // force clean-up, closes Process for sure
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation, bool bAutoSaveArtifact)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningWordDocuments = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Word.Document>();
foreach (Microsoft.Office.Interop.Word.Document doc in RunningWordDocuments)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
// Found the document to close * Close it Without Prompting *
object saveOption = null;
if (bAutoSaveArtifact)
saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
else
saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
var app = doc.Application;
// Let's get the Window that belongs to this document
Microsoft.Office.Interop.Word.Window docWindow = null;
foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
{
if (window.Document.FullName.ToLower() == strArtifactLocation.ToLower())
docWindow = window;
}
//Object missing = Missing.Value;
doc.Close(ref saveOption, ref originalFormat, ref routeDocument);
Marshal.FinalReleaseComObject(doc); // force clean-up
// Close the Window
if (docWindow != null)
docWindow.Close(ref saveOption, ref routeDocument);
// Close Word if this is the Last Document Instance
if (app.Documents.Count == 0)
{
app.Quit(ref saveOption, ref originalFormat, ref routeDocument);
Marshal.FinalReleaseComObject(docWindow);
Marshal.FinalReleaseComObject(app); // force clean-up, closes Process for sure
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
public FuncRetVal Close(string strArtifactLocation)
{
FuncRetVal retVal = new FuncRetVal();
if (!Validate.IsValidArtifactLocation(strArtifactLocation, ref retVal))
return retVal;
try
{
var RunningWordDocuments = Functions.GetRunningObjectsOfType<Microsoft.Office.Interop.Word.Document>();
foreach (Microsoft.Office.Interop.Word.Document doc in RunningWordDocuments)
{
if (doc.FullName.ToLower() == strArtifactLocation.ToLower())
{
// Found the document to close * Close it Without Prompting *
object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
var app = doc.Application;
// Let's get the Window that belongs to this document
Microsoft.Office.Interop.Word.Window docWindow = null;
foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
{
if (window.Document.FullName.ToLower() == strArtifactLocation.ToLower())
docWindow = window;
}
//Object missing = Missing.Value;
doc.Close(ref saveOption, ref originalFormat, ref routeDocument);
Marshal.FinalReleaseComObject(doc); // force clean-up
// Close the Window
if (docWindow != null)
docWindow.Close(ref saveOption, ref routeDocument);
// Close Word if this is the Last Document Instance
if (app.Documents.Count == 0)
{
app.Quit(ref saveOption, ref originalFormat, ref routeDocument);
Marshal.FinalReleaseComObject(docWindow);
Marshal.FinalReleaseComObject(app); // force clean-up, closes Process for sure
}
retVal.Type = FuncRetValEnum.Action_Succeeded;
return retVal;
}
}
retVal.Type = FuncRetValEnum.ArtifactUnavailable;
}
catch (Exception e)
{
retVal.Message = e.Message;
retVal.Type = FuncRetValEnum.ErrorThrown;
}
return retVal;
}
}
}