winzenghua 发表于 2013-1-26 15:43:41

基于Visual C++2010 与office2010开发办公自动化(17)-使用自动化运行Word宏

office宏,译自英文单词Macro。宏是微软公司为其OFFICE软件包设计的一个特殊功能,软件设计者为了让人们在使用软件进行工作时,避免一再地重复相同的动作而设计出来的一种工具,它利用简单的语法,把常用的动作写成宏,当在工作时,就可以直接利用事先编好的宏自动运行,去完成某项特定的任务,而不必再重复相同的动作,目的是让用户文档中的一些任务自动化。
前面我们已经讲解了怎么用自动化运行Excel宏(见:http://blog.csdn.net/yincheng01/archive/2010/06/01/5639397.aspx)
这里我简单讲一下word中如何自动运行宏。
1.启动VS2010
http://hi.csdn.net/attachment/201005/29/0_12751226208PCC.gif
2.我们基于Visual C++ 新建一个CLR窗体工程,插入一个button,设置背景,如下图所示
http://hi.csdn.net/attachment/201006/13/0_1276430691bjJK.gif
添加下列引用
http://hi.csdn.net/attachment/201006/1/0_12753735677gRG.gif
插入下列代码,详细请见代码注释
#pragma oncenamespace yincheng {using namespace System;using namespace System::ComponentModel;using namespace System::Collections;using namespace System::Windows::Forms;using namespace System::Data;using namespace System::Drawing;/// <summary>/// Form1 摘要////// 警告: 如果更改此类的名称,则需要更改///          与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的///          “资源文件名”属性。否则,///          设计器将不能与此窗体的关联///          本地化资源正确交互。/// </summary>public ref class Form1 : public System::Windows::Forms::Form{public:Form1(void){InitializeComponent();////TODO: 在此处添加构造函数代码//}protected:/// <summary>/// 清理所有正在使用的资源。/// </summary>~Form1(){if (components){delete components;}}private: System::Windows::Forms::Button^button1;protected: private:/// <summary>/// 必需的设计器变量。/// </summary>System::ComponentModel::Container ^components;#pragma region Windows Form Designer generated code/// <summary>/// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </summary>void InitializeComponent(void){System::ComponentModel::ComponentResourceManager^resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));this->button1 = (gcnew System::Windows::Forms::Button());this->SuspendLayout();// // button1// this->button1->Location = System::Drawing::Point(131, 194);this->button1->Name = L"button1";this->button1->Size = System::Drawing::Size(299, 56);this->button1->TabIndex = 0;this->button1->Text = L"使用自动化运行Word示例宏";this->button1->UseVisualStyleBackColor = true;this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);// // Form1// this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;this->BackColor = System::Drawing::SystemColors::ActiveCaptionText;this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"$this.BackgroundImage")));this->ClientSize = System::Drawing::Size(543, 431);this->Controls->Add(this->button1);this->Name = L"Form1";this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;this->Text = L"演示使用自动化运行Word宏";this->ResumeLayout(false);}#pragma endregion//使用自动化运行Word示例宏private: System::Void button1_Click(System::Object^sender, System::EventArgs^e) {Microsoft::Office::Interop::Word::ApplicationClass^ MyWord;System::Object^ MyDoc;System::Object^ MyObj;    try{MyWord=gcnew Microsoft::Office::Interop::Word::ApplicationClass();    MyWord->Visible=true;            MyDoc=Application::StartupPath+"\\MyWordMacro.doc";MyObj=System::Reflection::Missing::Value;MyWord->Documents->Open(MyDoc,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj);MyWord->Run("MyMacro",MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj,MyObj);MyWord->Quit(MyObj,MyObj,MyObj);System::Runtime::InteropServices::Marshal::ReleaseComObject(MyWord);GC::Collect();this->Close();}catch(Exception^ MyEx){MessageBox::Show(MyEx->Message,"信息提示", MessageBoxButtons::OK, MessageBoxIcon::Information);}}};}
3.调试运行如下:
http://hi.csdn.net/attachment/201006/13/0_127643108656Hc.gif
点击“自动化运行Word示例宏”按钮启动word如下:
http://hi.csdn.net/attachment/201006/13/0_12764313635a3a.gif
页: [1]
查看完整版本: 基于Visual C++2010 与office2010开发办公自动化(17)-使用自动化运行Word宏