我使用OLE自动化完成了以下工作:
打开word文档模板文件。
将邮件合并文件保存到新的word文档文件中。
第一次尝试时,我获取csv文件中的行数,并将其乘以模板中的页数,以获得合并后的总页数。然后我使用合并来创建多个文件。但问题是,我无法准确计算合并文档的页数,因为在某些情况下,模板的9页都不会使用,因为使用了数据和合并字段。因此,在某些情况下,一行只能创建3页(使用9页模板),而其他行可能在邮件合并期间创建9页(使用9页模板)。
因此,唯一的解决方案是将所有行合并到一个文档中,然后再将其拆分为多个文档,以确保每个文件中确实存在像3000页属性这样的确切页数,直到原始合并文件中没有剩余的页数为止。
在我最后一次尝试时,我一直试图使用GoTo来获取特定的页码并删除该页。我本来打算每一页一个一个地尝试这样做,直到我找到我想要的文件的起始位置,然后将其另存为一个新文件,但一直无法这样做。
有人能给我提些建议吗?
谢谢和问候
下面是使用jave中的OLE自动化打开word文件的示例:
Code sample
OleAutomation documentsAutomation = this.getChildAutomation(this.wordAutomation, "Documents");
int [ ] id = documentsAutomation.getIDsOfNames(new String[]{"Open"});
Variant[] arguments = new Variant[1];
arguments[0] = new Variant(fileName); // where filename is the absolute path to the docx file
Variant invokeResult = documentsAutomation.invoke(id[0], arguments);
private OleAutomation getChildAutomation(OleAutomation automation, String childName) {
int[] id = automation.getIDsOfNames(new String[]{childName});
Variant pVarResult = automation.getProperty(id[0]);
return(pVarResult.getAutomation());
}
Code sample