
Here is a temporary link to a .docm version of your document.
It has a macro that creates document variables for each part.
Those are reflected in the docment in DocVariable Fields which are updated at the end of the macro.
This macro is a "kludge" in that it could be made more efficient by using a function. It is possible that the non-macro field formulae you were trying to use could have been made to work, but my skill with those is minimal.
Warning: The macro deletes any existing document variables, so should not be used if there are other variables in the document. A document variable is a vba construct and there were none in your sample.
It has the following macro in it:
Sub FileNameParse()
' Charles Kenyon
' 2023-09-October
' Parse Filename and put in document variables
'
Dim strFileName As String
Dim iLength As Long
Dim strPart As String
Dim oField As Field
On Error GoTo EndSub
'
' GET FILENAME
Let strFileName = ActiveDocument.Name
'
' TEST LENGTH OF FILENAME AND REMOVE EXTENSION
Let iLength = Len(strFileName)
If iLength < 21 Then
MsgBox "Filename is too Short"
GoTo EndSub
End If
Let strFileName = Left(strFileName, 21)
'
' CHECK FOR "." IN NAME INDICATING FILENAME WAS TOO SHORT
If InStr(strFileName, ".") <> 0 Then
MsgBox "Filename is too Short"
GoTo EndSub
End If
'
' DELETE EXISTING DOCUMENT VARIABLES (IF ANY)
With ActiveDocument
For iLength = .Variables.Count To 1 Step -1
.Variables(iLength).Delete
Next iLength
End With
' GoTo EndSub
'
' PARSE OUT PARTS AND ASSIGN TO DOCVARIABLES
' FIRST FOUR - COMMESSA
Let strPart = Left(strFileName, 4)
ActiveDocument.Variables.Add Name:="Commessa", Value:=strPart
Let strFileName = Right(strFileName, 17) 'Remove that part
' NEXT TWO - LOTTO
Let strPart = Left(strFileName, 2)
ActiveDocument.Variables.Add Name:="Lotto", Value:=strPart
Let strFileName = Right(strFileName, 15) 'Remove that part
' NEXT ONE - FASE
Let strPart = Left(strFileName, 1)
ActiveDocument.Variables.Add Name:="Fase", Value:=strPart
Let strFileName = Right(strFileName, 14) 'Remove that part
' NEXT TWO - ENTE
Let strPart = Left(strFileName, 2)
ActiveDocument.Variables.Add Name:="Ente", Value:=strPart
Let strFileName = Right(strFileName, 12) 'Remove that part
' NEXT TWO - TIPO
Let strPart = Left(strFileName, 2)
ActiveDocument.Variables.Add Name:="Tipo", Value:=strPart
Let strFileName = Right(strFileName, 10) 'Remove that part
' NEXT SIX - OPERA_DISIPLINA
Let strPart = Left(strFileName, 6)
ActiveDocument.Variables.Add Name:="Opera_Disciplina", Value:=strPart
Let strFileName = Right(strFileName, 4) 'Remove that part
' NEXT THREE - PROGR
Let strPart = Left(strFileName, 3)
ActiveDocument.Variables.Add Name:="Progr", Value:=strPart
Let strFileName = Right(strFileName, 1) 'Remove that part
' LAST ONE - REV
ActiveDocument.Variables.Add Name:="Rev", Value:=strFileName
'
' UPDATE DOCUMENT VARIABLE FIELDS
With ActiveDocument
For Each oField In .Fields
If oField.Type = wdFieldDocVariable Then oField.Update
Next oField
End With
'
EndSub:
On Error GoTo -1
Set oField = Nothing
'
End Sub
If you open the sample document you can find and copy the relevant document variable fields. You need not allow the macro to run to access those fields.
The macro need not be stored in each document but could be in a Document or Global Template.
It does not check to see if the filename follows your pattern but does check to make sure that the name is at least 21 characters in length. You document should have at least one DocVariable field in it for the macro to run properly. When you first insert a DocVariable field in the document it will display an error if the variable does not exist. This is OK, as the macro creates the variables, but not the fields.
Absent the macro, these fields do not necessarily update themselves. See reference below.
You could save the table with the DocVariable fields as an AutoText entry or QuickPart and insert it easily in documents.
References:
- Instructions for Installing Macros from websites or forums by Graham Mayor, MVP
- Install vba Procedures (Macros) by Greg Maxey
- String Manipulation - Office Support
- Dealing With Fields in Word - my article