English 中文(简体)
与微软的Interop 准入与用户会议没有互动
原标题:Interop with Microsoft Access does not interact with User s session

我正在从Microsoft Access数据库中注入一部VBA代码。 净额。

这只是一条单一的法典,它运行着Macro。 所有的Macro都在一个模块内操作了VBA代码。

The issue I am having is that this all happens in a new MSAccess session, which I can t even see, instead of the session the user currently has open.

相反,这能否与目前的MSAccess会议用户互动? 整个要点是,在MSAccess会议内部就“网络”活动的每一场活动开放一种特殊形式。 我的C#守则如下:

using Microsoft.Office.Interop.Access;

var msAccess = new Application();

msAccess.OpenCurrentDatabase(@"x:fooar.accdb", false);
msAccess.DoCmd.RunMacro("macCTI");
msAccess.CloseCurrentDatabase();

成就

最佳回答

如果您有一个单一的无障碍环境,可使用Marshal.GetActiveObject:>

using Microsoft.Office.Interop.Access;
using System.Runtime.InteropServices
...
try
{
    var msAccess = (Application)Marshal.GetActiveObject("Access.Application");
    msAccess.DoCmd.RunMacro("macCTI");
}
catch (COMException ex)
{
    // handle error
}

或者,如果不止一个正在运行,而且没有两个情况有相同的数据库,那么你可以使用<<>Marshal.Bind Toiker <<>:<

var msAccess = (Application) Marshal.BindToMoniker(@"x:fooar.accdb"); 

<>><>>说明: http://support.microsoft.com/kb/316126“rel=“nofollow” 微软知识基础: 必须指出:

Whether a COM server is Single Use (Multiple Instances) or Multiuse (Single Instance) might affect your decision to use GetActiveObject to get reference to that server. Because potentially more than one instance of Word, Excel, or Microsoft Access can be running, GetActiveObject on a particular server may return an instance that you did not expect. The instance that is first registered in the ROT is typically the instance that is returned by GetActiveObject. If you want to get an Automation Reference to a specific running instance of Word, Excel, or Microsoft Access, use BindToMoniker with the name of the file that is opened in that instance. For a Multiuse (Single Instance) server like PowerPoint, it does not matter, because the automation reference points to the same running instance.

问题回答

To not get a new Access instance you will have to see if an instance already exists. Use Marshal.GetActiveObject as Matt suggested:

Application GetAccessApplication() {
  var application = (Application) Marshal.GetActiveObject("Access.Application");
  return application ?? new Application();
}

通知说,这里存在某种脱节之处,因为你必须利用ProgID获得一个现有准入案例,但创建一种新案例,请你使用中间议会。

之后,你可以发现“无障碍”申请:

var msAccess = GetAccessApplication();
msAccess.Visible = true;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签