![]() |
|
|
|
#1
|
||||
|
||||
|
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
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 |
|
#2
|
|||
|
|||
|
Ok so I tested this code and I got it to do something. Right now it's saving multiple files. First time I ran it, it saved 3 files. The second time I ran it, it nows saves 5 files. 4 our of 5 files have the person's last name then a comma and then first name, then a bunch of numbers. Now that I'm looking at the pattern it looks like the date is our of sequence with this format dd-mm-yy and then the rest of the numbers represents the time which looks to be in the correct format. Let me try and tweak your code a bit and see if I can figure out the date format. Also the last file that does not contain a name is just the date and time. The date and time is in the same format pattern as the rest.
Would you know why this code is saving multiple files? |
|
|
|
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 |
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 |
Have to rename file every time to save workbook
|
intelli | Excel | 3 | 03-27-2014 11:53 PM |
Rename Document & Save
|
d4okeefe | Word VBA | 4 | 05-23-2013 09:35 AM |