Deleting document variables

  • Thread starter Christopher Brewster via OfficeKB.com
  • Start date
C

Christopher Brewster via OfficeKB.com

I'm trying to set up some document variables whose values appear in the text
but also in headers and footers, and that I can easily change. It all seems
more difficult than it should be. Detecting whether there's already a
variable by a certain name should be easier. But how do I get rid of doc
variables that I don't need? My template now has all the test variables I was
using, and they may cause trouble.
 
G

Greg Maxey

Christopher,

The following code example will show you how you can add variables, evaluate
and process variable deletion 1 by 1, or delete all.

Sub Test()
Dim oVar As Variable
Dim pVar As Variables
Dim i As Long
'Create some variables
With ActiveDocument.Variables
.Item("TestVar1").Value = "This is test 1"
.Item("TestVar2").Value = "This is test 2"
End With
'Evaluate each variable and decide to keep or delete
For Each oVar In ActiveDocument.Variables
If MsgBox("Do you want to delete the variable " & _
oVar.Name & " with value " & oVar.Value, _
vbYesNo) = vbYes Then
oVar.Delete
End If
Next
'Delete all variables
With ActiveDocument.Variables
For i = .Count To 1 Step -1
.Item(i).Delete
Next i
End With
End Sub

When you get your docvariables fields created use a macro something like
this to update:

Sub UpdateFields()
Dim oStory As Range
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top