How to do: Open folder, open each file, change header text, save a

  • Thread starter Janet A. Thompson
  • Start date
J

Janet A. Thompson

I want my computer to open a folder , or if necessary I could be in that
folder, and for each file in the folder, go into the header text and remove
the text SP-001, close the header, do a Save As and alter the existing file
name so that the BP#1 is removed from the file name. The Save As could save
it in the same or a different folder.

How to do?

Word 2003 XP, probably with the latest Service Pack

-- \
Janet A.
 
J

Janet A. Thompson

I clicked the second link but it takes me right back to this Forum.
I copied the code from the first link into my VB editor then made the
document a template and am trying to get a dialog box to appear but nothing.
 
J

Janet A. Thompson

I copied the code into the Normal.dot and from Tools, Commands, dragged that
BatchReplaceAll macro up to the toolbar. I don't know how to get a dialog
box to open for the first document. Am I headed in the right direction?
 
J

Jay Freedman

Yes, you are headed in the right direction. I forgot to point out at least
one gotcha in that article: The line

PathToUse = "C:\Test\"

has to be edited to use the actual path to the folder that contains the
documents you want to change. (A more friendly approach would have been to
display a dialog to let you pick the folder from a list, but the article was
written a long time ago with the intention of concentrating on the
replacement part of the macro.)

The other thing that needs attention is that the macro currently saves the
changes in the same document file by running the line

myDoc.Close SaveChanges:=wdSaveChanges

To change the name as you specified in your first post, that would have to
be replaced by these lines:

myDoc.SaveAs FileName:=Replace(myDoc.FullName, "BP#1", "")
myDoc.Close SaveChanges:=wdDoNotSaveChanges
 
J

Janet A. Thompson

I did the two changes you directed. When I view my code (Alt +F11) and look
in upper left corner box, I see I am "in" the New Module. I went Tools,
Customize in 2003 and pulled the macro up tothe toolbar. When I open the
doucment, I click on that and the screen goes totally blank.!!! with no
document open but the Word toolbar showing.

If I knew the basics of VB that would help. What mini course on VB is out
there or in Houston that I could take to get the basics down. What do you
think I am doing wrong above? Thanks!
 
D

Doug Robbins - Word MVP on news.microsoft.com

Show us the code of the macro that you have created.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

Janet A. Thompson

Option Explicit

Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

PathToUse = "\server file path"

'Error handler to handle error generated whenever
'the FindReplace dialog is closed

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will
'only be displayed for the first document

FirstLoop = True

'Set the directory and type of file to batch process

myFile = Dir$(PathToUse & "*.doc")

While myFile <> ""

'Open document
Set myDoc = Documents.Open(PathToUse & myFile)

If FirstLoop Then

'Display dialog on first loop only

Dialogs(wdDialogEditReplace).Show

FirstLoop = False

Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub

Else

'On subsequent loops (files), a ReplaceAll is
'executed with the original settings and without
'displaying the dialog box again

With Dialogs(wdDialogEditReplace)
.ReplaceAll = 1
.Execute
End With

End If

'Close the modified document after saving changes

myDoc.SaveAs FileName:=Replace(myDoc.FullName, "BP#1", "")
myDoc.Close SaveChanges:=wdDoNotSaveChanges

'Next file in folder

myFile = Dir$()

Wend

End Sub
 
D

Doug Robbins - Word MVP on news.microsoft.com

I would think that your main problem is that

PathToUse = "\server file path"

will not return anything useful.

If you replace that with

Dim fd as FileDialog

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Select the folder containing the files."
If .Show = -1 Then
PathToUse = .SelectedItems(1) & "\"
Else
End If
End With

then when you run the code, a dialog box will open that you can use to
navigate to the folder that contains the files.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

Janet A. Thompson

I replaced the "bad" code with what you wrote. I have to use 2007 at this
site and when I double clicked on the macro name in the Add-In bar, the VB
editor opens showing a yellow highlight over the first line of the vb name.

yikes; I will try on 2003 tomorrow.
 
D

Doug Robbins - Word MVP on news.microsoft.com

If the code errors in 2007 it will also error in 2003. Exactly what line of
code is highlighted in Yellow.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

Janet A. Thompson

Now in 2003, in a template that has been added as a global templated and is
checked in the Templates and Add in box, I have this code which now simply
opens Word without any document open. No dialogue box opens.
What I want it to do is open all the files in the folder and save as the
file name minus the BP#1 in the file name.

Option Explicit

Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

Dim fd As FileDialog

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "Select the folder containing the files."
If .Show = -1 Then
PathToUse = .SelectedItems(1) & "\"
Else
End If
End With



'Error handler to handle error generated whenever
'the FindReplace dialog is closed

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will
'only be displayed for the first document

FirstLoop = True

'Set the directory and type of file to batch process

myFile = Dir$(PathToUse & "*.doc")

While myFile <> ""

'Open document
Set myDoc = Documents.Open(PathToUse & myFile)

If FirstLoop Then

'Display dialog on first loop only

Dialogs(wdDialogEditReplace).Show

FirstLoop = False

Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub

Else

'On subsequent loops (files), a ReplaceAll is
'executed with the original settings and without
'displaying the dialog box again

With Dialogs(wdDialogEditReplace)
.ReplaceAll = 1
.Execute
End With

End If

'Close the modified document after saving changes

myDoc.SaveAs FileName:=Replace(myDoc.FullName, "BP#1", "")
myDoc.Close SaveChanges:=wdDoNotSaveChanges

'Next file in folder

myFile = Dir$()

Wend

End Sub
 
J

Janet A. Thompson

Actually, when I "step into" the Vb editor does open and highlights
Public Sub BatchReplaceAll()
 
D

Doug Robbins - Word MVP on news.microsoft.com

Show us all of the code as you have it now, or send a copy of the template
to me at (e-mail address removed)

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top