Как я могу переименовать свой customVariable в слове? У меня есть Customvaraiable под названием «Номер документа», но теперь я хотел бы переименовать его в _DocumentNumber «изменить имя CustomDocumentProperties в слове с vba
Я знаю, что могу создать новый CustomVariable, удалить старый, но я тогда борется с тем, как обновлять все ссылки в документе на новый
Отредактированный код:.
Sub test()
Dim A As word.Field
Dim FldUpd As String
Dim findText As String
Dim replaceText As String
findText = "Document Number"
replaceText = "_DocumentNumber"
ActiveWindow.View.ShowFieldCodes = False
If ActiveDocument.CustomDocumentProperties(findText).value = "" Then Exit Sub
For Each A In ActiveDocument.Fields
If A.result.Text = ActiveDocument.CustomDocumentProperties(findText).value Then
Call WordProperties.createCustomDocumentProperty(ActiveDocument.name, replaceText, ActiveDocument.CustomDocumentProperties(findText).value)
ActiveWindow.View.ShowFieldCodes = True
A.Select
'DOCPROPERTY "Document number" \* MERGEFORMAT
With Selection.Find
.Text = "DOCPROPERTY*" & findText
.Replacement.Text = "DOCPROPERTY " & replaceText
.Format = True
.MatchCase = False
.MatchWildcards = True
End With
tempBool = Selection.Find.Execute(replace:=wdReplaceAll)
' Refresh fields
ActiveDocument.Fields.update
ActiveWindow.View.ShowFieldCodes = False
If tempBool Then ActiveDocument.CustomDocumentProperties(findText).Delete
End If
Next
End Sub
Отредактировано: проблема заключается в том, что метод находкой не возвращает истинный
Вы все еще создавать/удалять docproperty внутри цикла. Но это не свойство поля, а документа. Таким образом, он принадлежит снаружи. – Andre