Now in 2003, No code is highlighted..
- From: Janet A. Thompson <JanetA@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 13 Apr 2009 06:39:01 -0700
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
--
Janet A.
"Doug Robbins - Word MVP on news.microsof" wrote:
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
"Janet A. Thompson" <JanetA@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:367C50E9-352D-4FA5-9672-F9CD6F855FCE@xxxxxxxxxxxxxxxx
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.
--
Janet A.
"Doug Robbins - Word MVP on news.microsof" wrote:
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
"Janet A. Thompson" <JanetA@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:FB68ABFD-AECE-4C7F-8741-EA9DD536B4F1@xxxxxxxxxxxxxxxx
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
--
Janet A.
"Doug Robbins - Word MVP on news.microsof" wrote:
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
"Janet A. Thompson" <JanetA@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:3E37AB36-06CC-454A-8FCB-B6D67E35F95D@xxxxxxxxxxxxxxxx
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!
--
Janet A.
"Janet A. Thompson" wrote:
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.
- Follow-Ups:
- Re: Now in 2003, No code is highlighted..
- From: Doug Robbins - Word MVP on news.microsoft.com
- Re: Now in 2003, No code is highlighted..
- References:
- How to do: Open folder, open each file, change header text, save a
- From: Janet A. Thompson
- RE: How to do: Open folder, open each file, change header text, save a
- From: Janet A. Thompson
- Re: How to do: Open folder, open each file, change header text, save a
- From: Doug Robbins - Word MVP on news.microsoft.com
- Code I used
- From: Janet A. Thompson
- Re: Code I used
- From: Doug Robbins - Word MVP on news.microsoft.com
- Re: yellow highlight over first line of code name
- From: Janet A. Thompson
- Re: yellow highlight over first line of code name
- From: Doug Robbins - Word MVP on news.microsoft.com
- How to do: Open folder, open each file, change header text, save a
- Prev by Date: Re: How to validate WORD 2007 forms using VBA
- Next by Date: RE: How to do: Open folder, open each file, change header text, save a
- Previous by thread: Re: yellow highlight over first line of code name
- Next by thread: Re: Now in 2003, No code is highlighted..
- Index(es):
Relevant Pages
|
Loading