Log in

View Full Version : Using MS Word Property Content Controls to create Filename



steffie85
10-26-2017, 04:10 AM
Hi Peeps...:hi:



I need help with saving a document PLEEZE. :crying:



Using VBA, I know one can create a button to run a SaveAs procedure. But don't know how to compile it.



I've created Property Content Controls by using INSERT>QUICK PARTS>DOCUMENT PROPERTY> then I've selected AUTHOR, and renamed it to ie. DRIVER (for vehicles). (I've created this to repeat the data in another field)



I've created one for Company, Driver, RegNo and Disc.



I would love to save the file with a push of a button into a file on the server with a new name: Disc-RegNo-Company-Driver
(I know how to create the button, but have no idea what to put into the VBA field)
(If it can be set up to create a new file - on the server - with the same name as the document, it would be a bonus...)



Any help would be appreciated...:bug:

gmaxey
10-26-2017, 05:56 AM
Sub ScratchMacro()
Dim strFileName As String
Dim bInvalid As Boolean
bInvalid = False

With ActiveDocument
Select Case True
Case .SelectContentControlsByTitle("Disc.").Item(1).ShowingPlaceholderText: bInvalid = True
Case .SelectContentControlsByTitle("RegNo").Item(1).ShowingPlaceholderText: bInvalid = True
Case .SelectContentControlsByTitle("Driver").Item(1).ShowingPlaceholderText: bInvalid = True
Case .SelectContentControlsByTitle("Company").Item(1).ShowingPlaceholderText: bInvalid = True
Case Else
strFileName = .SelectContentControlsByTitle("Disc.").Item(1).Range.Text & "-" & _
.SelectContentControlsByTitle("RegNo").Item(1).Range.Text & "-" & _
.SelectContentControlsByTitle("Company").Item(1).Range.Text & "-" & _
.SelectContentControlsByTitle("Driver").Item(1).Range.Text
End Select
End With
If bInvalid Then
MsgBox "File name not defined from document content."
Exit Sub
End If
With Dialogs(wdDialogFileSaveAs)
.Name = strFileName
.Show
End With
lbl_Exit:
Exit Sub
End Sub