Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Word automation find and replace
Message
From
02/12/2004 03:41:50
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
01/12/2004 15:33:21
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00966187
Message ID:
00966284
Views:
12
>Hi
>
>I must do find and replace using OLE automation with Word. I have some code who is working but just in the body of the document. I am not able to find and replace in header and footer. I need to make word search in the whole document including header and footer.
>
>Here is a sample of my code.
>
> LOCAL oWord AS "Word.Application"
> IF TYPE("oWord") <> "O"
> oWord = CREATEOBJECT("Word.Application")
> ENDIF
> oWord.Documents.Open("d:\transit2.doc")
> oWord.VISIBLE = .t.
> loSelection = oWord.ActiveWindow.ActivePane.SELECTION
> loSelection.FIND.ClearFormatting()
>
> WITH loSelection.FIND
> .TEXT = "@Transit"
> .Replacement.TEXT = "81599999"
> .Forward = .T.
> .FORMAT = .F.
> .MatchCase = .F.
> .MatchWholeWord = .F.
> .MatchWildcards = .F.
> .MatchSoundsLike = .F.
> .MatchAllWordForms = .F.
> ENDWITH
> loSelection.FIND.Execute(,,,,,,,,,,2)
>
>Thanks
>
>Mike
#DEFINE wdReplaceNone	0	
#DEFINE wdReplaceOne	1	
#DEFINE wdReplaceAll	2	


Local lcDocument
lcDocument = 'c:\myPath\myDoc.doc'

* Replacement settings
* Replace all <Name> with 'Cetin Basoz'
* Replace all <SomeDate> with long format datetime
Local Array arrReplace[2,2] && Find what in first col, replacement in 2nd
arrReplace[1,1] = '<Name>'
arrReplace[1,2] = 'Cetin Basoz'
arrReplace[2,1] = '<SomeDate>'
arrReplace[2,2] = Transform(Datetime(),'@YL')

Local oWord As 'Word.Application'
oWord = Createobject('Word.Application')
With oWord
	.Documents.Open(lcDocument)
	With .ActiveDocument
		For Each myStoryRange In .StoryRanges
			ReplaceInRange( myStoryRange, @arrReplace )
		Endfor
		myStoryRange = .Null.
	EndWith
	.Visible = .t.
	.Activate
Endwith

Function ReplaceInRange
	Lparameters toRange, taReplacements
	Local ix
	For ix=1 To Alen(taReplacements,1)
		toRange.Find.Execute(taReplacements[ix,1], ;
			.F., .T.,,,,,,, taReplacements[ix,2], wdReplaceAll)
	Endfor
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform