I currently have a cope snippet that allows me to extract the first 6 characters of a filename to insert in the right of a document footer and the file title (not including extension) in the left of the footer. It was all based on character position and had served its purpose. However, a complexity has been introduced.
Our previous file naming structure was solely "###### - [Title]"
(where we had 6 digits, a space, dash, a space, a title). We now have additional file names of the format "######.## - [TITLE]"
. I don't believe I can rely solely on character position to achieve extraction and insertion. I'm looking for some assistance in how I might be able to setup the code to determine whether the 7th character is a decimal or a space and proceed accordingly with inserting #####
versus ######.##
into the footer. The flip side would also be true where I would need to determine the actual title (not relying on the position since it would be dependent on the first case.
Any assistance is appreciated. Below is the current code. It might not be the cleanest, but it gets the job done:
Sub FooterFields(myFooterHL, myTotalPageCount, myFooterBold)
sTitle = ActiveDocument.Name
J = InStrRev(sTitle, ".")
If J > 0 Then
sTitle = Left(sTitle, J - 9)
If Len(sTitle) > 5 Then
sTitle = Left(sTitle, 9)
End If
Dim specTitle As String
Dim specTitle2 As String
Dim specTitleInt As String
specTitle = ActiveDocument.Name
If Right(specTitle, 5) = ".docx" Then
specTitleInt = Left(specTitle, Len(specTitle) - 5)
specTitle2 = Right(specTitleInt, Len(specTitleInt) - 9)
ElseIf Right(specTitle, 4) = ".doc" Then
specTitleInt = Left(specTitle, Len(specTitle) - 4)
specTitle2 = Right(specTitleInt, Len(specTitleInt) - 9)
Else
End If
sDiv = ActiveDocument.Name
K = InStrRev(sDiv, ".")
If K > 0 Then
sDivIntermediate = Right(sDiv, K - 6)
sDivFinal = specTitle2
If Len(sDiv) > 5 Then
sDivIntermediate = Right(sDiv, K - 6)
sDivFinal = specTitle2
End If
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.WholeStory
Selection.Delete
Selection.Font.AllCaps = True
With Selection
.Range.Text = sTitle
.Range.InsertAlignmentTab wdRight, wdMargin
.EndKey Unit:=wdLine
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE ",
PreserveFormatting:=True
If myTotalPageCount = vbYes Then
.TypeText Text:="/"
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="NUMPAGES ", PreserveFormatting:=True
Else
End If
.HomeKey Unit:=wdLine
.Range.Text = sDivFinal
End With
PageNumberAlignment:=wdAlignPageNumberRight, FirstPage:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Else
MsgBox "Document has no filename extension."
End If
End If
End Sub