如何使用C#访问当前关注的Excel工作簿 [英] How to access currently focused Excel Workbook using C#
问题描述
我正在创建一个应用程序,该应用程序使用全局热键从当前关注的应用程序中复制所选文本.我的应用程序在后台运行使用WinProc方法监视剪贴板.
问题是从Excel电子表格复制文本,当运行多个excel应用程序的Worhbooks时,它总是复制打开的第一个工作簿的选定文本.
我已经搜索了很多有关如何访问不同工作簿或访问当前重点工作簿的内容,但是没有一种方法适合我.
我想使用这种情况:
1)用户打开一个新的Excel工作簿(例如WorkBook1).
2)用户按下热键(例如CTRL + ALT + V),(工作正常)
3)用户打开另一个新的Excel工作簿(例如WorkBook2).
4)用户按热键,聚焦新工作簿. (不起作用,它正在复制工作簿的第一个实例中的选定文本).
我使用以下代码获取Excel应用程序:
I am creating an application which uses global hotkeys to copy selected text from currently focused application. My application runs in background & monitors clipboard using WinProc method.
The problem is copying text from Excel spreadsheet, when multiple Worhbooks of excel application are running, it always copy selected text of first workbook opened.
I have searched a lot regarding how to access different workbooks, or access currently focused workbook, but none of the method worked for me.
I want to use this case scenario:
1) User opens a new Excel Workbook (say WorkBook1).
2) User press hotkey (say CTRL+ALT+V), (Working fine)
3) User opens another new Excel workbook (say WorkBook2).
4) User press hotkey, focusing new workbook. (not working, it is copying selected text from first instance of workbook).
I used the following code to get Excel Application:
Excel.Application objExcel;
objExcel = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
...
使用
using
objExcel.ActiveWorkbook.Name
总是给我第一个打开的工作簿的名称.
我只需要使用C#代码访问当前活动工作簿中的选定文本.
谁能帮我解决这个问题...
在此先感谢您.
always gives me the name of first workbook opened.
I only need to access selected text from current active workbook using C# code.
Can anyone please help me solving this problem...
Thanks in advance.
推荐答案
检查此链接:
http://stackoverflow.com/questions/1956830/a-way-to-update-the-active-sheet-from-the-active-excel-workbook-using-c [
check this link:
http://stackoverflow.com/questions/1956830/a-way-to-update-the-active-sheet-from-the-active-excel-workbook-using-c[^]
这篇关于如何使用C#访问当前关注的Excel工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!