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