#1
|
|||
|
|||
Save file dynamically with suffix
I need a Macro that can save a Word document based on data from a text file. This is because the data will be set dynamically in the Text file on a hard drive. The only thing it needs is a file path. One tricky part is I want it to count the files in the directory and append 0001 to the first file and then 0002, 0003, and 0004, respectively.
For example, in directory C:\Temp I wil have a file name worddocs.txt. That file only will contain a base file path. e.g. C\myDocs\wordsOfWisdon.docx. That is, until it is appended with the file names. Each time it saves, it should be appended with 0001, 0002, 0003... So the file name would be wordsOfWisdon0001.docx. I want a button to press and save it with this filename. I have tried some VBA in the past, but I do not know about accessing the external text file. Thanks for any help. |
#2
|
|||
|
|||
If I understand what you're trying to do, please give this a try. You'll need to change the file paths in these files to your paths. Notice how in the text file, the 2nd line has a 1. This will be read by the script, and from there increment by one each time the script is executed. Saving the current document as somefile0001.docx, somefile0002.docx, etc. To keep track of the incremented saves, the script will update the text file with the new number. So if you close the doc and come back to it another time, it will pick up with the last number recorded.
Hopefully this helps. The text file--------------- Code:
C:\myDocs\wordsOfWisdon.docx 1 Code:
Sub SaveDocumentWithIncrementedSuffix() ' Specify the path to the text file containing the base path and file name Dim filePath As String filePath = "C:\Temp\worddocs.txt." ' Read the base path and file name from the text file Dim baseFilePath As String Dim fileNumber As Integer Open filePath For Input As #1 Line Input #1, baseFilePath Line Input #1, fileNumberStr Close #1 ' Convert the read value to an integer fileNumber = Val(fileNumberStr) ' Extract the existing file extension Dim fileExt As String fileExt = "." & Split(baseFilePath, ".")(UBound(Split(baseFilePath, "."))) ' Remove the existing file extension from the base file path baseFilePath = Left(baseFilePath, Len(baseFilePath) - Len(fileExt)) ' Increment the file number fileNumber = fileNumber + 1 ' Create the new file name with the incremented suffix and removed duplicate file extension Dim newFileName As String newFileName = baseFilePath & Format(fileNumber, "0000") & fileExt ' Save the current document to the new path ActiveDocument.SaveAs2 newFileName ' Update the text file with the incremented file number Open filePath For Output As #1 Print #1, baseFilePath & fileExt Print #1, fileNumber Close #1 End Sub |
Tags |
dynamic content, save, vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Different suffix per entry | Dave T | Excel Programming | 5 | 04-02-2020 12:17 AM |
Title and Suffix in contacts | narvan21 | Outlook | 0 | 12-22-2017 02:00 AM |
Have Powerpoint dynamically update from an excel or other file? | oshkosh | PowerPoint | 3 | 04-05-2016 06:10 AM |
save dialog box promt doesn't save file | brmveen | Word VBA | 2 | 11-04-2015 12:51 AM |
Macro to rename multiple Word file with same suffix | ozil61 | Word VBA | 2 | 05-06-2014 07:36 AM |