Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question "" in userform deletes docvariable

    I have a userform and there are several variables that I allow to be blank (""). When a user leaves one of these fields blank and my code writes the "" to the docvariable the variable is deleted. I think this is a "feature" of Word 2007.

    Is there a way to prevent this because I allow the user to reopen the userform and when the form initializes I get an error because the variable no longer exist. I want the user to be able to add a non-blank vlaue later if they change their mind.

    Thank you

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    You need to adapt your code to assign a unique character (maybe a hyphen?) to the variable when the field is left blank.

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I am wrestling with this at present and can think of a couple of workarounds for this problem.
    1. Create a function to check for the presence of the docvariable and returns the value if found or "" if not found
    2. Follow Jefferson's suggestion and then if you are using the value in a field you can nest it to not show {If {DocVariable aa} = "-" "" {DocVariable aa}}
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    New Lounger
    Join Date
    Mar 2015
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    user leaves on of these fields blank and my code writes the "" to the docv the variable is deleted

    WebHarvest: can you please share your userform code for variables you are allowing to be blank (""). I'm brand new to vba, and it's driving me up the wall. I've found many ways to accomplish what you described, "When a user leaves one of these fields blank and my code writes the "" to the docvariable the variable is deleted." However, I can't get any to work properly or totally. I have a very simple userform requesting 10-15 pieces of information. It requests the first and last names of two people, then places their names in many places throughout the document. When only one person's is required, I can't delete or get rid of all of the docvariables "Error! docvariable not supplied." from the actual document for the second person.

    the appropriate sample of my code is:

    Private Sub cmdSubmit_Click()
    Set oVars = ActiveDocument.Variables
    Me.Hide
    oVars("varPerson1FirstName").Value = Me.txtPerson1FirstName.Value
    oVars("varPerson1LastName").Value = Me.txtPerson1LastName.Value
    'adds "and" in between the two names in certain places in the document
    oVars("varAndifPerson2").Value = Me.txtAndifPerson2.Value
    oVars("varPerson2FirstName").Value = Me.txtPerson2FirstName.Value
    oVars("varPerson2LastName").Value = Me.txtPerson2LastName.Value
    ActiveDocument.Fields.Update
    Set oVars = Nothing
    Unload Me

    End Sub

    Then, I am using \*CHARFORMAT where the docvariable is placed throughout the document.

    Any help or insight would be greatly appreciated. Wouldn't mind code deleting the docvariable for the 2nd person information, left blank in userform, when there is only one person's name in the userform/document.

    Thanks, in advance.



    Quote Originally Posted by webharvest View Post
    I have a userform and there are several variables that I allow to be blank (""). When a user leaves one of these fields blank and my code writes the "" to the docvariable the variable is deleted. I think this is a "feature" of Word 2007.

    Is there a way to prevent this because I allow the user to reopen the userform and when the form initializes I get an error because the variable no longer exist. I want the user to be able to add a non-blank vlaue later if they change their mind.

    Thank you

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    If you don't want a character (a hyphen, as suggested) to display in your document, try setting an empty form field to two or three spaces in the doc variable. I recall that will not delete the doc variable, and you won't see anything displayed in the document.

  6. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    I would either use content controls and an embedded xml file linked to those CCs or just get the vba to do whatever alterations to the content you require. Since you are using VBA anyway you might as well just have the code write/delete content in the file.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #7
    New Lounger
    Join Date
    Mar 2015
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok. That sounds like the right choice, have the code write/delete content in the file. Struggling with this. Not sure where to put the delete code. I found these, but not sure how to use them with my sample code provided earlier:


    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

    other one i found:

    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

    where would they go in the vba code? any assistance is sincerely, appreciated.

    Thanks, in advance.

  8. #8
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Add a line after
    ActiveDocument.Fields.Update

    The line should just have the name of the macro you want to run eg

    Code:
    ActiveDocument.Fields.Update
    RemoveDocVarFieldsWithNoDocVars
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Tags for this Thread

Posting Permissions

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