http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoffdev/html/vsofficedev.asp
visual c++/mfc—frequently asked questions
how do i automate a microsoft office application from visual c++?
there are several ways you can control office applications through automation using visual c++:
- use mfc and the visual c++ classwizard to generate "wrapper classes" from the microsoft office type libraries.
- use "smart pointers" created with the #import directive from an office type library.
- use c/c++ to make direct calls to com services without the overhead of mfc or #import.
for more details, see info: using visual c++ to automate office (q238972).
how do i attach to the running instance of an office application?
to automate an office application that is already running, you can use the getactiveobject() api function to obtain the idispatch pointer. once you have the idispatch pointer for the running instance, you can then call its methods and properties. for additional information, see howto: attach to a running instance of an office application (q238975).
how do i pass optional parameters to methods and properties?
when you call a method that has optional parameters from visual c++, you cannot omit the optional parameters. instead, if the parameter type is a variant, you can pass a special variant for arguments you intend to omit. this variant has the type vt_error and a code member of disp_e_paramnotfound. for more details, see howto: passing optional parameters when calling a function (q238981).
how do i pass a coledispatchdriver as an argument for a method expecting a variant?
some methods require that you pass a variant that represents an automation object. with mfc, these objects are typically handled by coledispatchdriver-derived classes. to pass one of these to a method expecting a variant, you can create a new variant with its vt member set to vt_dispatch and its pdispval member set to the coledispatchdriver class' m_lpdispatch. for more details and a sample, please see howto: pass a coledispatchdriver as an argument for a method expecting a variant (q253501).
how do i catch events?
automation objects that can raise events implement a connection point interface. automation controllers can create "sinks" to "connect" with an automation object's connection point so that it receives event notifications. note that mfc's default implementation of idispatch::invoke does not support named arguments. therefore, with some automation servers—microsoft excel, for example—you must provide your own implementation of idispatch in the sink. for examples, see howto: catch microsoft word97 application events using vc++ (q183599) and howto: catch microsoft excel application events using vc++ (q186427).
how do i improve the performance of my automation code?
you can improve the performance of your automation code in visual c++ by using a two-dimensional variant safearray to read and write data all at once, and then using the clipboard to copy and paste data. for additional information, see howto: improving the performance of office automation code (q238984).
what do these huge error values, such as -2147352573 or 0x80030002, mean?
while automating an office application, you might receive a large error value, such as -2147221494. troubleshooting the error is greatly facilitated by determining the exact description of the error. to obtain a description of the error, you can use the error lookup utility provided with visual c++, call the formatmessage() api at run-time, or use the watch window to display the error message. these approaches are described in info: translating large office automation error values (q238986).
the application i'm automating stays in memory after my program is finished. what's happening?