• Document variable

    Author
    Topic
    #502337

    I’m reviewing some VBA code which I did not write, and I’ve seen code similar to the following:

    Code:
    ActiveDocument.Variables.Add “Office”, “0” Or “123”
    

    I’m not familiar with the Or operator used when setting the value of a variable. Can anyone tell me how the Or operator functions here, and how/why a condition would be evaluated (if indeed that’s what is occurring).

    Thanks.

    Viewing 5 reply threads
    Author
    Replies
    • #1529460

      My guess is that this is a coding error. It doesn’t make any sense to have a condition there.

    • #1529474

      Hi

      I am guessing that someone has missed an apostrophe to comment out the “0” Or “123”.
      Hence coding error.

      I wonder if the code runs because the value component of the variables.add method is optional.

      I tried this quickly

      Code:
      Sub test()
      ActiveDocument.Variables.Add "Office", "0" Or "123"
      End Sub
      
      Sub test2()
      For Each oVar In ActiveDocument.Variables
          Debug.Print oVar.Name & vbTab & oVar.Value
      Next
      End Sub
      

      The printed result read Office 123

      Cheers
      G

    • #1529485

      The two strings would be converted to numbers, then a bitwise OR is performed. Not a lot of point if one of them is 0 though…

      • #1529671

        Thank you. I’m wondering if “123” should have been “128”, which would make sense: “0” OR “128” or “64” OR “128”. (Similarly, “0010” OR “0100”.)

        Is this what you are suggesting?

    • #1529680

      No. A bitwise OR of 0 and any other number just returns that other number. There is no point to it.

      • #1530118

        Understood. I’m asking what occurs if x = 64 OR 128. Can you tell me about that?

    • #1530148

      192 according to the online calculator I used.

      cheers, Paul

    • #1530149

      It’s a bitwise OR, so write the two numbers down in binary and where either (or both) of the digits is 1, the result is 1, otherwise 0. So:
      01000000 = 64
      10000000 = 128
      ———-
      11000000 = 192

    Viewing 5 reply threads
    Reply To: Document variable

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

    Your information: