Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-06-2023, 10:57 PM
pctechtv pctechtv is offline Save file dynamically with suffix Windows 10 Save file dynamically with suffix Office 2016
Novice
Save file dynamically with suffix
 
Join Date: Apr 2017
Posts: 3
pctechtv is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 06-08-2023, 06:34 PM
BrianHoard BrianHoard is offline Save file dynamically with suffix Windows 10 Save file dynamically with suffix Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

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
-----VBA code: (Be sure to change the paths to yours)
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
Reply With Quote
Reply

Tags
dynamic content, save, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Save file dynamically with suffix 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
Save file dynamically with suffix Have Powerpoint dynamically update from an excel or other file? oshkosh PowerPoint 3 04-05-2016 06:10 AM
Save file dynamically with suffix 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft