Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Lose unused variables? (Word97/WinNT)

    Edited to correct sub - got me """"""'s in a muddle!

    Just a quick question - the template I'm working on uses a lot of docvariables; around 30. However, during the design of this template quite a lot others were added and have since been deleted - hence getting a list of variables in use in the document (as below) actually brings back 113!

    How do I tell Word that if they're not there now it can delete them?!
    <pre>Sub ListThemAll()
    Set adVar = Nothing
    Set adVar = ActiveDocument.Variables
    Dim i, j As String
    i = 1
    j = ""
    For i = i To adVar.Count
    j = j + adVar(i).Name + " = """ + adVar(i).Value + """" + Chr(13)
    Next i
    Application.Documents.Add
    ActiveDocument.Content = j
    End Sub</pre>

    (Hans - note, I'm beginning to get the hang of loops at last!)
    Beryl M


  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Lose unused variables? (Word97/WinNT)

    What is your definition of "not there now"?

    In other words how do we know which ones you want to delete?

    You will run into problems if you try to delete inside that loop because as soon as you delete one then the next item found is two items later (as all the later items slip back a position). You will find sample code here where the loop counts backwards to avoid this issue.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lose unused variables? (Word97/WinNT)

    Hi Beryl,

    As Andrew notes, you will have to define what "not there now" is in order to decide which ones to delete.

    Notes:

    1. The line

    Dim i, j As String

    declares i without explicitly specifying a variable type, hence i is a Variant. In your code, i is used as a loop counter, so you probably want

    Dim i As Long, j As String

    2. The line

    i = 1

    is superfluous, for the loop instruction

    For i = 1 To ...

    initializes i at 1.

  4. #4
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lose unused variables? (Word97/WinNT)

    For instance, with fields showing, the text might now look something like this:
    <font color=blue>"The loan is for the purpose of { docvariable "purpose" } and will be for
    Beryl M


  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lose unused variables? (Word97/WinNT)

    <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15> And I thought I was doing well, too!

    Noted, sir, and corrected in my code. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    'Not there now' defined in reply to Andrew.
    Beryl M


  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lose unused variables? (Word97/WinNT)

    > The variable "rate" is no longer there - but if you run the sub I put in my original post, it will quote "rate" in the list.

    This is contradictory. Apparently "rate" is still there, otherwise it wouldn't be listed. What does your sub list as value for "rate"?

  7. #7
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lose unused variables? (Word97/WinNT)

    Er - just read your amendment ... 'sample code here' - where, please?
    Beryl M


  8. #8
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lose unused variables? (Word97/WinNT)

    Whatever the last value was for rate - usually a space, since if you let the variable be empty the VBA claims it's been deleted although it's still there (the reverse of what it does if you DO delete the variable!).

    And yes, I know it's contradictory, but it's still true!
    Beryl M


  9. #9
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lose unused variables? (Word97/WinNT)

    Here's an example - this document contains four docvariables and nothing else. Run the code above on it, though, and you get a list of 113 variables ...
    Attached Files Attached Files
    Beryl M


  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lose unused variables? (Word97/WinNT)

    I haven't seen this behavior either in Word 2002 (the version I'm using now) or in Word 97 (which I used from 1998 until last year). Deleting a variable explicitly through ActiveDocument.Variables("MyVar").Delete removes it from the Variables collection (and hence from the list produced by your sub), and setting a variable to an empty string "" through ActiveDocument.Variables("MyVar") = "" does the same.

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Lose unused variables? (Word97/WinNT)

    Your document contains only 4 FIELDs of type DocVariable, but its Variables collection contains 113 elements. Just removing a FIELD of type DocVariable does NOT remove the corresponding element of the Variables collection.

    Here is an example of code looping backwards through the Variables collection to remove ALL elements (Andrew meant "in this forum" when he wrote "here"):

    Sub DeleteThemAll()
    Dim i As Integer
    For i = ActiveDocument.Variables.Count To 1 Step -1
    ActiveDocument.Variables(i).Delete
    Next i
    End Sub

    The reason for looping backwards is that if you try to delete items while looping forwards, you are pulling the rug from under the loop, as it were.

  12. #12
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Lose unused variables? (Word97/WinNT)

    I see - I hadn't appreciated that there was a distinction before! So I need to write a variation of your code that checks whether a corresponding field exists for each variable and if not delete the variable ... <img src=/S/hmmn.gif border=0 alt=hmmn width=15 height=15>

    I'll have to put my thinking cap on - ta ever so muchly!
    Beryl M


  13. #13
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lose unused variables? (Word97/WinNT)

    Hi Beryl

    Before I saw your most recent post, I understood the problem to be one of just cleaning out "placeholder" variables, that had the value " ". To this end, the code below worked. ListVariables() came up with 113 variables, CleanUpVars() removed the placeholders, and then ListVariables() showed only the two - mgr and for. It might still be of use to you:
    <pre>Option Explicit

    Public Sub CleanUpVars()
    Dim oVar As Variable
    With ThisDocument
    For Each oVar In .Variables
    If oVar.Value = " " Then oVar.Delete
    Next oVar
    End With
    End Sub

    Public Sub ListVariables()
    Dim oVar As Variable, msg As String, count As Integer
    msg = ""
    count = 0
    With ThisDocument
    For Each oVar In .Variables
    msg = msg & oVar.Name & " = " & Chr(34) & oVar.Value & Chr(34) & vbTab
    count = count + 1
    If count = Int(count / 5) * 5 Then msg = msg & vbCrLf
    If count = Int(count / 50) * 50 Then
    MsgBox msg
    msg = ""
    End If
    Next 'oVar
    End With
    MsgBox msg
    MsgBox "Variable count is " & count
    End Sub
    </pre>


    Alan

  14. #14
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Lose unused variables? (Word97/WinNT)

    Beryl,

    Does this Macro do what you want? Make sure you run it on a copy of your document to test <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    StuartR

    <code>
    Public Sub DeleteUnusedDocVars()

    Dim fldNext As Field
    Dim i As Long
    Dim blVariableFound As Boolean
    Dim strFieldCode As String

    For i = ActiveDocument.Variables.Count To 1 Step -1
    blVariableFound = False
    strFieldCode = " DOCVARIABLE """ & ActiveDocument.Variables(i).Name
    For Each fldNext In ActiveDocument.Fields
    If fldNext.Type = wdFieldDocVariable Then
    If Left(fldNext.Code, Len(strFieldCode)) = strFieldCode Then
    blVariableFound = True
    Exit For
    End If <font color=448800>' Left(fldNext.Code, Len(strFieldCode)) = strFieldCode </font color=448800>
    End If <font color=448800>' fldNext.Type = wdFieldDocVariable</font color=448800>
    Next fldNext
    If Not blVariableFound Then ActiveDocument.Variables(i).Delete
    Next i
    End Sub
    </code>

  15. #15
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Lose unused variables? (Word97/WinNT)

    Stuart

    How do you manage to get your "pre" formatted text to appear at normal size?
    Do you use the "big" tags as well?

    Alan

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •