Word Work

This code can be used as is to run a test form so you can see how it works. As the request from the UKBUG for this sort of thing has just been raised I am still in the prosess of cutting out code from my apps to give you the basics of how to do it.The table used in this demo is commected to the customer table in the demo data

 

Unit MainForm;

Interface

Uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Word_TLB,
  ActiveX, StdCtrls, ComObj, Db, DBTables;

Type
  TMainFormF = Class(TForm)
      bStartWord               : TButton;
      bBlankDoc                : TButton;
      bBlankTemplate           : TButton;
      bOpenTemplate            : TButton;
      bSaveDoc                 : TButton;
      bOpenDoc                 : TButton;
      bInsertText              : TButton;
      bTableCustomer           : TButton;
      Edit1                    : TEdit;
      Edit2                    : TEdit;
      Edit3                    : TEdit;
      bTable1                  : TButton;
      bSpace                   : TButton;
      bMoveRight               : TButton;
      bMoveLeft                : TButton;
      bMoveUp                  : TButton;
      bMoveDown                : TButton;
      bSizeTable               : TButton;
      tcustomer                : TTable;
      tcustomerCustNo          : TFloatField;
      tcustomerCompany         : TStringField;
      tcustomerAddr1           : TStringField;
      tcustomerAddr2           : TStringField;
      tcustomerCity            : TStringField;
      tcustomerState           : TStringField;
      tcustomerZip             : TStringField;
      tcustomerCountry         : TStringField;
      tcustomerPhone           : TStringField;
      tcustomerFAX             : TStringField;
      tcustomerTaxRate         : TFloatField;
      tcustomerContact         : TStringField;
      tcustomerLastInvoiceDate : TDateTimeField;
      Procedure bStartWordClick(Sender: TObject);
      Procedure FormClose(Sender: TObject; Var Action: TCloseAction);
      Procedure bBlankDocClick(Sender: TObject);
      Procedure FormShow(Sender: TObject);
      Procedure bBlankTemplateClick(Sender: TObject);
      Procedure bOpenTemplateClick(Sender: TObject);
      Procedure bOpenDocClick(Sender: TObject);
      Procedure bInsertTextClick(Sender: TObject);
      Procedure bTableCustomerClick(Sender: TObject);
      Procedure bTable1Click(Sender: TObject);
      Procedure bSpaceClick(Sender: TObject);
      Procedure bMoveRightClick(Sender: TObject);
      Procedure bMoveLeftClick(Sender: TObject);
      Procedure bMoveUpClick(Sender: TObject);
      Procedure bMoveDownClick(Sender: TObject);
      Procedure bSizeTableClick(Sender: TObject);
    private
      rsWord      : Application;
      rsDoc       : Document;
      rsBookmark  : Bookmark;
      rsParagraph : Paragraph;
      rsSelection : Selection;
      rsTable     : Table;

      LCID : Integer;

      tpFileName                : OleVariant;
      tpFileFormat              : OleVariant;
      tpLockComments            : OleVariant;
      tpPassword                : OleVariant;
      tpAddToRecentFiles        : OleVariant;
      tpWritePassword           : OleVariant;
      tpReadOnlyRecommended     : OleVariant;
      tpEmbedTrueTypeFonts      : OleVariant;
      tpSaveNativePictureFormat : OleVariant;
      tpSaveFormsData           : OleVariant;
      tpSaveAsAOCELetter        : OleVariant;
      SaveChanges               : OleVariant;
      OriginalFormat            : OleVariant;
      RouteDocument             : OleVariant;
      Range                     : Variant; //

      tpFormat                : OleVariant;
      tpWritePasswordTemplate : OleVariant;
      tpWritePasswordDocument : OleVariant;
      tpRevert                : OleVariant;
      tpPasswordTemplate      : OleVariant;
      tpPasswordDocument      : OleVariant;
      tpReadOnly              : OleVariant;
      tpConfirmConversions    : OleVariant;
      tpFileNameSave          : OleVariant;
      tpAddToRecentFilesSave  : OleVariant;
      tpNewDoc                : OleVariant;
      tpNewTemplate           : OleVariant;

    public
      { Public declarations }
  End;


Var
  MainFormF : TMainFormF;
  MainPath  : String; //

Implementation

{$R *.DFM}

