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.Appl ication');
WordApp.Visible := True;
Wordapp.documents.open('c: \test1.doc ');
Range := WordApp.Documents.Item(1). Range;
WordApp.documents.item(1). tables.ite m(3).cell( 1,1).selec t;
WordApp.selection.copy;
memo1.lines.clear;
Memo1.pastefromclipboard;
WordApp.documents.item(1). Close;
WordApp.Quit;
end;
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
var
WordApp : Variant;
Range : Variant;
i : integer;
Selection : Variant;
begin
WordApp := CreateOleObject('Word.Appl
WordApp.Visible := True;
Wordapp.documents.open('c:
Range := WordApp.Documents.Item(1).
WordApp.documents.item(1).
WordApp.selection.copy;
memo1.lines.clear;
Memo1.pastefromclipboard;
WordApp.documents.item(1).
WordApp.Quit;
end;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
go to location
($Delphi)\OCX\Servers
open WordXXX.pas You can find there a lot interesting properties :)
ziolko.
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
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,
...
procedure TForm1.InsertLines(LineNum
var
iCount : Integer;
begin
for iCount := 1 to LineNum do
wrdApp.Selection.TypeParag
end;
procedure TForm1.FillRow(Doc : Variant; Row : Integer;
Text1,Text2,Text3,Text4 : String);
begin
Doc.Tables.Item(1).Cell(Ro
Doc.Tables.Item(1).Cell(Ro
Doc.Tables.Item(1).Cell(Ro
Doc.Tables.Item(1).Cell(Ro
end;
...
// Insert a new table with 9 rows and 4 columns
wrdDoc.Tables.Add(wrdSelec
wrdDoc.Tables.Item(1).Colu
wrdDoc.Tables.Item(1).Colu
wrdDoc.Tables.Item(1).Colu
wrdDoc.Tables.Item(1).Colu
// Set the shading on the first row to light gray
wrdDoc.Tables.Item(1).Rows
// BOLD the first row
wrdDoc.Tables.Item(1).Rows
// Center the text in Cell (1,1)
wrdDoc.Tables.Item(1).Cell
// 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(wdGo
InsertLines(2);
and so on.
I think that with this you should be able to write the code you need.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
{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 ?
Did you test the code I supplied you ? Any feedback ?
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
Many Thanks for your help guys
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.