CLR C++ Set Word CustomDocumentProperties

  1. // WordIssue.cpp : main project file.  
  2.   
  3. #include "stdafx.h"  
  4.   
  5. using namespace System;  
  6. using namespace System::Reflection;  
  7. #define Office Microsoft::Office  
  8. #define Word Office::Interop::Word  
  9. #define Core Office::Core  
  10.   
  11. int main(array<System::String ^> ^args)  
  12. {  
  13.     Word::Application^ wdApp = gcnew Word::Application();  
  14.     wdApp->Visible = true;  
  15.     Object^ missing = Type::Missing;  
  16.     String^% FileName =   
  17.         gcnew String("C:\Users\v-yangxu\Desktop\Work\1212030001.docx");  
  18.     Boolean^% flag = gcnew Boolean(false);  
  19.     Object^ m = Type::Missing;   
  20.     Word::Document^ wdDoc = wdApp->Documents->Open(FileName,m,flag,m,m,m,m,m,  
  21.         m,m,m,m,m,m,m,m);  
  22.     Object^ cps = wdDoc->GetType()->InvokeMember("CustomDocumentProperties",  
  23.         BindingFlags::Default | BindingFlags::GetProperty,  
  24.         nullptr,wdDoc,nullptr);  
  25.     Type^ type = cps->GetType();  
  26.     array<Object^>^ oargs = {"MyAttribute",false,  
  27.         Core::MsoDocProperties::msoPropertyTypeString,"Just a test"};  
  28.     try{  
  29.         type->InvokeMember("Add",  
  30.             BindingFlags::Default | BindingFlags::InvokeMethod, nullptr,   
  31.             cps, oargs );  
  32.     }catch(Exception^ ex){  
  33.         Console::WriteLine(ex->Message);  
  34.     }  
  35.     Console::ReadKey();  
  36.     return 0;  
  37. }  
原文地址:https://www.cnblogs.com/Alex80/p/4349775.html