Link to home
Start Free TrialLog in
Avatar of lukeMH
lukeMH

asked on

Delphi - MS Word Automation - Finding text + writing to headers

Hi,

I have been experimenting with automation in delphi to access MS word doccuments. I am finding that there is an extreme lack of doccumentation available on the web. If anyone has any links to good information sites for delphi / word automation (how to reference objects) that would be a great start.

I have been reading the VBAWD9 help file, but unfortunatly it doesn't translate well into delphi. For example, the help file details the object 'headers' and 'headersfooter' but trying to access either of those gives me an exception.



My question is in 3 parts

(1) How do I fist of all search for some text in a word doccument and then preform actions like 'Homekey' 'Endkey'  (example: I want to find the word 'action' in a word doccument and then go to the beginning of the line that word is found in)

(2) How do I select text inbetween 2 points? I want to find 2 different words in the doccument and select all the text / paragraphs that apear inbetween those 2 words

(3) How do I modify a header? I have a document with a header that contains a table. I need to add a number (a document reference number) to a specific cell in that table.



Here is an example of the code i have been playing with so far that copies data from a cell in a specified table.

procedure TForm1.Button1Click(Sender: TObject);

var
  WordApp : Variant;
  Range : Variant;
  i : integer;
  Selection : Variant;

begin
  WordApp := CreateOleObject('Word.Application');
  WordApp.Visible := True;
  Wordapp.documents.open('c:\test1.doc');
  Range := WordApp.Documents.Item(1).Range;
  WordApp.documents.item(1).tables.item(3).cell(1,1).select;
  WordApp.selection.copy;
  memo1.lines.clear;
  Memo1.pastefromclipboard;
  WordApp.documents.item(1).Close;
  WordApp.Quit;
end;
SOLUTION
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of robert_marquardt
robert_marquardt

There is documentation. You are simply looking in the wrong place.

1. Start the Word macro recording
2. Do what you want by hand in Word
3. Stop macro recording
4. Open the recorded macro in the macro editor

If you have installed the VBA help for Word then you now have access to all the info you need. Just press F1.
The macro is nothing but a bit of VB glue around the automation objects of Word.

go to location
($Delphi)\OCX\Servers
open WordXXX.pas You can find there a lot interesting properties :)

ziolko.
Avatar of lukeMH

ASKER

Hi guys, thanks for your replys

I am still struggling to do the 3 things I need to do. I am finding it very hard to translate from the VBA help to something that works in delphi.

Luke

Inserting text in header: see

http://www.swissdelphicenter.ch/en/showcode.php?id=81

Controling and using MS Word from Delphi.
Appendix A – Full TWinWord class source
The full source for the tWinWord class is show below. This include the implementation of all methods:
unit doc;
...

See:

http://clk.about.com/?zi=1/XJ&sdn=delphi&zu=http%3A%2F%2Fvzone.virgin.net%2Fgraham.marshall%2Fword.htm%23word.htm

See also:
http://www.softmosis.ca/wordfaq.phtml#GetData



Found on microsoft web site (page in French but not the code)
©2006 Microsoft Corporation. All rights reserved.
the link: http://clk.about.com/?zi=1/XJ&sdn=delphi&zu=http%3A%2F%2Fsupport.microsoft.com%2Fsupport%2Fkb%2Farticles%2Fq229%2F3%2F10.asp

partial excerpt for your information (see the page for the complete unit):

    procedure InsertLines(LineNum : Integer);
    procedure CreateMailMergeDataFile;
    procedure FillRow(Doc : Variant; Row : Integer;Text1,Text2,Text3,Text4 : String);
...
procedure TForm1.InsertLines(LineNum : Integer);
var
  iCount : Integer;
begin
  for iCount := 1 to LineNum do
     wrdApp.Selection.TypeParagraph;
end;

procedure TForm1.FillRow(Doc : Variant; Row : Integer;
                 Text1,Text2,Text3,Text4 : String);

begin
  Doc.Tables.Item(1).Cell(Row,1).Range.InsertAfter(Text1);
  Doc.Tables.Item(1).Cell(Row,2).Range.InsertAfter(Text2);
  Doc.Tables.Item(1).Cell(Row,3).Range.InsertAfter(Text3);
  Doc.Tables.Item(1).Cell(Row,4).Range.InsertAfter(Text4);
end;

...

  // Insert a new table with 9 rows and 4 columns
  wrdDoc.Tables.Add(wrdSelection.Range,9,4);
  wrdDoc.Tables.Item(1).Columns.Item(1).SetWidth(51,wdAdjustNone);
  wrdDoc.Tables.Item(1).Columns.Item(2).SetWidth(170,wdAdjustNone);
  wrdDoc.Tables.Item(1).Columns.Item(3).SetWidth(100,wdAdjustNone);
  wrdDoc.Tables.Item(1).Columns.Item(4).SetWidth(111,wdAdjustNone);
  // Set the shading on the first row to light gray

  wrdDoc.Tables.Item(1).Rows.Item(1).Cells.Shading.BackgroundPatternColorIndex := wdGray25;
  // BOLD the first row
  wrdDoc.Tables.Item(1).Rows.Item(1).Range.Bold := True;
  // Center the text in Cell (1,1)
  wrdDoc.Tables.Item(1).Cell(1,1).Range.Paragraphs.Alignment := wdAlignParagraphCenter;

  // Fill each row of the table with data
  FillRow(wrdDoc, 1, 'Class Number', 'Class Name', 'Class Time', 'Instructor');
  FillRow(wrdDoc, 2, 'EE220', 'Introduction to Electronics II', '1:00-2:00 M,W,F', 'Dr. Jensen');
...
  // Go to the end of the document

  wrdApp.Selection.GoTo(wdGotoLine,wdGoToLast);
  InsertLines(2);

and so on.

I think that with this you should be able to write the code you need.


Avatar of lukeMH

ASKER

Thanks for your help.

I still cannot do the headers.

I have a doccument that has a table in the header, and I need to write a value to one of the cells.

I tried using the example that bernani supplied, but I can't manage to select anything in the table that is in the header

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If needed, you can use const by their name if you declare them under the const section

{const

wdNormalView = 1;
wdOutlineView = 2;
wdPrintView = 3;
wdPrintPreview = 4;
wdMasterView = 5;
wdWebView = 6;

wdSeekFirstPageFooter = 5;

wdSeekCurrentPageHeader = 9;
wdSeekCurrentPageFooter = 10;

}

lukeMH,

Did you test the code I supplied you ? Any feedback ?
Avatar of lukeMH

ASKER

Sorry for my delay in getting back to you. I think I now have all the code examples I need.

Many Thanks for your help guys