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 can not 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 ivprove 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 have?
If the Office application you are automating continues to reside in memory after your Visual C program finishes executing, the most likely cause is that you have not released an acquired interface For more information, see PRB:. Office Application Remains in Memory After Program Finishes ( Q238987)
How do i access my document print at an office document?
You can automate Microsoft Word, Excel or PowerPoint with Visual C to retrieve and manipulate both built-in and custom document properties For more information and an example, see HOWTO:. Use Visual C to Access DocumentProperties with Automation (Q238393).
You can also retrieve document property information without Automation and even without the need for the Office application that created the file. Office documents are OLE compound documents that store document properties in persistent property sets. These property sets are managed by COM / OLE and can be retrieved using the IPropertySetStorage and IPropertyStorage interfaces For details, see HOWTO:. Read Compound Document Properties Directly with VC (Q186898) and Dsofile.exe Lets You Edit Office Document Properties from Visual Basic and Active Server Pages (Q224351) .How can I automate an EMBEDDED Microsoft Office Document?
To automate an embedded Office document, you must first obtain the IDispatch pointer for the document object. Once you've obtained the IDispatch pointer to the document object, you can automate the server by calling the appropriate methods and properties for that server. For some Examples, See Howto: Embed and Automate A Microsoft Excel Worksheet with MFC (Q184663) and howto: Embed and Automate A Word Document with MFC (Q238611).
MY Automation Client for an Office Application Runs WITHOUT ERROR EXCEPT WHEN I Run My Client from an NT Service.
Microsoft Office is designed for use on client systems as end-user products and has not been optimized to run from Windows NT Services. Automating an Office application from a service is not recommended or supported. For details on problems that you might encounter automating an Office Application from a Service and Common Causes of Those Problems, please See Info: Consideration for Server-Side Automation Of Office (Q257757).
Visual C / MFC-INFORMATION AND SAMPLE CODE
This section contains information and links to code samples for Office Automation using Visual C and MFC. Any one of the articles listed below may illustrate Automation with a specific version of Microsoft Office. In most cases, you can follow the step-by-step instructions in the article and run the sample without needing to modify the code. However, if you are using one of the code samples created with a version of Office earlier than the one you are automating, you may need to modify the code to account for new Arguments of updated methods. for more information, see the knowledge base article type libraries for Office May Change with new release (Q224925) .General Concepts
Create Automation Project Using MFC and A Type Library (Q178749)
Locating resources to study automation (q152023)
Find the path and version of an office application from Visual C (Q247985)
Use Ole Automation from A C Application Rather Than C (Q181473)
Create An Automation Client Project Using ATL (Q196135)
Do 16-Bit Automation In C Using VC 1.52 (Q194656)
OLE / COM Concepts
Descriptions and Workings of Ole Threading Models (Q150777)
Ole Threads Must Dispatch Messages (Q136885)
Marshal.exe: How to Marshal Interfaces Across Apartments (Q206076)
Com security frequently asked qustions (Q158508)
TN039: MFC / OLE Automation Implementation (MFC Technical Note)
Microsoft Excel Automation
Use mfc to automate excel and create / format a new workbook (Q179706)
Embed and automate a Microsoft Excel Worksheet with MFC (Q184663)
Use mfc to automate Excel and Fill A Range with an array (Q186120)
Use mfc to automate excel and obtain an Array from a Range (Q186122)
Automate Excel Using MFC and Worksheet Functions (Q178781) Use MFC to Copy A Dao RecordSet To Excel with Automation (Q243394)
Use mfc to create a Microsoft Excel Chart (Q178783)
Catch Microsoft Excel Application Events Using VC (Q186427)
Automate Excel and Then Know The User Closed It (Q192348)
Use mfc to automate Excel and navigate Worksheets (Q178782)
Automate Using VC To Save Excel Worksheet As HTML File (Q199691)
Convert Indices (Row, Col) Into Excel-Style A1: C1 Strings (Q198112)
Automate Excel from C without using mfc or #import (Q216686)
COMEXCEL: Demonstrates An Automation Client Application With Compiler COM Support