Procedure TMainFormF.FormShow(Sender: TObject);
Begin
  tpNewDoc           := 0;
  tpNewTemplate      := 1;
  tpPasswordDocument := '';
  tpPasswordTemplate := 0;

End;

Procedure TMainFormF.FormClose(Sender: TObject; Var Action: TCloseAction);
Begin
  Try
    If rsWord <> Nil Then
    Begin
      rsWord.Quit(SaveChanges,OriginalFormat,RouteDocument);
      rsWord := Nil;
    End;
  Except
  End;
End;

Procedure TMainFormF.bStartWordClick(Sender: TObject);
Var
  Result        : HResult;
  AppWasRunning : boolean;
  Unknown       : IUnknown;
Begin
  AppWasRunning := False;

  Result := GetActiveObject(Class_Application, Nil, Unknown);
  If (Result = MK_E_UNAVAILABLE) Then
  Begin
    rsWord := CoApplication.Create;
  End
  Else
  Begin
    { make sure no other error occurred during GetActiveObject }
    OleCheck(Result);
    OleCheck(Unknown.QueryInterface(_Application, rsWord));
    AppWasRunning := True;
  End;
  rsWord.Visible := True;
End;

Procedure TMainFormF.bBlankDocClick(Sender: TObject);
Begin
  If rsWord <> Nil Then
  Begin
    rsDoc := rsWord.Documents.Add(tpPasswordDocument, tpNewDoc);
  End;
End;

Procedure TMainFormF.bBlankTemplateClick(Sender: TObject);
Begin
  If rsWord <> Nil Then
  Begin
    rsDoc := rsWord.Documents.Add(tpPasswordDocument, tpNewTemplate);
  End;
End;

Procedure TMainFormF.bOpenDocClick(Sender: TObject);
Begin
  tpFileName              := MainPath + 'Test.doc';
  tpConfirmConversions    := True;
  tpReadOnly              := False;
  tpAddToRecentFiles      := False;
  tpPasswordDocument      := '';
  tpPasswordTemplate      := '';
  tpRevert                := False;
  tpWritePasswordDocument := '';
  tpWritePasswordTemplate := '';
  tpFormat                := wdOpenformatAuto;

  rsDoc := rsWord.Documents.Open(tpFileName, tpConfirmConversions, tpReadOnly,
                    tpAddToRecentFiles, tpPasswordDocument, tpPasswordTemplate, tpRevert,
                    tpWritePasswordDocument, tpWritePasswordTemplate, tpFormat);
End;

Procedure TMainFormF.bOpenTemplateClick(Sender: TObject);
Begin
  tpFileName              := MainPath + 'Test.dot';
  tpConfirmConversions    := True;
  tpReadOnly              := False;
  tpAddToRecentFiles      := False;
  tpPasswordDocument      := '';
  tpPasswordTemplate      := '';
  tpRevert                := False;
  tpWritePasswordDocument := '';
  tpWritePasswordTemplate := '';
  tpFormat                := wdOpenformatAuto;

  rsDoc := rsWord.Documents.Open(tpFileName, tpConfirmConversions, tpReadOnly,
                    tpAddToRecentFiles, tpPasswordDocument, tpPasswordTemplate, tpRevert,
                    tpWritePasswordDocument, tpWritePasswordTemplate, tpFormat);
End;

Procedure TMainFormF.bInsertTextClick(Sender: TObject);
Var
  S : Selection;
Begin
  S := rsWord.Selection;

  // Write the next text in bold type
  S.Font.Bold := integer(True);
  // Set the Font to 12
  S.Font.Size := 12;
  // Set Font Type
  S.Font.Name := 'Arial Bold';
  // Paste Text
  S.TypeText('This Text Will Be Bold');
  // Reset Bold to false
  S.Font.Bold := integer(False);
  // Do A line Feed
  S.TypeParagraph;

  // Write the next text in bold type
  S.Font.Bold := integer(False);
  // Set the Font to 12
  S.Font.Size := 12;
  // Set Font Type
  S.Font.Name := 'Arial Bold';
  // Paste Text
  S.TypeText('This Text Will Be Arial Bold  as you can see the Font is setting the Bold');
  // Reset Bold to false
  S.Font.Bold := integer(False);
  // Do A line Feed
  S.TypeParagraph;

  // Write the next text in bold type
  S.Font.Bold := integer(False);
  // Set the Font to 12
  S.Font.Size := 12;
  // Set Font Type
  S.Font.Name := 'Arial';
  // Paste Text
  S.TypeText('This Text Will Be Arial');
  // Reset Bold to false
  S.Font.Bold := integer(False);
  // Do A line Feed
  S.TypeParagraph;


  // Write the next text in italic type
  S.Font.Italic := integer(True);
  // Set the Font to 14
  S.Font.Size   := 14;
  S.TypeText('Print Italic');
  S.Font.Italic := integer(False);
  // Do A line Feed
  S.TypeParagraph;
