我正在尝试通过 c++ builder 5 应用程序控制 Word。我想打开一个用 Word 创建的“.dot”模型文件并修改它。在“.dot”模型文件中有一些字段。例如,、 、Title
等等LastName
,
我想修改这些字段,将文本放入其中,然后用新名称保存文件,例如“Warning.doc”,而“.dot”文件保持不变。FirstName
Address
我可以打开文件,计算它包含的字段数,但是当用字符串替换每个字段时,我不知道该怎么做,因为我没有关于 OleFunction 和 OlePropertyGet 方法的完整文档。我将我的源代码附加到此消息中,有人可以帮我解决这个问题吗?
try
{
my_word = Variant::CreateObject("word.application");
}
catch (...)
{
Application->MessageBox("Unable to obtain Word automation object",
"Error:",MB_OK | MB_ICONERROR);
}
my_word.OlePropertySet("Visible", (Variant)true);
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Variant this_doc;
Variant my_fields;
Variant test;
int k,field_count;
AnsiString test1;
AnsiString filename = "d:\\ProgrammaWord\\1-Avviso.dot";
my_docs = my_word.OlePropertyGet("Documents");
this_doc = my_docs.OleFunction("Open", filename);
my_fields = this_doc.OlePropertyGet("Fields");
field_count = my_fields.OlePropertyGet("Count");
for(k = 1; k <= field_count; k++)
{
test = my_fields.OleFunction("Item",(Variant)k);
test1 = test.OleFunction("Value"); //This instruction throws an exception
// "Value" is not a recognized parameter
// in this case
Memo1->Lines->Add(test1);
}
}