Results 1 to 5 of 5
  1. #1
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Document Variable field problem (W2000 SR1a)

    The following code works perfectly most of the time. I pass this routine the name of a document variable and it inserts it into the document at the current insertion point, then marks it for no spell checking.
    <pre>Public Sub InsertVarField(DocVar As String)
    Dim rngRange As Range
    Dim fldField As Field
    Set rngRange = Selection.Range
    rngRange.Collapse Direction:=wdCollapseEnd
    Set fldField = ActiveDocument.Fields.Add(Range:=rngRange, Type:=wdFieldDocVariable, Text:=DocVar)
    With fldField.Result
    .LanguageID = wdNoProofing
    .NoProofing = True
    End With
    End Sub
    </pre>

    Sometimes it fails with "Error 91, Object variable or With block variable not set", when trying to execute the line "With fldField.Result". On these occassions the variable fldField has a value of Nothing when I try to use it. The field has been correctly added, I can see it in the document, but it seems to have failed to set the variable.

    I think it might only happen when the newly inserted field is the last thing in a table cell in the document footer, but it's not consistent enough for me to be sure. When it fails, it will repeatably fail at the same point in the same document, but works if I insert the field elsewhere in the footer!

    As a temporary measure I tried using
    <pre> If Not IsEmpty(fldField) Then
    End If
    </pre>

    But this didn't seem to prevent the error.

    Any ideas or suggestions?

    StuartR

  2. #2
    Star Lounger
    Join Date
    Sep 2001
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Document Variable field problem (W2000 SR1a)

    Hi StuartR

    I assume you already tried to put a DoEvents in between those two lines?

    hedgehog

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

    Re: Document Variable field problem (W2000 SR1a)

    I haven't tried a DoEvents, but I have tried single stepping through the de###### and the variable definitely does not get set when it fails! I can't imagine that DoEvents would help when everything should be happening in the context of the running code - but I'm desparate so I'll give it a try!

    StuartR

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

    Re: Document Variable field problem (W2000 SR1a)

    Stuart,

    You might try

    If fldField.Result Is Nothing Then
    ' exit, or whatever
    End If

    Not the use of Is, not =.

    Also, if the document variable has an empty string as value, fld.Result will be Error! Missing document variable. or something like that. You may want to check for that too.

    Regards,
    Hans

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

    Re: Document Variable field problem (W2000 SR1a)

    Hans,

    Thanks for the advice, I'll give it a try.

    This routine doesn't check that the document variable is valid etc. because the caller has already done so.

    StuartR

Posting Permissions

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