Why does this VBA print out random garbage?

不羁的心 提交于 2019-12-07 07:25:23

问题


Why does this VBA function print out random garbage at the end of the string?

Public Function GenGuid() As String
    Dim TypeLib As Object
    Dim Guid As String
    Set TypeLib = CreateObject("Scriptlet.TypeLib")
    Guid = TypeLib.Guid
    ' format is {24DD18D4-C902-497F-A64B-28B2FA741661}
    Guid = Replace(Guid, "{", "")
    Guid = Replace(Guid, "}", "")
    'Guid = Replace(Guid, "-", "")
    GenGuid = Guid
End Function

print genguid()
816BF7CB-68B9-4D6B-855B-1273F22063DB 6
print genguid()
53664789-B0CD-422C-87B4-8928D93E5078  
print genguid()
FDD8ABDE-41F1-45E0-8330-9BA6161873BE i

Reference

http://office.microsoft.com/client/helppreview.aspx?AssetId=HV805572319990&lcid=1033&NS=EXCEL%2EDEV&Version=12&queryid=&respos=1&HelpID=vblr6%2Echm1008930

Version

Excel 2007


回答1:


It's a long-standing GUID bug that has been documented for quite some time. It's a null termination of sorts, and it should be ending with ASC(0) in both places. I tested it out in the Excel immediate window and the second "garbage" digit cycles through different values and only the first null character is consistently null.

Once the function is called from a cell, however, printing it in the immediate window ceases to call a different last digit, and will consistently print out two ASC(0) values instead. Weird.

Trimming the last 2 places, however, does not show any harmful effects, as far as I've tested.



来源:https://stackoverflow.com/questions/19556268/why-does-this-vba-print-out-random-garbage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!