Specify filename – document naming
Having the created document automatically saved is very convenient:
- It guarantees a consistent approach, making a search for a document using the folder and filename easy.
- It saves time and effort when saving a new created document – one less thing to consider, no distracting typing and cut & paste.
- It supports creation of multiple documents in one go which will probably become a mess when all created documents stay open, having to be named individually.
Note before actually using the automated naming feature, make sure you know which are the document naming standards that apply to your organization.
If there are none, and you are free to decide yourself, you may want to read Document naming conventions (unimelb.edu.au) as an example.
In the following I will explain how in the document creation process wizard you can specify how the document will be named:
Composing the filename using VBA code
The document naming will be done using a line of code which concatenates and transforms the required data elements. In the VBA code generated on completion of the wizard there will be a line like this:
strFileName = ReplaceIllegalCharacters(ControlValue(![CustomerID]), "_") & _
ReplaceIllegalCharacters(ControlValue(![Invoice Date], Format:="yymmdd"), "_")
In this example we see a couple of function calls: ReplaceIllegalCharacters and ControlValue:
ReplaceIllegalCharacters
This function replaces all characters which are not acceptable to the file system by a character – here the underscore (_), but you may also choose empty string (“”).
ControlValue
In the above there are two calls to the ControlValue function. In the first call it picks up the display value of the CustomerID control on the form which shows the name of the company.
Note: getting the name of the company could of course have been implemented differently, such as having an extra text control specifically for the name.
The second call shows the use of the Format argument whose expression makes the name accessible for sorting on date which may be useful.
Extending and improving the naming expression
At any point, in the Code text box in the wizard or in the generated VBA code, you can adapt the code. For example, you may decide to
- Include strings in the naming expression
- Include any valid VBA function expression
strFileName = "invoice" & ControlValue(![CustomerID], UseDisplayValue:=False), Weekday()
- Make ReplaceIllegalCharacters surround all ControlValue function call so ReplaceIllegalCharacters is called only once:
strFileName = ReplaceIllegalCharacters(ControlValue(![CustomerID]) & _
ControlValue(![Invoice Date], Format:="yymmdd"), "_")
- Remove ReplaceIllegalCharacters in the second call it being the case that the Format:=”yymmdd” argument guaranties there will be no illegal characters here:
strFileName = ReplaceIllegalCharacters(ControlValue(![CustomerID]), "_") & _
ControlValue(![Invoice Date], Format:="yymmdd")