The Strangest VBA Question I Have Ever Seen (VBASigned Possible Boolean Error)

In MS Word, I added code to see if the document was missing its digital signature, or at least I thought I did. I decided to share before testing this on other systems.

Sub test()
    If Not ThisDocument.VBASigned Then
        Debug.Print "I am NOT signed"
    End If
End Sub

Problem: The code above gives the same result regardless of whether the document is digitally signed. If I change the code by deleting Not, I still get unexpected results.

I tried to force things by doing things like:

If Not CBool(ThisDocument.VBASigned) Then

But more surprisingly, the following code also fails:

Sub test()
    Dim isSigned As Boolean
    isSigned = ThisDocument.VBASigned

    If Not isSigned Then
        Debug.Print "I am NOT signed"
    End If
End Sub

Even if ThisDocument.VBASignedANDs isSignedare TRUE...
snapshot of my code running but if changed isSigned = ThisDocument.VBASignedto isSigned = True, then everything will work as expected.

- ? ?


:

  • , Option Explicit, Debug.
    :

    Option Explicit
    
    Sub test()
        Dim isSigned As Boolean
        isSigned = ThisDocument.VBASigned
        Debug.Print ThisDocument.VBASigned
        If Not isSigned Then
            Debug.Print "I am NOT signed"
        End If
    End Sub
    

    :

    True
      

  • : True * 0 - 1.

    Sub test()
        Dim isSigned As Boolean
        isSigned = ThisDocument.VBASigned
        Debug.Print ThisDocument.VBASigned
        If Not (isSigned * 0 - 1) Then
            Debug.Print "I am NOT signed"
        End If
    End Sub
    

    () :

    True

+6
2

, SlowLearner, , Word VBASigned 1, .

If, Not 1 -2, Not 0 -1 - , Not VBASigned (.. -False) .


MSDN , VBASigned , ( TypeName(ThisDocument.VBASigned)) Boolean, , , .


, CBool(ThisDocument.VBASigned) * 1 1, a CBool(1) * 1 -1. , , , VBA , Boolean (, ThisDocument.VBASigned ), . , CBool Boolean, -1.


, :

Sub test()
    Dim myVBASigned As Integer
    Dim isSigned As Boolean
    myVBASigned = ThisDocument.VBASigned 'Store as Integer
    isSigned = myVBASigned               'Convert to a "true" Boolean

    If Not isSigned Then                 'Use the "true" Boolean
        Debug.Print "I am NOT signed"
    End If

End Sub
+6

Excel , Word.

, VBA VBA:

Excel:

Sub testExcel()
    Dim isSigned As Boolean
    isSigned = ThisWorkbook.VBASigned
    Debug.Print ThisWorkbook.VBASigned
    If Not isSigned Then
        Debug.Print "I am NOT signed"
    Else
        Debug.Print "I AM signed"
    End If
End Sub

True

Word

Sub testWord()
    Dim isSigned As Boolean
    isSigned = ThisDocument.VBASigned
    Debug.Print ThisDocument.VBASigned
    If Not isSigned Then
        Debug.Print "I am NOT signed"
    Else
        Debug.Print "I AM signed"
    End If
End Sub

True

, Word .


  • Windows 10 x64
  • Office Professional Plus 2016
    • 1703 Build 7967.2139
    • 64-
+2

Source: https://habr.com/ru/post/1017125/


All Articles