无法获得正确的Excel实例以进行粘贴 [英] Can't get correct instance of Excel for paste

查看:119
本文介绍了无法获得正确的Excel实例以进行粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试访问最新的Excel实例,以便我可以粘贴数据。对我有用的是,如果Excel尚未打开,我打开它并可以将数据粘贴到我想要的位置。对我来说有用的是,如果有多个Excel实例打开。


我会解释:说我打开了3个Excel实例,每个实例都有一个工作簿。实例1 = Book1,实例2 = Book2,实例3 = Book3。我想将我的数据粘贴到Book3,因为那是最近打开的实例。这是我的代码:

Hi all,

I''m trying to access the most recent instance of Excel so I can paste data. What works for me is if Excel isn''t already opened, I open it and can paste the data where I want to. What isn''t working for me is if there are multiple instances of Excel open.

I''ll explain: Say I have 3 instances of Excel opened, each with one workbook. Instance 1 = Book1, instance 2 = Book2, and instance 3 = Book3. I want to paste my data to Book3, since that''s the most-recently-opened instance. Here''s my code:

展开 | 选择 | Wrap | 行号

推荐答案

你真的有三种不同吗? excel.exe正在运行,或者只有3个不同的窗口打开?

如果有3个Excel.exe正在运行,你可以检查它们的Process.StartTime是否为最新打开的?


我不熟悉你为元帅所做的事情,所以我查了一下:
Do you actually have three different excel.exe''s running, or are there just 3 different windows open?
If there were 3 Excel.exe''s running, you could examine the Process.StartTime of them for the newest opened?

I am unfamiliar with what you are doing for the Marshal, so I looked it up:

Marshal.GetActiveObject公开GetActiveObject COM API方法来自OLEAUT32.DLL;但是,后者需要类标识符(CLSID)而不是此方法所期望的编程标识符(ProgID)。要获取没有已注册ProgID的COM对象的运行实例,请使用平台调用来定义GetActivateObject COM方法。有关平台调用的说明,请参阅使用非托管DLL函数。有关GetActiveObject COM方法的其他信息,请参阅MSDN Library。
Marshal.GetActiveObject exposes the GetActiveObject COM API method from OLEAUT32.DLL; however, the latter expects a class identifier (CLSID) instead of the programmatic identifier (ProgID) expected by this method. To obtain a running instance of a COM object without a registered ProgID, use platform invoke to define the GetActivateObject COM method. For a description of platform invoke, see Consuming Unmanaged DLL Functions. For additional information about the GetActiveObject COM method, see the MSDN Library.



您可能必须使用P / Invoke

You may have to use the P/Invoke for this


我打开了3个独特的Excel实例(3个单独的条目在任务管理器中)。我知道如何在一个Excel实例中处理多个窗口。


我最初在我执行AppActivate之前在循环中查看Process.StartTime,但是在观察了ProcessIDs的顺序之后列出了,我看到最新的实例总是首先列出,然后是最近的一次,等等。这就是为什么我清理代码并且只是采用初始(0)实例。


正如我在上一篇文章中所述,我确实用AppActivate激活了正确的过程,因为那是弹出到前台的窗口(并且返回了所需的ProcID)。但是,当我尝试将该实例分配给Excel.Application对象时,它会分配最旧的(首先打开的)实例,而不是那个实际处于活动状态的实例。 Wacky。
I have 3 unique instances of Excel open (3 separate entries in Task Manager). I know how to handle multiple windows in 1 Excel instance.

I originally was looking at the Process.StartTime in a loop before I did my AppActivate, but after observing the order that the ProcessIDs were listed, I saw that the most recent instance was always listed first, and then the next-most-recent, etc. That''s why I cleaned up the code and am just taking the initial (0) instance.

As I stated in my previous note, I am indeed activating the correct process with AppActivate, since that''s the window that pops to the foreground (and the desired ProcID is returned). However, when I try to assign that instance to an Excel.Application object, it assigns the oldest (first-opened) instance instead, and not the one that''s actually active. Wacky.


嗯,你实际上没有对Marshall调用中的进程或进程ID做任何事情,你只需告诉它获取一个Excell.Application的实例,我的猜测将返回最老的实例(也就是第一个实例)。

我认为你必须找到一种方法来传递该excell实例的确切progid字符串,或者查看使用pinvoke打电话。


或者你可以使用msoffice.interop中内置的函数吗?
Well you don''t actually do anything with the process or process IDs in your Marshall call, you just tell it to get an instance of "Excell.Application", which my guess would be returns the oldest instance (aka the first instance).
I think you will have to find a way to pass in the exact progid string for that excell instance, or look into using the pinvoke call.

Or maybe there is some functions already built into the msoffice.interop you could use?


这篇关于无法获得正确的Excel实例以进行粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