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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Can’t get code to total variables (Word 97/VBA)

    Author
    Topic
    #374931

    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.

    Viewing 0 reply threads
    Author
    Replies
    • #608137

      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…

      • #608142

        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.

        • #608165

          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.

          • #608228

            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.

          • #608250

            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:

            Dim varST As Variable
            
            For Each varST In ActiveDocument.Variables
                If varST.Name = "StandardTotal" Then
                    varST.Delete
                End If
            Next varST
            
    Viewing 0 reply threads
    Reply To: Can’t get code to total variables (Word 97/VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: