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

Viewed 268

i write small code to update 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;
1 Answers

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. 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. Calling Doc.Save, like I suggested, didn't.

Related