Board index » delphi » OLE: Delphi + WordBasic

OLE: Delphi + WordBasic

Help help!

I need to retrieve the name of the current font from a Word document to be
used by my Delphi OLE application. The WordBasic command for this is
"Font$()". Please note that this is different from the WordBasic "Font"
command: "Font" is a procedure which *sets* the font in the Word document,
while "Font$()" is a function that *returns* the font in use in the document.

My probelm is that I can't seem to find the correct syntax to include the
"Font$()" command in Delphi. The "$" bit in particular causes Delphi to have a
hissy-fit.

Any suggestions?

John K

   \\\\   John Kean
   OO |   ~~~~~~~~~
   <  )   kew...@wagresearch.criw.nwz
   \/ |   (there's really no 'w's in my email address)
   `--

 

Re:OLE: Delphi + WordBasic


The following code should be sufficient to help you do what you need to do.
 You no longer need to use Font$.

unit oleword;
// Need a form with a button, memo, and edit component.
// Written for MSWord 8.
// Also need to create a test document d:\test.doc
interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
       MSWord: Variant;
   // WordVersion: Byte;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var Test, Test1:  Integer;
     AString: Variant;
begin
   MSWord := CreateOLEObject('Word.Application');     //Word 8
   MSWord.Documents.Open (FileName:='d:\test.doc', ReadOnly:=True);
   MSWord.Visible := 1;         //Uncomment if you wish to show the file;
   Test := MSWord.FontNames.Count;
    For Test1 := 1 To Test  do
      begin
      AString := MSWord.FontNames.Item(Test1) ;
      Memo1.Lines.Add(AString);
      end;
      MSWord.ActiveDocument.Range(Start:=0, End:=0);
      MSWord.ActiveDocument.Range.InsertAfter(Text:='Title');
      MSWord.ActiveDocument.Range.InsertParagraphAfter;
      MSWord.ActiveDocument.Range.Font.Name := 'Arial';
      MSWord.ActiveDocument.Range.Font.Size := 24;
      AString := MSWord.ActiveDocument.Range.Font.Name;
      Edit1.Text := AString;
end;

end.

John Kean <kew...@wagresearch.criw.nwz but without the 'w's> wrote in
article <6duuah$jg...@wnnews1.netlink.net.nz>...

Quote

> Help help!

> I need to retrieve the name of the current font from a Word document to
be
> used by my Delphi OLE application. The WordBasic command for this is
> "Font$()". Please note that this is different from the WordBasic "Font"
> command: "Font" is a procedure which *sets* the font in the Word
document,
> while "Font$()" is a function that *returns* the font in use in the
document.

> My probelm is that I can't seem to find the correct syntax to include the
> "Font$()" command in Delphi. The "$" bit in particular causes Delphi to
have a
> hissy-fit.

> Any suggestions?

> John K

>    \\\\   John Kean
>    OO |   ~~~~~~~~~
>    <  )   kew...@wagresearch.criw.nwz
>    \/ |   (there's really no 'w's in my email address)
>    `--

Re:OLE: Delphi + WordBasic


This should explain it.  You do not need to use Font$...

 unit oleword;
// Need a form with a button, memo, and edit component.
// Written for MSWord 8.
// Also need to create a test document d:\test.doc
interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
       MSWord: Variant;
   // WordVersion: Byte;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var Test, Test1:  Integer;
     AString: Variant;
begin
   MSWord := CreateOLEObject('Word.Application');     //Word 8
   MSWord.Documents.Open (FileName:='d:\test.doc', ReadOnly:=True);
   MSWord.Visible := 1;         //Uncomment if you wish to show the file;
   Test := MSWord.FontNames.Count;
    For Test1 := 1 To Test  do
      begin
      AString := MSWord.FontNames.Item(Test1) ;
      Memo1.Lines.Add(AString);
      end;
      MSWord.ActiveDocument.Range(Start:=0, End:=0);
      MSWord.ActiveDocument.Range.InsertAfter(Text:='Title');
      MSWord.ActiveDocument.Range.InsertParagraphAfter;
      MSWord.ActiveDocument.Range.Font.Name := 'Arial';
      MSWord.ActiveDocument.Range.Font.Size := 24;
      AString := MSWord.ActiveDocument.Range.Font.Name;
      Edit1.Text := AString;
end;

end.

John Kean <kew...@wagresearch.criw.nwz but without the 'w's> wrote in
article <6duuah$jg...@wnnews1.netlink.net.nz>...

Quote

> Help help!

> I need to retrieve the name of the current font from a Word document to
be
> used by my Delphi OLE application. The WordBasic command for this is
> "Font$()". Please note that this is different from the WordBasic "Font"
> command: "Font" is a procedure which *sets* the font in the Word
document,
> while "Font$()" is a function that *returns* the font in use in the
document.

> My probelm is that I can't seem to find the correct syntax to include the
> "Font$()" command in Delphi. The "$" bit in particular causes Delphi to
have a
> hissy-fit.

> Any suggestions?

> John K

>    \\\\   John Kean
>    OO |   ~~~~~~~~~
>    <  )   kew...@wagresearch.criw.nwz
>    \/ |   (there's really no 'w's in my email address)
>    `--

Re:OLE: Delphi + WordBasic


Quote
>This should explain it.  You do not need to use Font$...

> unit oleword;
>// Need a form with a button, memo, and edit component.
>// Written for MSWord 8.
>// Also need to create a test document d:\test.doc
>interface

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

>type
>  TForm1 = class(TForm)
>    Memo1: TMemo;
>    Edit1: TEdit;
>    Button1: TButton;
>    procedure Button1Click(Sender: TObject);
>  private
>    { Private declarations }
>  public
>    { Public declarations }
>       MSWord: Variant;
>   // WordVersion: Byte;
>  end;

>var
>  Form1: TForm1;

>implementation

>{$R *.DFM}

>procedure TForm1.Button1Click(Sender: TObject);
>var Test, Test1:  Integer;
>     AString: Variant;
>begin
>   MSWord := CreateOLEObject('Word.Application');     //Word 8
>   MSWord.Documents.Open (FileName:='d:\test.doc', ReadOnly:=True);
>   MSWord.Visible := 1;         //Uncomment if you wish to show the file;
>   Test := MSWord.FontNames.Count;
>    For Test1 := 1 To Test  do
>      begin
>      AString := MSWord.FontNames.Item(Test1) ;
>      Memo1.Lines.Add(AString);
>      end;
>      MSWord.ActiveDocument.Range(Start:=0, End:=0);
>      MSWord.ActiveDocument.Range.InsertAfter(Text:='Title');
>      MSWord.ActiveDocument.Range.InsertParagraphAfter;
>      MSWord.ActiveDocument.Range.Font.Name := 'Arial';
>      MSWord.ActiveDocument.Range.Font.Size := 24;
>      AString := MSWord.ActiveDocument.Range.Font.Name;
>      Edit1.Text := AString;
>end;

>end.

>John Kean <kew...@wagresearch.criw.nwz but without the 'w's> wrote in
>article <6duuah$jg...@wnnews1.netlink.net.nz>...

>> Help help!

>> I need to retrieve the name of the current font from a Word document to
>be
>> used by my Delphi OLE application. The WordBasic command for this is
>> "Font$()". Please note that this is different from the WordBasic "Font"
>> command: "Font" is a procedure which *sets* the font in the Word
>document,
>> while "Font$()" is a function that *returns* the font in use in the
>document.

>> My probelm is that I can't seem to find the correct syntax to include the

>> "Font$()" command in Delphi. The "$" bit in particular causes Delphi to
>have a
>> hissy-fit.

>> Any suggestions?

>> John K

>>    \\\\   John Kean
>>    OO |   ~~~~~~~~~
>>    <  )   kew...@wagresearch.criw.nwz
>>    \/ |   (there's really no 'w's in my email address)
>>    `--

This is OK, assuming that it's Word 8 & not Word 7.
The answer is a little bit klugy.  You have two options:
1)  In Word 7,  write a macro that stores the name of the font in some
location, such as in the 1st 10 char of the document or at the end,
following/preceding a delimiter.  Call that macro from Delphi via OLE & word's
ToolsMacro.  You can then use cutting & pasting to get the string, via the
clipboard, and OLE control of word's EditCopy.

or, 2)  Use OLE control of editselectall & editcopy to get the entire document
on the clipboard.  Paste it to a TRichEdit, and process  the selattributes
property within Delphi.

Trav

Other Threads