Board index » delphi » access a textbox in Word document

access a textbox in Word document

I have this :

app:_application
doc:_document;

and

app:=coapplication_.create;
doc:=codocument.create;

I would like to acces to a Textbox which is in a word document.

Thanks for your help.

 

Re:access a textbox in Word document


Quote
"pjdp" <p....@accesinter.com> wrote:
>I have this :

>app:_application
>doc:_document;

>and

>app:=coapplication_.create;
>doc:=codocument.create;

>I would like to acces to a Textbox which is in a word document.

How do you know which Textbox you want to access?

Regards,
Chris Roberts

Re:access a textbox in Word document


I have created a Word Document with many textbox. For example I would like
to access to TextBox1.
But I don't know how do with a doc:_document.
With a Variant I have no problem I use doc.textbox1.text for accessing to
the property Text of the Textbox1.

Chris Roberts a crit dans le message
<378c7be9.13261...@forums.inprise.com>...

Quote
>"pjdp" <p....@accesinter.com> wrote:

>>I have this :

>>app:_application
>>doc:_document;

>>and

>>app:=coapplication_.create;
>>doc:=codocument.create;

>>I would like to acces to a Textbox which is in a word document.

>How do you know which Textbox you want to access?

>Regards,
>Chris Roberts

Re:access a textbox in Word document


Quote
"pjdp" <p....@accesinter.com> wrote:
>I have created a Word Document with many textbox. For example I would like
>to access to TextBox1.
>But I don't know how do with a doc:_document.
>With a Variant I have no problem I use doc.textbox1.text for accessing to
>the property Text of the Textbox1.

Here is some Delphi3 code. You may have to make minor changes for it to
compile.

========
{ Delphi3 code }
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,  
    Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btnOpen: TButton;
    OpenDialog1: TOpenDialog;
    Edit1: TEdit;
    ListBox1: TListBox;
    btnTBChange: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure btnTBChangeClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

uses Word_TLB, ComObj, ActiveX;

var
  WordApp: _Application;
  Doc: _Document;
  CloseOnExit: boolean;
{$IFDEF VER100}
  EmptyParam: OleVariant;
{$ENDIF}

procedure TForm1.FormCreate(Sender: TObject);
var
  Unknown: IUnknown;
begin
  CloseOnExit := false;
  try
    // get active Word object
    OleCheck(GetActiveObject(CLASS_Application, nil, Unknown));
    OleCheck(Unknown.QueryInterface(_Application, WordApp));
  except
    // otherwise start one
    WordApp := CoApplication.Create;
    CloseOnExit := true;
  end;
  WordApp.Visible := true;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  SaveChanges: OleVariant;
begin
  if Assigned(Doc) then
  try
    SaveChanges := wdPromptToSaveChanges;
    Doc.Close(SaveChanges, EmptyParam, EmptyParam);
  except
  end;
  if CloseOnExit and Assigned(WordApp) then
  try
    SaveChanges := wdSaveChanges;
    WordApp.Quit(SaveChanges, EmptyParam, EmptyParam);
  except
  end;
end;

procedure TForm1.btnOpenClick(Sender: TObject);
var
  Filename, Index: OleVariant;
  i, ShapeCount: integer;
  s: string;
begin
  if OpenDialog1.Execute then
  begin
    Filename := OpenDialog1.FileName;
    Doc := WordApp.Documents.Open(Filename, EmptyParam, EmptyParam,
           EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
           EmptyParam, EmptyParam);
    ListBox1.Clear;
    ShapeCount := Doc.Shapes.Count;
    for i := 1 to ShapeCount do
    begin
      Index := i;
      s := Doc.Shapes.Item(Index).Name;
      if Pos('Text Box', s) <> 0 then
        ListBox1.Items.Add(s);
    end;
  end;
end;

procedure TForm1.btnTBChangeClick(Sender: TObject);
var
  Index: OleVariant;
  i: integer;
begin
  if Assigned(Doc) then
  begin
    i := ListBox1.ItemIndex;
    if i >= 0 then
    begin
      Index := ListBox1.Items[i];
      // have original text in required alignment etc. otherwise DIY
      Doc.Shapes.Item(Index).TextFrame.TextRange.Text := 'Replacement
Text';
    end;
  end;
end;

initialization
{$IFDEF VER100}
  TVarData(EmptyParam).VType := varError;
  TVarData(EmptyParam).VError := DISP_E_PARAMNOTFOUND;
{$ENDIF}
end.
========

Regards,
Chris Roberts

Other Threads