我试图找出文档中是否使用了任何cutomvariables,但是如果在页眉或页脚中定义它是否是这种情况我会遇到问题。
这是我的子循环遍历所有customVariables:
Sub deleteCustomVarNotInUse()
Dim doc As Document
Set doc = ActiveDocument
For i = 1 To Documents(doc).CustomDocumentProperties.Count
If CustomProperties.findProperty(doc, Documents(doc).CustomDocumentProperties(i).name) Then
'Delete variable...
End If
Next i
End Sub
以下是我尝试从here
修改的功能Public Function findProperty(doc As Document, findText As String) As Boolean
On Error Resume Next
findProperty = False
Dim rng As Range
Dim intSecCount As Integer
Dim intHFType As Integer
Set rng = Documents(doc).Content
rng.Find.Execute findText:="DOCPROPERTY*" & findText, Forward:=True
If rng.Find.found = True Then
findProperty = True
Exit Function
End If
intSecCount = Documents(doc).Sections.Count
For intSection = 1 To intSecCount
With Documents(doc).Sections(intSection)
For intHFType = 1 To 3
Set rng = Documents(doc).Sections(intSection).Headers(intHFType).Range
rng.Find.Execute findText:="DOCPROPERTY*" & findText, Forward:=True
If rng.Find.found = True Then
findProperty = True
Exit Function
End If
Next intHFType
End With
Next intSection
End Function
答案 0 :(得分:0)
这似乎有效:
Public Function findProperty(doc As Document, findText As String) As Boolean
Dim rngStory As word.Range
Dim oFld As word.Field
findProperty = False
For Each rngStory In doc.StoryRanges
Do
For Each oFld In rngStory.Fields
If oFld.Type = wdFieldDocProperty Then
'Dig a little deeper and see what the field code contains.
If InStr(UCase(oFld.Code.Text), UCase(findText)) Then
findProperty = True
Exit Function
End If
End If
Next oFld
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next rngStory
End Function