Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Word - replace in headers
Message
From
23/10/2009 18:40:31
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
01429439
Message ID:
01431155
Views:
30
>Can you post your whole solution? I'm sorry, I didn't have a chance to try myself.

Sure, here goes.

In the following code, loWord is a reference to the class that manages Word automation; loWord.oWord is a reference to the Word program; loWord.oDoc is a reference to the active document.

Since I wanted to replace several texts, and not change several times between headers, footers, and main text, I created an array, of texts to be replaced, called the array laErsetzen and then:
WITH loWord.oWord.Selection.Find
...
FOR i = 1 TO 5 && One round each for text, header, second header, footer, second footer
	DO case
	CASE i = 1 && No need to do anything
		*
	CASE i = 2 && Select main header
		loWord.oDoc.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader
	CASE i = 3 && Select second header (page 2 and later)
		loWord.oDoc.ActiveWindow.ActivePane.View.NextHeaderFooter
	CASE i = 4 && Select footer
		loWord.oDoc.ActiveWindow.View.SeekView = wdSeekMainDocument && Back to main document
		loWord.oWord.Selection.HomeKey(wdStory)
		loWord.oDoc.ActiveWindow.View.SeekView = wdSeekCurrentPageFooter
	CASE i = 5 && Select second footer
		loWord.oDoc.ActiveWindow.ActivePane.View.NextHeaderFooter
	ENDCASE
	FOR j = 1 TO lnZeile && Loop through all texts that should be replaced
		lcFindText = laErsetzen(j, 1)
		lcReplaceText = laErsetzen(j, 2)
		lcReplaceText = ALLTRIM(lcReplaceText)
		lcReplaceText = STRTRAN(lcReplaceText, CHR(13) + CHR(10), CHR(10))
		lcReplaceText = STRTRAN(lcReplaceText, CHR(13), CHR(10))
		lcReplaceText = STRTRAN(lcReplaceText, CHR(10), "^l") && For Word, this means "new line"
		.Execute(lcFindText,,,,,,,,, lcReplaceText, wdReplaceAll)
	NEXT
NEXT
Previously I also inserted a page break, which I later deleted; otherwise, the second header and footer can't be selected.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Reply
Map
View

Click here to load this message in the networking platform