Results 1 to 9 of 9
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Document variable

    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.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    My guess is that this is a coding error. It doesn't make any sense to have a condition there.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. The Following User Says Thank You to Andrew Lockton For This Useful Post:

    richardbarrett (2015-09-23)

  4. #3
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts
    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

  5. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    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...
    Regards,
    Rory

    Microsoft MVP - Excel

  6. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    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?

  7. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    No. A bitwise OR of 0 and any other number just returns that other number. There is no point to it.
    Regards,
    Rory

    Microsoft MVP - Excel

  8. #7
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Understood. I'm asking what occurs if x = 64 OR 128. Can you tell me about that?

  9. #8
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,177
    Thanks
    47
    Thanked 983 Times in 913 Posts
    192 according to the online calculator I used.

    cheers, Paul

  10. #9
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    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
    Regards,
    Rory

    Microsoft MVP - Excel

  11. The Following User Says Thank You to rory For This Useful Post:

    richardbarrett (2015-09-29)

Posting Permissions

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