Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    Jan 2001
    Location
    Upstate NY, New York, USA
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't get code to total variables (Word 97/VBA)

    At the risk of sounding like a total idiot, I ask the following question:

    Why won't this statement give me a total sum of all variables?

    ActiveDocument.Variables.Add Name:="StandardTotal", Value:=Format(MStandard + _
    TStandard + WStandard + ThStandard + FStandard + SStandard + SuStandard, "#0.00")

    Everything is declared as integers....I either get a string text (123.45) or 00.00. Hope this is enough information -- thank you all.

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

    Re: Can't get code to total variables (Word 97/VBA)

    The result of the Format function is always a string. Why do you want to format the sum as #0.00 when "Everything is declared as integers"? An integer by definition is a number without fractional part...

  3. #3
    Star Lounger
    Join Date
    Jan 2001
    Location
    Upstate NY, New York, USA
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't get code to total variables (Word 97/VBA)

    Even when I delete the format reference, and leave it just MStandard + TStandard, etc., it still won't total. I'm trying to add up the hours an employee worked on specific days to get a total for the week.

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

    Re: Can't get code to total variables (Word 97/VBA)

    I copied your code to a module. It works OK the first time; if you try to run it again, it fails, as is to be expected, because the document variable StandardTotal (which is not the same as a VBA variable) already exists. You can get around this with an On Error Resume Next.

    See the attached document.
    Attached Files Attached Files

  5. #5
    Star Lounger
    Join Date
    Jan 2001
    Location
    Upstate NY, New York, USA
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't get code to total variables (Word 97/VBA)

    THANK YOU HANS! Your sample document was helpful but something you said in your message about the DocVariable being different from the VB variable gave me the information I needed to correct my code. It's now a pretty awesome form.. puts in the week ending date, also all the appropriate dates from Monday-Sunday depending on the week ending, etc. I've never had any training in VB so I'm kind of stumbling along the best I can and learn as I go. Your input was extremely helpful. Thank you again.

  6. #6
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't get code to total variables (Word 97/VBA)

    Hans,,

    I prefer not to use an "On Error Resume next" if I can help it- it can lead to problems if it's left on, or it can hide other errors in the same line.

    In the case of document variables, I'd prefer to do it like this:<pre>Dim varST As Variable

    For Each varST In ActiveDocument.Variables
    If varST.Name = "StandardTotal" Then
    varST.Delete
    End If
    Next varST
    </pre>

    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Posting Permissions

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