title: system.security.cryptography.SHA256Managed in .NET 4.5 (Windows 10)
body: <p>I am using the following VBA function to get the SHA256 value for a file;</p>
  
  <pre><code>Public Function FileToSHA256(sfilename As String) As String
      Dim enc
      Dim bytes
      Dim outstr As String
      Dim pos As Integer
      Set enc = CreateObject("System.Security.Cryptography.SHA256Managed")
      'Convert the string to a byte array and hash it
      bytes = GetFileBytes(sfilename)
      bytes = enc.ComputeHash_2((bytes))
      'Convert the byte array to a hex string
      For pos = 1 To LenB(bytes)
          outstr = outstr &amp; LCase(Right("0" &amp; Hex(AscB(MidB(bytes, pos, 1))), 2))
      Next
      FileToSHA256 = outstr 'Returns a 40 byte/character hex string
      Set enc = Nothing
  End Function
  
  Private Function GetFileBytes(ByVal path As String) As Byte()
      Dim lngFileNum As Long
      Dim bytRtnVal() As Byte
      lngFileNum = FreeFile
      If LenB(Dir(path)) Then ''// Does file exist?
          Open path For Binary Access Read As lngFileNum
          ReDim bytRtnVal(LOF(lngFileNum) - 1&amp;) As Byte
          Get lngFileNum, , bytRtnVal
          Close lngFileNum
      Else
          Err.Raise 53
      End If
      GetFileBytes = bytRtnVal
      Erase bytRtnVal
  End Function
  </code></pre>
  
  <p>This works perfectly under a Windows 7 environment on Word 2013. I do not know the .NET version this runs on (presuming 3.5).</p>
  
  <p>It has now been launched onto a Windows 10 Environment (still Word 2013). I have been told by our IT dept that this is on .NET 4.5, but according to VBA its running from .NET frameworks 4.0.30319.</p>
  
  <p>It now throws error  - </p>
  
  <blockquote>
    <p>Run-Time Error '-2146232576 (80131700)' Automation Error</p>
  </blockquote>
  
  <p>on this line;</p>
  
  <pre><code>Set enc = CreateObject("System.Security.Cryptography.SHA256Managed")
  </code></pre>
  
  <p>I have MSCORLIB.DLL referenced in the project.</p>
  
  <p>I can't figure out if I need to add additional references or change the code.</p>
  
  <p>If a code change is necessary, it would need to cover the earlier version of .NET also, something with a case check to cover different versions, but I am not sure how to do this.</p>