我有一个窗口应用程序,我们在其中自动化MsOffice(word / excel / powerpoint / access)。 我们已经为word / excel / powerpoint编写过helper类,现在我们正在尝试为Access编写帮助类。
*其中,我们要将方法写入
1退出应用程序(访问),
2评估当前正在运行,
3应用程序(访问),
4 Getor创建应用程序(访问),
5运行应用程序(访问),
6关闭应用程序(访问),&等等,
What i have tried so far:
AccessHelper.cs
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using AccessInterop = Microsoft.Office.Interop.Access;
namespace TeTec.Action.Office2013.Access
{
public static class AccessHelper
{
#region Constants
private static readonly string _accessApplicationObjectStoreKey = "AccessApplication";
#endregion
#region Private methods
private static void QuitApplication(AccessInterop.Application accessApplication)
{
if (accessApplication != null)
{
try
{
accessApplication.DoCmd.SetWarnings(false);
accessApplication.Visible = false;
accessApplication.DoCmd.Quit();
Marshal.FinalReleaseComObject(accessApplication);
accessApplication = null;
GC.Collect();
GC.WaitForPendingFinalizers();
accessApplication.DoCmd.CloseDatabase();
accessApplication.DoCmd.Quit();
}
catch (Exception)
{
//Ignore
}
}
}
private static AccessInterop.Application GetOrCreateAccessApplication(IObjectStore objectStore)
{
AccessInterop.Application accessApplication = objectStore.Retrieve<AccessInterop.Application>(_accessApplicationObjectStoreKey);
if (accessApplication == null)
{
accessApplication = new AccessInterop.Application();
objectStore.Store(_accessApplicationObjectStoreKey, accessApplication);
}
return accessApplication;
}
#endregion
#region Internal methods
internal static void Cleanup(IObjectStore objectStore, bool terminate)
{
AccessInterop.Application accessApplication = objectStore.Retrieve<AccessInterop.Application>(_accessApplicationObjectStoreKey);
if (accessApplication != null)
{
QuitApplication(accessApplication);
objectStore.Remove(_accessApplicationObjectStoreKey);
}
if (terminate)
WinUtil.TerminateProcess("access.exe", null, null, true);
}
#endregion
#region Public methods
#region Basic operation
public static bool Run(IQuestion question)
{
AccessInterop.Application accessApplication = GetOrCreateAccessApplication(question.ObjectStore);
//AccessInterop.DataAccessPage accesPage = null;
try
{
//Avoid screen flickering or unwanted alerts while initializing
accessApplication.DoCmd.Echo(false);
accessApplication.DoCmd.SetWarnings(false);
//Create a document, if one does not exist already
//if (accessApplication.catlo == 0)
// accesPage = accessApplication.DataAccessPages.a;
//Activate Word application window
//accessApplication.Visible = true;
//accessApplication.ScreenUpdating = true;
//accessApplication.DisplayAlerts = displayAlertLevel;
//accessApplication.ActiveWindow.Activate();
////Setup application main window
//if (question.WorkArea != Rectangle.Empty)
//{
// accessApplication.WindowState = AccessInterop.WdWindowState.wdWindowStateNormal;
// accessApplication.ActiveWindow.Left = question.WorkArea.Left;
// accessApplication.ActiveWindow.Top = question.WorkArea.Top;
// accessApplication.ActiveWindow.Width = question.WorkArea.Width;
//}
//else
//{
// accessApplication.WindowState = AccessInterop.WdWindowState.wdWindowStateMaximize;
//}
return true;
}
catch (Exception)
{
Cleanup(question.ObjectStore, true);
return false;
}
}
public static void Close(IQuestion question)
{
Cleanup(question.ObjectStore, false);
}
#endregion
#endregion
}
}
GetOrCreateApplication&amp; CleanUpApplication无法正常工作。其中一种方法正在运行。
任何人都可以帮助我。
由于