G
Greg Maxey
Hi,
I am trying to combine some code that Jay Freedman and Doug Robbins have
posted with the object of renaming a batch of files. The renamed files
should be named with the first couple of words in the test.
The files are all located in C:\Text an named 1.doc, 2.doc, 3.doc etc.
Here is my code, followed by problems I can't resolve:
Option Explicit
Public Sub BatchReNameFiles()
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim fn As String
Dim rg As Range
PathToUse = "C:\Test\"
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
With ActiveDocument
Set rg = .Words(1)
rg.End = .Words(min(9, .Words.Count - 1)).End
fn = Trim(rg.Text) & ".doc"
fn = Replace(fn, "\", "")
fn = Replace(fn, ":", "")
fn = Replace(fn, """", "")
fn = Replace(fn, vbCr, "")
fn = Replace(fn, vbTab, "")
End With
With Dialogs(wdDialogFileSaveAs)
.Name = "C:\Documents\tests\" & fn
.Show
'Trying to use SendKeys to represent ALT+S and save the file. Isn't
working
SendKeys "%s"
End With
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub
Private Function min(a As Long, b As Long)
min = -((a < b) * a + (a >= b) * b)
End Function
Problems:
1) I can't get the SendKeys statement to duplicate ALT+s which completes
the save and closes the dialog box. I must manually click ALT+s or click
the save button to step through the macro. How do I get SendKeys to work?
2) For some reason the While statement repeats the first saved file. I mean
1.doc opens and is saved as say One Little Indian. Then 2.doc is saved as
Two Little Indians, then 3.doc as Three Little Indians. For some reason One
Little Indian.doc opens with the SaveAs One Little Indian dialog displayed.
Any ideas why?
3) The original 1.doc 2.doc and 3.doc files remain in the directory. Can
these be deleted as part of the VBA code?
Thanks All.
I am trying to combine some code that Jay Freedman and Doug Robbins have
posted with the object of renaming a batch of files. The renamed files
should be named with the first couple of words in the test.
The files are all located in C:\Text an named 1.doc, 2.doc, 3.doc etc.
Here is my code, followed by problems I can't resolve:
Option Explicit
Public Sub BatchReNameFiles()
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim fn As String
Dim rg As Range
PathToUse = "C:\Test\"
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
With ActiveDocument
Set rg = .Words(1)
rg.End = .Words(min(9, .Words.Count - 1)).End
fn = Trim(rg.Text) & ".doc"
fn = Replace(fn, "\", "")
fn = Replace(fn, ":", "")
fn = Replace(fn, """", "")
fn = Replace(fn, vbCr, "")
fn = Replace(fn, vbTab, "")
End With
With Dialogs(wdDialogFileSaveAs)
.Name = "C:\Documents\tests\" & fn
.Show
'Trying to use SendKeys to represent ALT+S and save the file. Isn't
working
SendKeys "%s"
End With
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub
Private Function min(a As Long, b As Long)
min = -((a < b) * a + (a >= b) * b)
End Function
Problems:
1) I can't get the SendKeys statement to duplicate ALT+s which completes
the save and closes the dialog box. I must manually click ALT+s or click
the save button to step through the macro. How do I get SendKeys to work?
2) For some reason the While statement repeats the first saved file. I mean
1.doc opens and is saved as say One Little Indian. Then 2.doc is saved as
Two Little Indians, then 3.doc as Three Little Indians. For some reason One
Little Indian.doc opens with the SaveAs One Little Indian dialog displayed.
Any ideas why?
3) The original 1.doc 2.doc and 3.doc files remain in the directory. Can
these be deleted as part of the VBA code?
Thanks All.