简体   繁体   English

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

[英]Need To Add/Set Read/Get CustomDocumentProperties in Word/Excel Document in delphi

i write small code to update CustomDocumentProperties.我编写小代码来更新 CustomDocumentProperties。

but after save the file and quit.但保存文件并退出后。 the prop dose not saved as part of the document?道具没有保存为文档的一部分? is it possible?可能吗? if so?如果是这样? what is the right way to do it?什么是正确的方法?

Thanks for advance.感谢提前。

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;

Apparently, if you set custom document properties in code you need to tell Word that the document hasn't been saved, otherwise it will fail to do so when asked - see http://www.vbaexpress.com/forum/showthread.php?16678-Custom-Document-Properties-Don-t-Always-Save .显然,如果您在代码中设置自定义文档属性,您需要告诉 Word 该文档尚未保存,否则在询问时它将无法保存 - 请参阅http://www.vbaexpress.com/forum/showthread.php ?16678-Custom-Document-Properties-Don-t-Always-Save So I suggest you change the central part of your code to:因此,我建议您将代码的中心部分更改为:

  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
  [...]

Works fine for me in D7 + Word2010 with this change.通过此更改,在 D7 + Word2010 中对我来说效果很好。 Calling Doc.Save, like I suggested, didn't.像我建议的那样,调用 Doc.Save 没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM