January 19, 2006
Late-Binding in C++...
Maybe someone had already the opportunity to implement late binding of an COM-object in VC++... you also found out that this is very complicated…
In VB(6/A) or VB-Script it looks pretty simple:
Dim wb As Object
Set wb = CreateObject("Word.Basic")
wb.AppShow
wb.FileNewDefault
wb.Insert "This is a test"
wb.FileSaveAs "c:sample.doc"
But in C++ this will bring you to several 100 lines of code ;-(
MS provided some helper functions to make the life easier. This was great, but the code had some bugs… after using it in several projects I now found some bugs and have a “corrected” version of this helper-functions available:
http://blog.kalmbachnet.de/files/InvHelp.zip
With this helper-functions you can make the same code in C++ only with a little bit more lines of code:
LPDISPATCH pdispWord;
CreateObject(OLESTR("Word.Basic"), &pdispWord);
Invoke(pdispWord, DISPATCH_METHOD, NULL, NULL, NULL,
OLESTR("AppShow"), NULL);
Invoke(pdispWord, DISPATCH_METHOD, NULL, NULL, NULL,
OLESTR("FileNewDefault"), NULL);
Invoke(pdispWord, DISPATCH_METHOD, NULL, NULL, NULL,
OLESTR("Insert"), TEXT("s"), (LPOLESTR)OLESTR("This is a test"));
Invoke(pdispWord, DISPATCH_METHOD, NULL, NULL, NULL,
OLESTR("FileSaveAs"), TEXT("s"), OLESTR("c:\sample.doc"));
pdispWord->Release();
Late-Binding is also the recommended way to automate office application if you want to target more than one office version.
The trackback url for this post is http://blog.kalmbachnet.de/bblog/trackback.php/63/
Comments have now been turned off for this post