none
VB.Net - Word 2007/2010 - Delete field if "Error! No document variable supplied." RRS feed

  • Question

  • I have a document template with many docvariable fields, but some of those fields may not apply and return "Error! No document variable supplied."

    I was using a find and replace code (or throwing IF statements into the document template itself) to replace any of those errors with an empty space, but my problem is that this still leaves a blank space, indentation or carriage return in my documents where I don't want them.

    What I'm looking for is something like the below (which doesn't work):    

            If oword.ActiveDocument.Variables("Variable1").Value = "Error! No document variable supplied." Then
                oword.ActiveDocument.Variables("Variable1").Delete()
            End If

    If anyone has a solution to offer that would help me out I would really appreciate it.  Thanks!


    • Edited by MBuchok Thursday, July 26, 2012 10:45 AM
    Thursday, July 26, 2012 10:42 AM

Answers

  • Try the following code sample.

    The problem with the approach you show is that you're deleting the document variables, not the fields...

    Sub RemoveDocVarFieldsWithNoDocVars()
        Dim flds As word.Fields
        Dim fld As word.Field
        
        Set flds = ActiveDocument.Fields
        For Each fld In flds
            If fld.Type = wdFieldDocVariable Then
                If fld.result = "Error! No document variable supplied." Then
                    Debug.Print fld.code
                    fld.Delete
                End If
            End If
        Next
    End Sub


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by MBuchok Friday, July 27, 2012 11:06 AM
    Thursday, July 26, 2012 1:04 PM
    Moderator
  • Cindy's macro works correctly. In my testing it does not delete all fields. FWIW, the following revision to Cindy's code will delete the fields if they contain either:
    Error! Document Variable not defined.
    Error! No document variable supplied.

    Sub RemoveDocVarFieldsWithNoDocVars()
    Dim fld As Word.Field
    For Each fld In ActiveDocument.Fields
      If fld.Type = wdFieldDocVariable Then
        If Left(fld.Result, 6) = "Error!" Then
          fld.Delete
        End If
      End If
    Next
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by MBuchok Friday, July 27, 2012 11:05 AM
    Friday, July 27, 2012 12:50 AM

All replies

  • The error message isn't in the Document Variable, but in the field's display. You could:
    • supply the Document Variable, perhaps populating it with a nullstring; or
    • modify the DOCVARIABLE field coding, like -

    {IF{DOCVARIABLE MyVar}<> "Error!*" {DOCVARIABLE MyVar}}

    The alternative is to delete the DOCVARIABLE field.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Thursday, July 26, 2012 1:04 PM
    Thursday, July 26, 2012 1:01 PM
  • Try the following code sample.

    The problem with the approach you show is that you're deleting the document variables, not the fields...

    Sub RemoveDocVarFieldsWithNoDocVars()
        Dim flds As word.Fields
        Dim fld As word.Field
        
        Set flds = ActiveDocument.Fields
        For Each fld In flds
            If fld.Type = wdFieldDocVariable Then
                If fld.result = "Error! No document variable supplied." Then
                    Debug.Print fld.code
                    fld.Delete
                End If
            End If
        Next
    End Sub


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by MBuchok Friday, July 27, 2012 11:06 AM
    Thursday, July 26, 2012 1:04 PM
    Moderator
  • Thanks for your responses.

    Macropod, your solution is similar to what I've done already, but doesn't create the effect I'm looking for, but thanks for offering a suggestion.

    Cindy, yours is close, except it's deleting ALL the fields in my document whether it holds the error or another value.  Is there something else we can do or maybe it just can't be done? 

    Is it possible to bookmark a location, then programmatically select that bookmark and its contents and delete?  Not sure that would work either... just throwing another idea out there.

    Thursday, July 26, 2012 10:14 PM
  • Cindy's macro works correctly. In my testing it does not delete all fields. FWIW, the following revision to Cindy's code will delete the fields if they contain either:
    Error! Document Variable not defined.
    Error! No document variable supplied.

    Sub RemoveDocVarFieldsWithNoDocVars()
    Dim fld As Word.Field
    For Each fld In ActiveDocument.Fields
      If fld.Type = wdFieldDocVariable Then
        If Left(fld.Result, 6) = "Error!" Then
          fld.Delete
        End If
      End If
    Next
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by MBuchok Friday, July 27, 2012 11:05 AM
    Friday, July 27, 2012 12:50 AM
  • I'll give this another test tomorrow and respond back then.  If it works for you, it should work for me.

    Thanks for the revision!

    Friday, July 27, 2012 3:20 AM
  • I did test before I posted and it was not deleting all the fields in my document...

    <<Is it possible to bookmark a location, then programmatically select that bookmark and its contents and delete?>>

    You certainly could do that, if you wanted to. Just select the field an insert a bookmark.


    Cindy Meister, VSTO/Word MVP

    Friday, July 27, 2012 8:19 AM
    Moderator
  • I must have mixed your macro up with another macro I tried yesterday.  I re-entered what you provided and it worked.

    Thank you!

    Friday, July 27, 2012 11:05 AM