Hi all, Once again I need to ask for your expertise. I have created a macro
that will open a file, remove the password, find text in the footer, then
replace that text, close the file, and move on to the next file in the folder
and so far it works OK, except I cannot get it to work on the first file in
the folder. Can anyone tell me where I went wrong? Here's what I have so
far:
Sub ReplaceFooter()
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As String
PathToUse = "U:\FindReplace\"
On Error Resume Next
Documents.Close SaveChanges:=wdPromptToSaveChanges
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
myDoc.Unprotect "cmf"
With ActiveWindow.View
..ShowRevisionsAndComments = False
..RevisionsView = wdRevisionsViewFinal
End With
Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "(ILLINOIS - STD.)"
.Replacement.Text = "(123456)"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "findme"
.Replacement.Text = ""
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub
Thanks in advance for your time and sharing of our knowledge!
|
|
0
|
|
|
|
Reply
|
Utf
|
4/7/2010 5:14:02 PM |
|
Never mind - I found out what my problem was - and it works great now!!
"PJY" wrote:
> Hi all, Once again I need to ask for your expertise. I have created a macro
> that will open a file, remove the password, find text in the footer, then
> replace that text, close the file, and move on to the next file in the folder
> and so far it works OK, except I cannot get it to work on the first file in
> the folder. Can anyone tell me where I went wrong? Here's what I have so
> far:
>
> Sub ReplaceFooter()
> Dim myFile As String
> Dim PathToUse As String
> Dim myDoc As Document
> Dim Response As String
> PathToUse = "U:\FindReplace\"
> On Error Resume Next
> Documents.Close SaveChanges:=wdPromptToSaveChanges
>
> myFile = Dir$(PathToUse & "*.doc")
> While myFile <> ""
> Set myDoc = Documents.Open(PathToUse & myFile)
> myDoc.Unprotect "cmf"
> With ActiveWindow.View
> .ShowRevisionsAndComments = False
> .RevisionsView = wdRevisionsViewFinal
> End With
> Dim myStoryRange As Range
>
> For Each myStoryRange In ActiveDocument.StoryRanges
> With myStoryRange.Find
> .Text = "(ILLINOIS - STD.)"
> .Replacement.Text = "(123456)"
> .Wrap = wdFindContinue
> .Execute Replace:=wdReplaceAll
> End With
> Do While Not (myStoryRange.NextStoryRange Is Nothing)
> Set myStoryRange = myStoryRange.NextStoryRange
> With myStoryRange.Find
> .Text = "findme"
> .Replacement.Text = ""
> .Wrap = wdFindContinue
> .Execute Replace:=wdReplaceAll
> End With
> Loop
> Next myStoryRange
>
> myDoc.Close SaveChanges:=wdSaveChanges
> myFile = Dir$()
> Wend
>
> End Sub
>
> Thanks in advance for your time and sharing of our knowledge!
>
>
|
|
0
|
|
|
|
Reply
|
Utf
|
4/7/2010 5:20:02 PM
|
|
If you know it just needs to action the footers, then you do not need to loop
through ALL of the StoryRanges.
Sub ReplaceFooter_2()
Dim myFile
Dim PathToUse As String
Dim oHF As HeaderFooter
Dim oSection As Section
PathToUse = "U:\FindReplace\"
myFile = Dir$(PathToUse & "*.doc")
Do While myFile <> ""
Documents.Open (PathToUse & myFile)
ActiveDocument.Unprotect "cmf"
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Footers
On Error Resume Next
oHF.Range.Text = _
Replace(oHF.Range.Text, "(ILLINOIS - STD.)", _
"(123456)")
Next
Next
ActiveDocument.Save
ActiveDocument.Close
myFile = Dir()
Loop
End Sub
PJY wrote:
>Never mind - I found out what my problem was - and it works great now!!
>
>> Hi all, Once again I need to ask for your expertise. I have created a macro
>> that will open a file, remove the password, find text in the footer, then
>[quoted text clipped - 47 lines]
>>
>> Thanks in advance for your time and sharing of our knowledge!
--
Gerry
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1
|
|
0
|
|
|
|
Reply
|
Fumei2
|
4/7/2010 5:36:53 PM
|
|
|
2 Replies
1663 Views
(page loaded in 0.068 seconds)
Similiar Articles: find and replace in footer - microsoft.public.word.vba.general ...Hi all, Once again I need to ask for your expertise. I have created a macro that will open a file, remove the password, find text in the footer, t... VBA Find a Replace - microsoft.public.word.vba.generalfind and replace in footer - microsoft.public.word.vba.general ... Hi all, Once again I need to ask for your expertise. I have created a macro that will open a file ... find and replace - microsoft.public.windows.live.mail.desktop ...find and replace in footer - microsoft.public.word.vba.general ... Hi all, Once again I need to ask for your expertise. I have created a macro that will open a file ... Find and Replace - microsoft.public.excel.newusersfind and replace in footer - microsoft.public.word.vba.general ... Hi all, Once again I need to ask for your expertise. I have created a macro that will open a file ... Change font and/or font size in headers, footers and body via VBA ...find and replace in footer - microsoft.public.word.vba.general ... find and replace in footer - microsoft.public.word.vba.general ... Change font and/or font size in ... find in files and replace - microsoft.public.windows.powershell ...find and replace in footer - microsoft.public.word.vba.general ... Hi all, Once again I need to ask for your expertise. I have created a macro that will open a file ... VBA to find filename and replace - microsoft.public.excel ...find and replace in footer - microsoft.public.word.vba.general ... VBA to find filename and replace - microsoft.public.excel ... VBA to find filename and replace ... Find and Replace Macro - microsoft.public.word.vba.general ...find and replace in footer - microsoft.public.word.vba.general ... Hi all, Once again I need to ask for your expertise. I have created a macro that will open a file ... Referencing an unbound text control in a footer - microsoft.public ...find and replace in footer - microsoft.public.word.vba.general ... Referencing an unbound text control in a footer - microsoft.public ... find and replace in footer ... Find/Replace command in Windows Live Mail - microsoft.public ...find and replace in footer - microsoft.public.word.vba.general ... Find/Replace command in Windows Live Mail - microsoft.public ... Does anyone know if the Find/Replace ... Find and Replace in Headers (Microsoft Excel)It is a rather trivial task to access what is stored in the various parts of the header and footer, check them for what you want to find, and then replace it with some ... [Microsoft.Office.Interop.Word] Find and Replace in footerHi, i'm working on VS 2008 SP1 with the Interop Library version 11.0.0.0 and i'm trying to find some text in a word document (Office 2003) and replace it ... Microsoft Word: Find and Replace in Word XP, headers and footers ...headers and footers, word xp, matter of fact: Hi Carol, As a matter of fact there is a way to use the Find and Replace function in Headers and Footers. Go to Edit ... find and replace in footer Word - Word Discussion List Wednesday ...Hi all, Once again I need to ask for your expertise. I have created a macro that will open a file, remove the password, find text in the footer, then re Word.Interop Find and Replace in Header, Footer, BodyI need to know how to discern between using the Word.Interop library to find and replace strings in the header, footer, and body of a Word document. I can ... 7/22/2012 3:20:01 AM
|