Felter kan bruges til mange ting, men du kan ikke lave en beregning som den ønskede uden at bruge programmering.
Du kan f.eks. markere den tekst, som skal optælles, og oprette et bogmærke omkring teksten. Hvis du f.eks. navngiver bogmærket ”TextToCount” og indsætter et bogmærke der, hvor resultatet skal vises, og kalder det ”CountResult”, så vil følgende makro opdatere bogmærket ”CountResult” med antal tegn i bogmærket ”TextToCount”:
Sub ShowCharacterCountInBookmarkCountResult_CountInBookmarkTextToCount()
Dim rngBM As Range
With ActiveDocument
'define a range where bookmark CountResult is
Set rngBM = .Bookmarks("CountResult").Range
'set text in range to the character count
rngBM.Text = .Bookmarks("TextToCount").Range.Characters.Count
're-create the bookmark
.Bookmarks.Add "CountResult", rngBM
End With
Set rngBM = Nothing
End Sub
Vedr. VBA og bogmærker: https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
I stedet for at indsætte antal tegn i et bogmærke kunne du oprette en brugerdefineret egenskab (custom document property) og indsætte et DocProperty-felt til at vise værdien. Så kunne makroen se sådan ud:
Sub ShowCharacterCountInDocPropertyFieldCountResult_CountInBookmarkTextToCount()
Dim rngBM As Range
With ActiveDocument
'set value of property CountResult to the number of characters in the bookmark "TextToCount"
.CustomDocumentProperties("CountResult").Value = .Bookmarks("TextToCount").Range.Characters.Count
'update fields in the relavant place to have the inserted DocProperty field updated with the new value
'assume the field is in the primary header of section 1
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields.Update
End With
End Sub
For information om custom document properties og DocProperty-felter, se min artikel https://wordaddins.com/support/how-properties-and-docproperty-fields-work/
For andre eksempler, se f.eks.: https://word.tips.net/T000519_Word_Count_for_a_Section.html
For hjælp til at installere en makro: https://wordmvp.com/FAQs/MacrosVBA/CreateAMacro.htm