2009. október 30., péntek

Define variables in a Word document and set their values programmatically


Problem/Question/Abstract:

I need to define some variables in a Word document and be able to set their values from my Delphi program. How can I do that?

Answer:

You can do that using custom document properties:

uses
  Office97; {or Office2000, OfficeXP, Office_TLB}

var
  VDoc, PropName, DocName: OleVariant;
  VDoc := Word.ActiveDocument;
  { ... }

{ Set a document property }
PropName := 'MyOpinionOfThisDocument';
VDoc.CustomDocumentProperties.Add(PropName, False, msoPropertyTypeString,
  'Utter drivel', EmptyParam);
{ Read a document property }
Caption := VDoc.CustomDocumentProperties[PropName].Value;
{ ... }

Nincsenek megjegyzések:

Megjegyzés küldése