发表于2004/9/20 23:11:00 1739人阅读
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?
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 properties in 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: Considerations for Server-Side Automation of Office (Q257757).
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).
Do 16-Bit Automation in C++ Using VC 1.52 (Q194656)
Microsoft Excel Automation
Augusdi 2013-10-24 15:01 4571
veryhehe2011 2012-11-03 16:56 675
u013580497 2015-09-30 18:04 753
Augusdi 2013-10-24 15:23 1632
MS Office 自动化编程（二）---Word文档的读写
leogray 2012-05-20 12:17 1959
MS Office 自动化编程（一）---从Word文档中获取文档属性
leogray 2012-05-19 20:21 3198
MS Office 自动化编程 - 从Word文档中获取文档属性
zuoyefeng1990 2016-08-23 13:48 309
VC++ Word文档 自动化技术
highyyy 2011-03-17 14:34 4942