基于Visual C++2010 与office2010开发办公自动化(16)-如何设置新建Word文档属性
VS2010是新一代全新开发工具http://hi.csdn.net/attachment/201005/4/0_1272937180tQOq.gif
Office 2010,是微软推出新一代办公软件,开发代号为Office 14,实际是第12个发行版。该软件共有6个版本,分别是初级版、家庭及学生版、家庭及商业版、标准版、专业版和专业高级版,此外还推出Office 2010免费版本,其中仅包括Word和Excel应用。除了完整版以外,微软还将发布针对Office 2007的升级版Office 2010。Office 2010可支持32位和64位vista及Windows7,仅支持32位WindowsXP,不支持64位XP。
http://hi.csdn.net/attachment/201005/4/0_12729373494mtI.gif
Word文档的“属性”里包含了作者、修改日期、摘要等信息。需要把文档打包批量发送给他人时,这些选项可能泄露信息,或者仅仅因为来源不同造成信息混乱。
那么在我们的程序中能怎么实现调用word2010,如何在程序中修改word文档属性呢?
1.启动VS2010
http://hi.csdn.net/attachment/201005/29/0_12751226208PCC.gif
2.我们基于Visual C++ 新建一个CLR窗体工程,插入2个button,若干Lable和TextBox,设置背景,如下图:
http://hi.csdn.net/attachment/201006/13/0_1276416284M8lU.gif
添加下列引用
0_12753735677gRG.gifhttp://hi.csdn.net/attachment/201006/1/0_12753735677gRG.gif在Form1.h中插入下列代码
#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::Label^label1;protected: private: System::Windows::Forms::TextBox^textBox1;private: System::Windows::Forms::TextBox^textBox2;private: System::Windows::Forms::Label^label2;private: System::Windows::Forms::TextBox^textBox3;private: System::Windows::Forms::Label^label3;private: System::Windows::Forms::TextBox^textBox4;private: System::Windows::Forms::Label^label4;private: System::Windows::Forms::TextBox^textBox5;private: System::Windows::Forms::Label^label5;private: System::Windows::Forms::Button^button1;private: System::Windows::Forms::Button^button2;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->label1 = (gcnew System::Windows::Forms::Label());this->textBox1 = (gcnew System::Windows::Forms::TextBox());this->textBox2 = (gcnew System::Windows::Forms::TextBox());this->label2 = (gcnew System::Windows::Forms::Label());this->textBox3 = (gcnew System::Windows::Forms::TextBox());this->label3 = (gcnew System::Windows::Forms::Label());this->textBox4 = (gcnew System::Windows::Forms::TextBox());this->label4 = (gcnew System::Windows::Forms::Label());this->textBox5 = (gcnew System::Windows::Forms::TextBox());this->label5 = (gcnew System::Windows::Forms::Label());this->button1 = (gcnew System::Windows::Forms::Button());this->button2 = (gcnew System::Windows::Forms::Button());this->SuspendLayout();// // label1// this->label1->AutoSize = true;this->label1->ForeColor = System::Drawing::Color::Blue;this->label1->Location = System::Drawing::Point(67, 169);this->label1->Name = L"label1";this->label1->Size = System::Drawing::Size(59, 12);this->label1->TabIndex = 0;this->label1->Text = L"作者(A):";// // textBox1// this->textBox1->ForeColor = System::Drawing::Color::Blue;this->textBox1->Location = System::Drawing::Point(132, 160);this->textBox1->Name = L"textBox1";this->textBox1->Size = System::Drawing::Size(333, 21);this->textBox1->TabIndex = 1;this->textBox1->Text = L"尹成";// // textBox2// this->textBox2->ForeColor = System::Drawing::Color::Blue;this->textBox2->Location = System::Drawing::Point(132, 187);this->textBox2->Name = L"textBox2";this->textBox2->Size = System::Drawing::Size(333, 21);this->textBox2->TabIndex = 3;this->textBox2->Text = L"基于Visual C++2010 与office2010开发办公自动化";// // label2// this->label2->AutoSize = true;this->label2->ForeColor = System::Drawing::Color::Blue;this->label2->Location = System::Drawing::Point(67, 196);this->label2->Name = L"label2";this->label2->Size = System::Drawing::Size(59, 12);this->label2->TabIndex = 2;this->label2->Text = L"主题(S):";// // textBox3// this->textBox3->ForeColor = System::Drawing::Color::Blue;this->textBox3->Location = System::Drawing::Point(132, 214);this->textBox3->Name = L"textBox3";this->textBox3->Size = System::Drawing::Size(333, 21);this->textBox3->TabIndex = 5;this->textBox3->Text = L"如何设置新建Word文档属性";// // label3// this->label3->AutoSize = true;this->label3->ForeColor = System::Drawing::Color::Blue;this->label3->Location = System::Drawing::Point(67, 223);this->label3->Name = L"label3";this->label3->Size = System::Drawing::Size(59, 12);this->label3->TabIndex = 4;this->label3->Text = L"标题(T):";// // textBox4// this->textBox4->ForeColor = System::Drawing::Color::Blue;this->textBox4->Location = System::Drawing::Point(132, 241);this->textBox4->Name = L"textBox4";this->textBox4->Size = System::Drawing::Size(333, 21);this->textBox4->TabIndex = 7;this->textBox4->Text = L"这是一个测试经理";// // label4// this->label4->AutoSize = true;this->label4->ForeColor = System::Drawing::Color::Blue;this->label4->Location = System::Drawing::Point(67, 250);this->label4->Name = L"label4";this->label4->Size = System::Drawing::Size(59, 12);this->label4->TabIndex = 6;this->label4->Text = L"经理(M):";// // textBox5// this->textBox5->ForeColor = System::Drawing::Color::Blue;this->textBox5->Location = System::Drawing::Point(132, 268);this->textBox5->Name = L"textBox5";this->textBox5->Size = System::Drawing::Size(333, 21);this->textBox5->TabIndex = 9;this->textBox5->Text = L"嵌入式事业部";// // label5// this->label5->AutoSize = true;this->label5->ForeColor = System::Drawing::Color::Blue;this->label5->Location = System::Drawing::Point(67, 277);this->label5->Name = L"label5";this->label5->Size = System::Drawing::Size(59, 12);this->label5->TabIndex = 8;this->label5->Text = L"单位(O):";// // button1// this->button1->ForeColor = System::Drawing::Color::Maroon;this->button1->Location = System::Drawing::Point(124, 322);this->button1->Name = L"button1";this->button1->Size = System::Drawing::Size(155, 58);this->button1->TabIndex = 10;this->button1->Text = L"设置文档属性";this->button1->UseVisualStyleBackColor = true;this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);// // button2// this->button2->ForeColor = System::Drawing::Color::Maroon;this->button2->Location = System::Drawing::Point(280, 322);this->button2->Name = L"button2";this->button2->Size = System::Drawing::Size(155, 58);this->button2->TabIndex = 11;this->button2->Text = L"关闭应用程序";this->button2->UseVisualStyleBackColor = true;this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);// // Form1// this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"$this.BackgroundImage")));this->ClientSize = System::Drawing::Size(558, 428);this->Controls->Add(this->button2);this->Controls->Add(this->button1);this->Controls->Add(this->textBox5);this->Controls->Add(this->label5);this->Controls->Add(this->textBox4);this->Controls->Add(this->label4);this->Controls->Add(this->textBox3);this->Controls->Add(this->label3);this->Controls->Add(this->textBox2);this->Controls->Add(this->label2);this->Controls->Add(this->textBox1);this->Controls->Add(this->label1);this->Name = L"Form1";this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;this->Text = L"csdn著名技术专家尹成-演示设置新建Word文档属性";this->ResumeLayout(false);this->PerformLayout();}#pragma endregion//设置文档属性private: System::Void button1_Click(System::Object^sender, System::EventArgs^e) {Microsoft::Office::Interop::Word::ApplicationClass^ MyWord;Microsoft::Office::Interop::Word:: _Document^ MyDocument;System::Object^ MyBuilt;System::Object^ MyCustom;System::Type^ MyTypeBuilt;System::String^ MyIndex;System::String^ MyValue;System::Object^ MyItem;System::Type^ MyItemType;System::Object^ MyObj;System::Reflection::Binder^ MyNull;array<System::Object^>^ MyObjs;try{ //创建Word实例并显示 MyWord=gcnew Microsoft::Office::Interop::Word::ApplicationClass(); MyWord->Visible=true; //创建新文档并取得文档属性集合 MyObj=System::Reflection::Missing::Value; MyDocument=MyWord->Documents->Add(MyObj,MyObj,MyObj,MyObj); MyBuilt=MyDocument->BuiltInDocumentProperties; MyTypeBuilt=MyBuilt->GetType(); //设置作者属性 MyIndex = "Author"; MyObjs = gcnew array<System::Object^>(1); MyObjs = MyIndex; MyItem = MyTypeBuilt->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::GetProperty, MyNull, MyBuilt, MyObjs); MyItemType = MyItem->GetType(); MyObjs =gcnew array<System::Object^>(2); MyObjs = "Author"; MyObjs = this->textBox1->Text; MyItemType->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::SetProperty, MyNull, MyBuilt, MyObjs); // 设置主题属性 MyIndex = "Subject"; MyObjs = gcnew array<System::Object^>(1); MyObjs = MyIndex; MyItem = MyTypeBuilt->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::GetProperty, MyNull, MyBuilt, MyObjs); MyItemType = MyItem->GetType(); MyObjs = gcnew array<System::Object^>(2); MyObjs = "Subject"; MyObjs = this->textBox2->Text; MyItemType->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::SetProperty, MyNull, MyBuilt, MyObjs); //设置标题属性 MyIndex = "Title"; MyObjs = gcnew array<System::Object^>(1); MyObjs = MyIndex; MyItem = MyTypeBuilt->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::GetProperty, MyNull, MyBuilt, MyObjs); MyItemType = MyItem->GetType(); MyObjs = gcnew array<System::Object^>(2); MyObjs = "Title"; MyObjs = this->textBox3->Text; MyItemType->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::SetProperty, MyNull, MyBuilt, MyObjs); //设置经理属性 MyIndex = "Manager"; MyObjs = gcnew array<System::Object^>(1); MyObjs = MyIndex; MyItem = MyTypeBuilt->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::GetProperty, MyNull, MyBuilt, MyObjs); MyItemType = MyItem->GetType(); MyObjs = gcnew array<System::Object^>(2); MyObjs = "Manager"; MyObjs = this->textBox4->Text; MyItemType->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::SetProperty, MyNull, MyBuilt, MyObjs); //设置单位属性 MyIndex = "Company"; MyObjs = gcnew array<System::Object^>(1); MyObjs = MyIndex; MyItem = MyTypeBuilt->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::GetProperty, MyNull, MyBuilt, MyObjs); MyItemType = MyItem->GetType(); MyObjs = gcnew array<System::Object^>(2); MyObjs = "Company"; MyObjs = this->textBox5->Text; MyItemType->InvokeMember("Item", System::Reflection::BindingFlags::Default | System::Reflection::BindingFlags::SetProperty, MyNull, MyBuilt, MyObjs); MessageBox::Show("请在“文件(F)\\属性(I)”菜单中查看设置结果!", "信息提示", MessageBoxButtons::OK, MessageBoxIcon::Information); }catch (Exception^ MyEx){MessageBox::Show(MyEx->Message, "信息提示", MessageBoxButtons::OK, MessageBoxIcon::Information); }}//关闭应用程序private: System::Void button2_Click(System::Object^sender, System::EventArgs^e) { this->Close(); }};}
3,调试运行界面如下:
http://hi.csdn.net/attachment/201006/13/0_1276417839Z2fA.gif
分别在textBox里输入如下信息:
http://hi.csdn.net/attachment/201006/13/0_1276417901w888.gif
点击“设置文档属性按钮”将自动新建一个word文档
http://hi.csdn.net/attachment/201006/13/0_12764181512u2y.gif
查看“文件/属性”属性信息如下:
http://hi.csdn.net/attachment/201006/13/0_12764182950gVm.gif
如上我们即可在word文档中修改相关属性信息,按照上面方法我们可以按照自己实际工作需要,可以统一修改多个文档属性,达到高速办公,节省时间目的。在此我只是以一个简单示例,介绍功能而已,实际操作应用还是大家自己去发掘了。
页:
[1]