Tarumar7
01-17-2016, 10:37 AM
I would appreciate any help with this macro.
What I'm trying to accomplish is with the help of a macro to be able to save an open file (there will be many!) to the
same directory with a consecutive number. With some help I have come up with the following code that checks a certain directory
for the highest number used in the file and saves with the consecutive number (e.g. say the files in the certain directory are
ST14 HP002, ST14 HP056 and ST14 HP087, so it will save the next file as ST14 HP088 and so on). I have made this macro in excel vba editor
(could probably be done with much fewer lines but works fine in Excel) thinking it would also work in Word but for some reason Word
doesn't recognize:
'num(Counter) = Evaluate(str(Counter))' and
'dblMax = Application.WorksheetFunction.Max(num())'
Also would there be a way to ask for a (create new if necessary) directory when the macro is run the first time for the first file to be saved as
ST14 HP001.
Thank you
Sub fileSaveConsecutive()
Dim dblMax As Double
Dim var_data(200)
Dim var_numdata(200)
'* - * - *
'to put our filenames in our specific directory into an array
Dim MyFile As String
Dim Counter As Long
'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)
Dim str()
ReDim str(1000)
Dim num()
ReDim num(1000)
'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$("C:\HAPPY\SANTA\ELVES\*.docx")
Do While MyFile <> ""
DirectoryListArray(Counter) = MyFile
MyFile = Dir$
str(Counter) = Mid(DirectoryListArray(Counter), 8, 3)
num(Counter) = Evaluate(str(Counter))
Counter = Counter + 1
Loop
'Reset the size of the array without losing its values by using Redim Preserve
ReDim Preserve DirectoryListArray(Counter - 1)
ReDim Preserve str(Counter - 1)
ReDim Preserve num(Counter - 1)
dblMax = Application.WorksheetFunction.Max(num())
Dim nextFilename As String
nextFilename = "C:\HAPPY\SANTA\ELVES\ST14 HP" + Format((dblMax + 1), "000") + ".docx"
ActiveDocument.SaveAs filename:=nextFilename
ActiveDocument.Close
End Sub
What I'm trying to accomplish is with the help of a macro to be able to save an open file (there will be many!) to the
same directory with a consecutive number. With some help I have come up with the following code that checks a certain directory
for the highest number used in the file and saves with the consecutive number (e.g. say the files in the certain directory are
ST14 HP002, ST14 HP056 and ST14 HP087, so it will save the next file as ST14 HP088 and so on). I have made this macro in excel vba editor
(could probably be done with much fewer lines but works fine in Excel) thinking it would also work in Word but for some reason Word
doesn't recognize:
'num(Counter) = Evaluate(str(Counter))' and
'dblMax = Application.WorksheetFunction.Max(num())'
Also would there be a way to ask for a (create new if necessary) directory when the macro is run the first time for the first file to be saved as
ST14 HP001.
Thank you
Sub fileSaveConsecutive()
Dim dblMax As Double
Dim var_data(200)
Dim var_numdata(200)
'* - * - *
'to put our filenames in our specific directory into an array
Dim MyFile As String
Dim Counter As Long
'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)
Dim str()
ReDim str(1000)
Dim num()
ReDim num(1000)
'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$("C:\HAPPY\SANTA\ELVES\*.docx")
Do While MyFile <> ""
DirectoryListArray(Counter) = MyFile
MyFile = Dir$
str(Counter) = Mid(DirectoryListArray(Counter), 8, 3)
num(Counter) = Evaluate(str(Counter))
Counter = Counter + 1
Loop
'Reset the size of the array without losing its values by using Redim Preserve
ReDim Preserve DirectoryListArray(Counter - 1)
ReDim Preserve str(Counter - 1)
ReDim Preserve num(Counter - 1)
dblMax = Application.WorksheetFunction.Max(num())
Dim nextFilename As String
nextFilename = "C:\HAPPY\SANTA\ELVES\ST14 HP" + Format((dblMax + 1), "000") + ".docx"
ActiveDocument.SaveAs filename:=nextFilename
ActiveDocument.Close
End Sub