【问题标题】:Need To Add/Set Read/Get CustomDocumentProperties in Word/Excel Document in delphi需要在delphi的Word/Excel文档中添加/设置读取/获取CustomDocumentProperties
【发布时间】:2020-06-23 03:35:56
【问题描述】:

我编写小代码来更新 CustomDocumentProperties。

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

感谢您的提前。

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 之前,您是否尝试过在 Doc 对象上显式调用 Save?
  • 是的。我试过了。没有帮助。

标签: delphi properties ms-word document


【解决方案1】:

显然,如果您在代码中设置自定义文档属性,您需要告诉 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 没有。

【讨论】:

  • 这个答案被接受。我不知道如何将其签名为接受:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多