Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Word automation find and replace
Message
From
02/12/2004 03:06:22
 
 
To
01/12/2004 15:33:21
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00966187
Message ID:
00966278
Views:
17
Michel,

I use the routine below. It works in headers. Give it a try
*----------------------------------------------------
function SearchAndReplace_Word
	lparameters	WordObj, SearchFor, ReplaceWith, MatchCase, MatchWholeWord, ;
				MatchWildCards, MatchSoundsLike, MatchAllWordForms, FormatInfoAlso

	#define wdFindStop     0
	#define wdFindContinue 1
	#define wdFindAsk      2

	#define wdReplaceNone  0
	#define wdReplaceOne   1
	#define wdReplaceAll   2

	local Forward, Wrap 
	
	for each StoryRange in WordObj.ActiveDocument.StoryRanges
		with StoryRange
			.WholeStory
			.Find.ClearFormatting
			.Find.Replacement.ClearFormatting
		
			Forward = TRUE
			Wrap = wdFindContinue
			
			&& replacement max 254 chars
			do case
			case len(ReplaceWith) <= 254
				=StoryRange.Find.Execute(SearchFor, MatchCase, MatchWholeWord, MatchWildCards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, FormatInfoAlso, ReplaceWith, wdReplaceAll, , , , )
			otherwise
				local x, xReplace, ReplaceLeft
				ReplaceLeft = ReplaceWith
				
				do while !empty(len(ReplaceLeft) )
					do case
					case len(ReplaceLeft) < 255
						xReplace = ReplaceLeft
						ReplaceLeft = ''
					otherwise
						xReplace = left(ReplaceLeft, 254 - len(SearchFor)) + SearchFor
						ReplaceLeft = Substr(ReplaceLeft, 254 - len(SearchFor) + 1)
					endcase
					
					=StoryRange.Find.Execute(SearchFor, MatchCase, MatchWholeWord, MatchWildCards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, FormatInfoAlso, xReplace, wdReplaceAll, , , , )
			
				enddo
			endcase
		endwith
	endfor
	#undefine wdFindStop
	#undefine wdFindContinue
	#undefine wdFindAsk

	#undefine wdReplaceNone
	#undefine wdReplaceOne
	#undefine wdReplaceAll
endfunc
*--------------------------------------------------------------------------
>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
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform