office宏,译自英文单词Macro。宏是微软公司为其OFFICE软件包设计的一个特殊功能,软件设计者为了让人们在使用软件进行工作时,避免一再地重复相同的动作而设计出来的一种工具,它利用简单的语法,把常用的动作写成宏,当在工作时,就可以直接利用事先编好的宏自动运行,去完成某项特定的任务,而不必再重复相同的动作,目的是让用户文档中的一些任务自动化。
前面我们已经讲解了怎么用自动化运行Excel宏(见:http://blog.csdn.net/yincheng01/archive/2010/06/01/5639397.aspx)
这里我简单讲一下word中如何自动运行宏。
1.启动VS2010
2.我们基于Visual C++ 新建一个CLR窗体工程,插入一个button,设置背景,如下图所示
添加下列引用
插入下列代码,详细请见代码注释
#pragma once namespace 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; /// /// Form1 摘要 /// /// 警告: 如果更改此类的名称,则需要更改 /// 与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的 /// “资源文件名”属性。否则, /// 设计器将不能与此窗体的关联 /// 本地化资源正确交互。 /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: 在此处添加构造函数代码 // } protected: /// /// 清理所有正在使用的资源。 /// ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: /// /// 必需的设计器变量。 /// System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// 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(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.调试运行如下:
点击“自动化运行Word示例宏”按钮启动word如下: