From: PJY on 7 Apr 2010 13:14 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!
From: PJY on 7 Apr 2010 13:20 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! > >
From: Fumei2 via OfficeKB.com on 7 Apr 2010 13:36 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
|
Pages: 1 Prev: Form fields vs. document properties Next: Can't delete file?!!? |