View Full Version : Create File Name when user clicks save as WORD
BCA15555
06-06-2013, 07:37 AM
I am looking for VBA code to capture text from a Read Only Word document (within a table) and create a "Save As" filename based on a few different text strings and a date.
The parts of the file name are:
In cell 1 : Event Name
In cell 2 : Customer
In cell 6 : 2013 09 06
The file name would look like this:
Event name Customer 2013 09 06.doc
I have some experience with VBA but mostly in Excel and that was a long time ago, thanks in advance for any help!
fumei
06-06-2013, 06:25 PM
Option Explicit
Sub FromTableCells()
Dim strFile As String
If Selection.Information(wdWithInTable) = True Then
strFile = CellText(Selection.Tables(1).Range.Cells(1).Range) & _
" " & _
CellText(Selection.Tables(1).Range.Cells(2).Range) & _
" " & _
CellText(Selection.Tables(1).Range.Cells(6).Range)
End If
End Sub
Function CellText(rngCell As Range) As String
CellText = Left(rngCell.Text, Len(rngCell.Text) - 2)
End Function
This assumes the Selection is in the table concerned.
As you do not state the structure of the table (is it one row with six columns, two rows of three columns, three rows of two columns....), it was not possible to use row,columns. So the code use Range.Cells.
Also ALL of the text of the cell is grabbed.
The function grabs the cell range and extracts the text (stripping off the end-of-cell marker).
BCA15555
06-07-2013, 12:56 PM
Thank you so much for the help, I should have been more specific
the table is 2 columns wide and 3 rows down.
How would I get that code to run if the user clicks "Save As"?
Also do I create a module and put the code in there or possibly in the "this document" section that is already there?
Thanks again for taking the time to answer!
Bruce
fumei
06-07-2013, 03:58 PM
You caan override SaveAs by changing the Sub from FromTableCells() to FileSaveAs().
Then ADD a line to do the actual file saveas. I could post that, but you may as well do it yourself. Record a macro doing a file SaveAs, then use the string strFile as the Filename:=, as in:
ActiveDocument.SaveAs FileName:=strFile & ".doc"
As for the structure of the table, as long as you do not have any merged cells, the numbered counting should work. Or you can change it to use row- column.
As for module, yes you could add the Sub to a module (perhaps newmacros if you do the recording yourself). It can also go into This
BCA15555
06-10-2013, 07:32 AM
I will give that a try thanks again for the help!!
Bruce
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.