Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-27-2022, 09:10 PM
gmayor's Avatar
gmayor gmayor is offline Drawing Button With VBA To Rename File And Save Windows 10 Drawing Button With VBA To Rename File And Save Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default


That would be simpler
Code:
Option Explicit

Sub SaveAs()
'
' SaveAs Macro
'
'

Dim sName As String, aCC As ContentControl
Dim sFilename As String, sNow As String, sPath As String
Dim i As Integer
    sPath = Environ("USERPROFILE") & Chr(92) & "Desktop\"
    For Each aCC In ActiveDocument.ContentControls
        If aCC.Title = "Subject" Then
            i = i + 1
            Exit For
        End If
    Next aCC
    If i = 0 Then
        MsgBox "The 'Subject' Content Control is missing", vbCritical
        Exit Sub
    End If
    For Each aCC In ActiveDocument.ContentControls
        If aCC.Title = "Subject" Then
            If aCC.ShowingPlaceholderText = True Then
                MsgBox "Complete the 'Subject' field!", vbCritical
                aCC.Range.Select
                Exit Sub
            End If
            sName = Trim(aCC.Range.Text)
            sName = Replace(sName, Chr(32), "")
        End If
        sNow = Format(Now, "dd-mm-yy_HHMMSS")
        sFilename = sName & "_" & sNow & ".docx"
        ActiveDocument.SaveAs2 FileName:=sPath & sFilename, FileFormat:=wdFormatXMLDocument
    Next aCC
    Set aCC = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #2  
Old 10-27-2022, 10:24 PM
data808 data808 is offline Drawing Button With VBA To Rename File And Save Windows 10 Drawing Button With VBA To Rename File And Save Office 2019
Novice
Drawing Button With VBA To Rename File And Save
 
Join Date: Oct 2022
Posts: 16
data808 is on a distinguished road
Default

Thanks. Had to change your "Next aCC" line to above the ActiveDocument.SaveAs2 line and then it worked. It was saving multiple files or if I didn't fill in the subject then it would have the prompt pop up but it would also save the file without a name of the company in the file name since subject wasn't filled out.

Thank you very much for your help with this. You just made life a lot easier for my coworkers. Take care.
Reply With Quote
  #3  
Old 10-28-2022, 12:41 PM
data808 data808 is offline Drawing Button With VBA To Rename File And Save Windows 10 Drawing Button With VBA To Rename File And Save Office 2019
Novice
Drawing Button With VBA To Rename File And Save
 
Join Date: Oct 2022
Posts: 16
data808 is on a distinguished road
Default

Sorry have one more question. I wanted to change the sPath from the desktop to our network Grive in one of the folders. When I replaced the entire sPath to this:

sPath = "G:\WP61"

The file saved only as far as the root of the Grive and also it did something weird to the file name. It put the Grive folder name that I wanted the file to be saved in, in front of the person's name. I did a test with the name John Doe and this is the file name it generated:

WP61Doe,John_10-28-2022_093118.docx
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to save Word file by field name as filename using VBA button? TribeBuckeyeFan Word VBA 4 02-02-2017 05:58 AM
Drawing Button With VBA To Rename File And Save How to save Word file by field name as filename using VBA button? chemtoli Word VBA 5 01-25-2017 08:06 AM
Save and rename attachments from ZIP FILE AndyDDUK Outlook 1 03-03-2016 12:32 AM
Drawing Button With VBA To Rename File And Save Have to rename file every time to save workbook intelli Excel 3 03-27-2014 11:53 PM
Drawing Button With VBA To Rename File And Save Rename Document & Save d4okeefe Word VBA 4 05-23-2013 09:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:05 PM.


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