質問

I want to create a .docx document (Word 2007 and 2010) with OpenXML in C++/CLI

I use this code for generate the document:

WordprocessingDocument^ wordDoc  = WordprocessingDocument::Create("C:\\...\MyFile.docx", WordprocessingDocumentType::Document);

MainDocumentPart^ mainPart = wordDoc->AddMainDocumentPart();

Document^ elt = gcnew Document( gcnew Body( gcnew Paragraph( gcnew Run( gcnew DocumentFormat::OpenXml::Wordprocessing::Text("Hello !") ) ) ) ); 

elt->Save(mainPart);

mainPart->Document->Save();

wordDoc->Close();

The file is created but it is empty.

If I write this code in C#, the file is good and it contains the text.

Why my code in C++/cli, create the file (MyFile.docx) with no text?

役に立ちましたか?

解決

I repro this problem. Not sure what is going on, this syntax is documented to work. It works properly when you write the code explicitly, using the AppendChild() method. Like this:

WordprocessingDocument^ wordDoc  = WordprocessingDocument::Create("C:\\temp\\MyFile.docx", WordprocessingDocumentType::Document);
MainDocumentPart^ mainPart = wordDoc->AddMainDocumentPart();

mainPart->Document = gcnew Document;
Body^ body = mainPart->Document->AppendChild(gcnew Body);
Paragraph^ para = body->AppendChild(gcnew Paragraph);
Run^ run = para->AppendChild(gcnew Run);
run->AppendChild(gcnew DocumentFormat::OpenXml::Wordprocessing::Text("Hello !"));

delete wordDoc;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top