我需要测试是否可以从我的程序中使用Excel OLE,因为它可以在没有Excel的PC上启动.网上的代码示例假设安装了Excel,但如果没有,那该怎么办?
XLApp := CreateOleObject('Excel.Application');
try
// Hide Excel
XLApp.Visible := False;
// Open the Workbook
XLApp.Workbooks.Open(aPath);
...snip...
finally
// Quit Excel
if not VarIsEmpty(XLApp) then
begin
XLApp.Quit;
XLAPP := Unassigned;
end;
end;
Run Code Online (Sandbox Code Playgroud)
这是否是正确的代码,以找到是否安装了Excel?
//Try to create Excel OLE
try
XLApp := CreateOleObject('Excel.Application');
except
ShowMessage('Error opening Excel');
Exit;
end;
Run Code Online (Sandbox Code Playgroud) 我正在编写一个小型DLL组件,需要访问两个第三方组件来组合数据,其中一个仅为32位,另一个仅为64位.两者都使用TypeLib注册并且与Automation兼容,因此编组不应成为问题.
如果我正确理解了文档,那么除非组件也有AppID和DllSurrogate键,否则无法强制加载代理.由于两者都是第三方组件,我有点不愿意修改他们的注册.
有没有办法在理论上没有任何额外依赖关系的DLL组件的代理进程中激活组件中没有AppID的对象,或者任何人都可以向我解释为什么这会是一个坏主意?
我有一个完美的工作函数来查找和替换word文档中的文本变量.
HRESULT CMSWord::FindReplace( CString szVar, CString szText, bool bOnlyOnce/*=false*/ )
{
if(m_pWApp==NULL || m_pActiveDocument==NULL) return E_FAIL;
IDispatch *pDocApp;
{
VARIANT result;
VariantInit(&result);
OLEMethod(DISPATCH_PROPERTYGET, &result, m_pActiveDocument, L"Application", 0);
pDocApp= result.pdispVal;
}
IDispatch *pSelection;
{
VARIANT result;
VariantInit(&result);
OLEMethod(DISPATCH_PROPERTYGET, &result, pDocApp, L"Selection", 0);
pSelection=result.pdispVal;
}
IDispatch *pFind;
{
VARIANT result;
VariantInit(&result);
OLEMethod(DISPATCH_PROPERTYGET, &result, pSelection, L"Find", 0);
pFind=result.pdispVal;
}
OLEMethod(DISPATCH_METHOD, NULL, pFind, L"ClearFormatting",0);
szText.Replace(_T("\r\n"), _T("\v"));
COleVariant sVariable(szVar);
COleVariant sReplaceText(szText);
COleVariant replace((long)2);
COleVariant varBoolTrue;
varBoolTrue.boolVal = true;
COleVariant varBoolFalse;
varBoolFalse.boolVal = false;
COleVariant wdContinue((long)1); …Run Code Online (Sandbox Code Playgroud) 我设法提炼了我的问题中的根本问题之一如何跟踪下面单元中的OLE自动化对象的_AddRef/_Release调用.
我也会回答这个问题,以防其他人碰到这个问题.
问题:使用下面的代码,为什么WINWORD.EXE不会一直退出(有时它会退出).
该装置可能会被削减甚至更多.
unit Unit2;
interface
uses
Winapi.Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls,
WordXP;
type
TForm2 = class(TForm)
WordXPFailsToQuitButton: TButton;
procedure WordXPFailsToQuitButtonClick(Sender: TObject);
private
FWordApplication: TWordApplication;
strict protected
function GetWordApplication: TWordApplication; virtual;
function GetWordApplication_Documents: Documents; virtual;
procedure WordApplication_DocumentBeforeClose(ASender: TObject; const Doc: _Document; var Cancel: WordBool); virtual;
procedure WordApplication_Quit(Sender: TObject); virtual;
property WordApplication: TWordApplication read GetWordApplication;
property WordApplication_Documents: Documents read GetWordApplication_Documents;
end;
var
Form2: TForm2;
implementation
uses
Vcl.OleServer;
{$R *.dfm}
function TForm2.GetWordApplication: …Run Code Online (Sandbox Code Playgroud) 我想使用Python从Office/Excel文档中添加和提取文件.到目前为止添加东西很容易但是为了提取我还没有找到一个干净的解决方案.
为了清楚我已经得到了什么,我没有写下下面的小例子test.py并进一步解释.
test.py
import win32com.client as win32
import os
from tkinter import messagebox
import win32clipboard
# (0) Setup
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(dir_path + "\\" + "test_excel.xlsx")
ws = wb.Worksheets.Item(1)
objs = ws.OLEObjects()
# (1) Embed file
f = dir_path + "\\" + "test_txt.txt"
name = "test_txt_ole.txt"
objs.Add( Filename=f, IconLabel=name )
# (2) Access embedded file
obj = objs.Item(1) # Get single OLE from OLE list
obj.Copy()
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData(0xC004) # Binary …Run Code Online (Sandbox Code Playgroud) 我想使用Python编写一个应用程序脚本,该应用程序将自己宣传为提供OLE组件.我应该如何开始?
我还不知道我需要在我将要访问的COMponents上调用哪些方法.我应该使用win32com来加载这些组件,然后开始在IPython中按'tab'吗?
使用MINGW,我试图将我的C代码链接到执行一些OLE操作的静态C++库:
mingw32-gcc main.o resources.o -o mbcom.exe -L../../Lib/Iup -liup -liupole -lole32 -lcomctl32 -lstdc++ -mwindows
Run Code Online (Sandbox Code Playgroud)
不幸的是,我得到了这个:
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0x341): undefined reference to `IID_IOleControl'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0x9b0): undefined reference to `IID_IViewObject2'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xcb5): undefined reference to `IID_IUnknown'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xce8): undefined reference to `IID_IPersistStorage'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xd1b): undefined reference to `IID_IOleObject'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xd9d): undefined reference to `IID_IUnknown'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xdbf): undefined reference to `IID_IOleClientSite'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xde1): undefined reference to `IID_IOleWindow'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xe03): undefined reference to `IID_IOleControlSite'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xe25): undefined reference to `IID_IDispatch'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0xe51): undefined reference to `IID_IOleInPlaceSite'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0x1129): undefined reference to `GUID_NULL'
../../Lib/Iup/libiupole.a(tOleHandler.o):tOleHandler.cpp:(.text+0x124d): undefined reference to `GUID_NULL'
../../Lib/Iup/libiupole.a(tOleInPlaceSite.o):tOleInPlaceSite.cpp:(.text+0xc8): …Run Code Online (Sandbox Code Playgroud) 我想自动化Internet Explorer 8(在Windows 7上使用python 2.7)机器.这是我在SO上找到的帖子后的代码:
import sys, time
from win32com.client import WithEvents, Dispatch
import pythoncom
import threading
stopEvent=threading.Event()
class EventSink(object):
def OnNavigateComplete2(self,*args):
print "complete",args
stopEvent.set()
def waitUntilReady(ie):
if ie.ReadyState!=4:
while 1:
print "waiting"
pythoncom.PumpWaitingMessages()
stopEvent.wait(.2)
if stopEvent.isSet() or ie.ReadyState==4:
stopEvent.clear()
break;
if __name__ == '__main__':
time.clock()
ie=Dispatch('InternetExplorer.Application',EventSink)
ev=WithEvents(ie,EventSink)
ie.Visible=True
ie.AddressBar = True
ie.Navigate("http://www.sap.com/austria/index.epx")
waitUntilReady(ie)
Run Code Online (Sandbox Code Playgroud)
我收到了http://www.sap.com/austria/index.epx的以下错误消息:
waiting
waiting
Traceback (most recent call last):
File "C:\Users\w\My Documents\Aptana Studio 3 Workspace\MyApp\src\GoToIdeaWindow.py", line 41, in <module>
waitUntilReady(ie)
File …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个代码片段,它可以做到这一点,最好是在C#甚至Perl中.
我希望这不是一项大任务;)