delphi - 需要在delphi中的Word/Excel文档中添加/设置读取/获取CustomDocumentProperties

标签 delphi properties ms-word document

我编写小代码来更新 CustomDocumentProperties。

但保存文件后退出。 Prop 没有保存为文档的一部分? 可能吗? 如果是这样?什么是正确的做法?

感谢提前。

Var
  Doc : OleVariant;
  DocProps : OleVariant;
  Item : OleVariant;
  i : integer;
  Value : string;
  SaveChanges: OleVariant;
begin
  Memo1.Lines.Clear;
  WordApplication1.Connect;
  WordApplication1.Visible := false;

  WordApplication1.Documents.Open(Edit1.Text, EmptyParam, EmptyParam, EmptyParam,
                          EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                              EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                              EmptyParam, EmptyParam, EmptyParam, EmptyParam);


  Doc := WordApplication1.ActiveDocument;

  DocProps := Doc.CustomDocumentProperties;

  DocProps.Add(
               'MyOpinionOfThisDocument2',
               False, msoPropertyTypeString,
               'Utter drivel', EmptyParam);
  DocProps.Add(
               'Mz_Ident2',
               False, msoPropertyTypeString,
               '1997', EmptyParam);

  for I := 1 to DocProps.Count do // Iterate
  begin
    Item := DocProps.Item[i];
    Memo1.Lines.Add(Item.name + ' = ' + item.value);
  end; 

  SaveChanges := wdSaveChanges;
  WordApplication1.Quit(SaveChanges, EmptyParam, EmptyParam);
  WordApplication1.Disconnect;

end;

最佳答案

显然,如果您在代码中设置自定义文档属性,您需要告诉 Word 文档尚未保存,否则在询问时将无法保存 - 参见 http://www.vbaexpress.com/forum/showthread.php?16678-Custom-Document-Properties-Don-t-Always-Save .所以 我建议您将代码的中心部分更改为:

  DocProps := Doc.CustomDocumentProperties;

  if DocProps.Count = 0 then begin
    DocProps.Add(
                'MyOpinionOfThisDocument2',
                 False, msoPropertyTypeString,
                 'Utter drivel', EmptyParam);
    DocProps.Add(
                 'Mz_Ident2',
                 False, msoPropertyTypeString,
                 '1997', EmptyParam);
  end;
  Doc.Saved := False;

  for I := 1 to DocProps.Count do // Iterate
  [...]

通过此更改,我在 D7 + Word2010 中工作正常。调用 Doc.Save,就像我建议的那样,没有。

关于delphi - 需要在delphi中的Word/Excel文档中添加/设置读取/获取CustomDocumentProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62515991/

相关文章:

c - 在 enum 类型的 gtk3 中安装 style-property

objective-c - 从 UITabBarController 访问 viewController 属性

ruby-on-rails - ruby rails : In there a way to convert word to html?

vba - 使用 VBA 在 Word 模板中填充表格?

python - 在 pywin32 中打开 MS Word 文件

delphi - Delphi 中的 TCPclient.connected 问题 - Indy

delphi - 德尔福中的不安全 Actor 有时只在某些机器上给出零?

json - 当我使用 SuperObject 解析字符串时,为什么会出现大量问号?

delphi - 如何从当前聚焦的组件中移除焦点?

oop - 中间类 : add getter-setter support for properties