View Single Post
 
Old 10-26-2022, 04:30 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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

The snippet of code tests to establish if the field is showing its placeholder text (Click or tap here to enter text), which if empty is what the control should display. However there must actually be a content control in the document with the title "Subject", or nothing happens.

You can test for that:
Code:
Sub SaveAs()
'
' SaveAs Macro
'
'

Dim sName As String, aCC As ContentControl, iSpacePos As Integer
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)
        End If
        iSpacePos = InStr(sName, " ")
        If iSpacePos > 0 Then
            sName = Mid(sName, iSpacePos) & "," & Left(sName, iSpacePos)
            sName = Replace(sName, " ", "")
        End If
        sNow = Format(Now, "dd-mm-yy_HHMMSS")
        sFilename = sName & "_" & sNow & ".docm"

        ActiveDocument.SaveAs2 FileName:=sPath & sFilename, FileFormat:=wdFormatXMLDocumentMacroEnabled
    Next aCC
    Set aCC = Nothing
End Sub
The path as written is fine. It will save to the user's desktop.


You can repeat the content of a content control by inserting a second content control and map it to the first. Both operations are easily achieved using my content control tools - Insert Content Control Add-In
__________________
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