End;

Procedure TMainFormF.bTableCustomerClick(Sender: TObject);
Var
  tpRows     : Integer; //
  tpRecCount : Integer;
  Range      : Variant; //
  j          : integer;
  S          : Selection;
Begin
  S     := rsWord.Selection;
  Range := S;

  With tcustomer Do
  Begin
    Active := True;
    First;
    tpRecCount := RecordCount;
    tpRows     := RecordCount + 1;
    rsTable    := rsDoc.Tables.Add(S.Get_Range, tpRows, 8);

    rsTable.Cell(1, 1).Range.Text := 'CustNo';
    rsTable.Cell(1, 2).Range.Text := 'Company';
    rsTable.Cell(1, 3).Range.Text := 'Addr1';
    rsTable.Cell(1, 4).Range.Text := 'Addr2';
    rsTable.Cell(1, 5).Range.Text := 'City';
    rsTable.Cell(1, 6).Range.Text := 'State';
    rsTable.Cell(1, 7).Range.Text := 'Zip';
    rsTable.Cell(1, 8).Range.Text := 'Country';

    rsTable.Cell(1, 1).Range.Bold := 1;
    rsTable.Cell(1, 2).Range.Bold := 1;
    rsTable.Cell(1, 3).Range.Bold := 1;
    rsTable.Cell(1, 4).Range.Bold := 1;
    rsTable.Cell(1, 5).Range.Bold := 1;
    rsTable.Cell(1, 6).Range.Bold := 1;
    rsTable.Cell(1, 7).Range.Bold := 1;
    rsTable.Cell(1, 8).Range.Bold := 1;

    j := 2;
    While Not EOF Do
    Begin
      rsTable.Cell(j, 1).Range.Text := tcustomerCustNo.AsString;
      rsTable.Cell(j, 2).Range.Text := tcustomerCompany.AsString;
      rsTable.Cell(j, 3).Range.Text := tcustomerAddr1.AsString;
      rsTable.Cell(j, 4).Range.Text := tcustomerAddr1.AsString;
      rsTable.Cell(j, 5).Range.Text := tcustomerCity.AsString;
      rsTable.Cell(j, 6).Range.Text := tcustomerState.AsString;
      rsTable.Cell(j, 7).Range.Text := tcustomerZip.AsString;
      rsTable.Cell(j, 8).Range.Text := tcustomerCountry.AsString;
      inc(j);
      Next;
    End;
    Active := False;
  End;    // for
  Range.Font.Bold := False;

  Range.Font.Size := 11;

End;

Procedure TMainFormF.bTable1Click(Sender: TObject);
Var
  j       : integer;
  tpCount : integer;
  Range   : OleVariant;
  S       : Selection;

Begin
  tpCount := rsDoc.Paragraphs.Count;


  rsParagraph     := rsDoc.Paragraphs.Item(tpCount);
  Range           := rsParagraph.Get_Range;
  Range.Text      := 'ONE - TWO';
  Range.Font.Bold := True;
  Range.Font.Size := 10;
  tpCount         := rsDoc.Paragraphs.Count;

  rsParagraph := rsDoc.Paragraphs.Item(tpCount);
  Range       := rsParagraph.Get_Range;
  rsTable     := rsDoc.Tables.Add(rsParagraph.Get_Range, 11, 6);


  rsTable.Cell(1, 1).Range.Text := '';
  rsTable.Cell(1, 2).Range.Text := 'Employee Name';
  rsTable.Cell(1, 3).Range.Text := 'Status';
  rsTable.Cell(1, 4).Range.Text := 'Cover';
  rsTable.Cell(1, 5).Range.Text := 'W.E.F';
  rsTable.Cell(1, 6).Range.Text := 'Premium';

  rsTable.Cell(1, 1).Range.Bold := 1;
  rsTable.Cell(1, 2).Range.Bold := 1;
  rsTable.Cell(1, 3).Range.Bold := 1;
  rsTable.Cell(1, 4).Range.Bold := 1;
  rsTable.Cell(1, 5).Range.Bold := 1;
  rsTable.Cell(1, 6).Range.Bold := 1;

  For j := 2 To 10 Do    // Iterate
  Begin

    Begin
      rsTable.Cell(j, 1).Range.Text := IntToStr(j - 1);
      rsTable.Cell(j, 2).Range.Text := 'FIRSTNAME';
      rsTable.Cell(j, 3).Range.Text := 'FamilyStatus';
      rsTable.Cell(j, 4).Range.Text := 'COVERBAND';
      rsTable.Cell(j, 5).Range.Text := FormatDateTime('dd mm yyyy', DATE);
      rsTable.Cell(j, 6).Range.Text := '£ ';

      Next;
    End;
  End;    // for
  Range.Font.Bold := False;
  Range.Font.Size := 11;


  Range := S;
  S     := rsWord.Selection;
  S.TypeParagraph;

