用vc开发office自动化入门文档(转载msdn)

原创 2004年09月20日 23:11:00

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?

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).

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 Questions (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

 

VC++之Office自动化开发

去年底老板临时交给一个任务,为一个试验数据管理系统编写一个电子报告生成程序。因为老板也不怎么重视这个项目(钱少啊,呵呵),所以这个任务只好落在我这种小硕身上了。我也是两眼瞎,万事从头干。三个月的苦斗,...
  • Augusdi
  • Augusdi
  • 2013年10月24日 15:01
  • 4639

VC之Office自动化开发(一)

    去年底老板临时交给一个任务,为一个试验数据管理系统编写一个电子报告生成程序。因为老板也不怎么重视这个...
  • sgdgoodboy
  • sgdgoodboy
  • 2008年02月18日 13:23
  • 5146

Office自动化

摘要   只为方便平时工作,干点有价值含量的事,重复的让计算机去做。office自动化选择,一些资料。 一、技术选型 日常生活工作中,对数据(信息)处理经常用到oiifce套件,除去套件初始功能...
  • u012712186
  • u012712186
  • 2016年11月22日 16:17
  • 884

如何嵌入并自动化 Word 文档的 MFC-微软官方文档

概要 使用 OLE 嵌入,其他应用程序文档中嵌入的文档可以修改自动化通过双击嵌入的文档以将其激活"编辑"或"打开"中模式。 本文演示如何嵌入并自动化 Microsoft Office Word 文...
  • zuoyefeng1990
  • zuoyefeng1990
  • 2016年08月23日 14:05
  • 478

Office 自动化---准备

Office 自动化---准备 引言 笔者在使用C++编写Word和Excel导出报告的时候,由于网上相应的资料很少,所以走了许多弯路。在自己探索尝试了之后,积累一些开发的经验,写成这个系列的博文...
  • Stan1989
  • Stan1989
  • 2013年05月02日 22:56
  • 956

使用 Visual C++ 使 Office 自动化

概要 自动化和 COM 从 Visual C++ 使用自动化的三种方法 如何使用 Office 类型库 使嵌入的 Office 组件自动运行 参考 一、概要您可以使用自动化功能从 Visual C++...
  • guanchanghui
  • guanchanghui
  • 2006年07月28日 00:18
  • 1921

VC之office自动化开发

去年底老板临时交给一个任务,为一个试验数据管理系统编写一个电子报告生成程序。因为老板也不怎么重视这个项目(钱少啊,呵呵),所以这个任务只好落在我这种小硕身上了。我也是两眼瞎,万事从头干。三个月的苦斗,...
  • anbaixiu
  • anbaixiu
  • 2017年08月07日 15:05
  • 149

VC之Office自动化开发

去年底老板临时交给一个任务,为一个试验数据管理系统编写一个电子报告生成程序。因为老板也不怎么重视这个项目(钱少啊,呵呵),所以这个任务只好落在我这种小硕身上了。我也是两眼瞎,万事从头干。三个月的苦斗,...
  • Augusdi
  • Augusdi
  • 2011年05月15日 18:56
  • 1952

VC进行Office(word)编程

第一步:将word的接口类导入进来 如果是vc6.01、点View菜单中的ClassWizard(或按CTRL+W) 2. 选择Automation标签,点Add Class并选择"From a ty...
  • Augusdi
  • Augusdi
  • 2013年10月24日 15:25
  • 3556

使用VC++ ATL开发Office COM插件(二)

7、给插件添加菜单和按钮:   这里简单的介绍一下Office 的命令条:在Office中,菜单栏、工具栏和弹出式菜单都叫做命令条(Command Bar对象)。所有这些种类的命令条中都可以包括其他命...
  • wallaceli1981
  • wallaceli1981
  • 2007年08月31日 12:44
  • 2110
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:用vc开发office自动化入门文档(转载msdn)
举报原因:
原因补充:

(最多只允许输入30个字)