How to extract part of the filename as data field - Microsoft Word

Alessandro Gardani 20 Reputation points
2023-10-07T12:29:47.9466667+00:00

Hello, I need to improve the Quality-Assurance of technical reports.

To do this, I have to fill the document code in the word document directly from the filename.

The document code is structured by couples of numbers, therefore the filling will be done extracting pieces of the document code.

I think I have to use the "data fields" but I can't find the correct formula.

Any help?

example

DOCUMENT NAME "IN2G11EZZRRMD0000001A.docx"

User's image

Here's a word samplehttps://spaalpina-my.sharepoint.com/:w:/g/personal/alessandro_gardani_alpina-spa_it/EU4nH4T0LgRJjHhuj32afAkBJOuzEfR81-l1WPDtVrhxKw?e=tT2j6K

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Word | For business | Windows
{count} votes

Accepted answer
  1. Charles Kenyon 3,241 Reputation points Volunteer Moderator
    2023-10-09T16:05:44.14+00:00

    Here is a temporary link to a .docm version of your document.

    https://www.dropbox.com/scl/fi/p6o42xr9vkzp2s09pvzv1/IN2G11EZZRRMD0000001A-Deleteme.docm?rlkey=f8xxecljdhopsf4k4apv3dqmz&dl=0

    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:

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.