End;

Procedure TMainFormF.bSpaceClick(Sender: TObject);
Var
  tpCount : integer;
  Range   : OleVariant;
  S       : Selection;
Begin
  tpCount     := rsDoc.Paragraphs.Count;
  rsParagraph := rsDoc.Paragraphs.Item(tpCount);
  Range       := rsParagraph.Get_Range;
  Range.Text  := #13;
End;

Procedure TMainFormF.bSizeTableClick(Sender: TObject);
Var
  tpCount : Integer;
Begin
  tpCount                       := rsDoc.Tables.Count;
  rsTable.Columns.Item(1).Width := 20;
  rsTable.Columns.Item(2).Width := 100;
End;

Procedure TMainFormF.bMoveRightClick(Sender: TObject);
Var
  MoveUnit : OleVariant;
  vCount   : OleVariant;
  Extended : OleVariant;
Begin
  MoveUnit := wdWord; // Will Move One Word At A Time
  vCount   := 1;
  Extended := unassigned;
  rsWord.selection.MoveRight(MoveUnit, vCount, Extended);
  rsWord.ActiveDocument.Fields.Update;
End;

Procedure TMainFormF.Button5Click(Sender: TObject);
Var
  MoveUnit : OleVariant;
  vCount   : OleVariant;
  Extended : OleVariant;
Begin
  MoveUnit := wdWord; // Will Move One Word At A Time
  //  MoveUnit := wdCell; // Will Move One Cell At A Time Must Have A Table To Work On
  vCount   := 1;
  Extended := unassigned;

  rsWord.selection.MoveEnd(MoveUnit, vCount);
  rsWord.ActiveDocument.Fields.Update;

End;

Procedure TMainFormF.bMoveLeftClick(Sender: TObject);
Var
  MoveUnit : OleVariant;
  vCount   : OleVariant;
  Extended : OleVariant;
Begin
  MoveUnit := wdCell;
  vCount   := 1;
  Extended := unassigned;

  rsWord.selection.MoveLeft(MoveUnit, vCount, Extended);
  rsWord.ActiveDocument.Fields.Update;
End;

Procedure TMainFormF.bMoveUpClick(Sender: TObject);
Var
  MoveUnit : OleVariant;
  vCount   : OleVariant;
  Extended : OleVariant;
Begin
  file://wdLine, wdParagraph, wdWindow or wdScreen.
  MoveUnit := wdLine;
  vCount   := 1;
  Extended := unassigned;

  Edit1.Text := IntToStr(rsWord.selection.MoveUp(MoveUnit, vCount, Extended));
  rsWord.ActiveDocument.Fields.Update;
End;

Procedure TMainFormF.bMoveDownClick(Sender: TObject);
Var
  MoveUnit : OleVariant;
  vCount   : OleVariant;
  Extended : OleVariant;
Begin
  file://wdLine, wdParagraph, wdWindow or wdScreen.
  MoveUnit := wdLine;
  vCount   := 1;
  Extended := unassigned;

  Edit1.Text := IntToStr(rsWord.selection.MoveDown(MoveUnit, vCount, Extended));
  rsWord.ActiveDocument.Fields.Update;
End;

 

Initialization
  MainPath := ExtractFilePath(Forms.Application.ExeName);
End.