Replace() not working on question mark in image di

2019-05-01 05:02发布

问题:

I'm trying to get the height of a bitmap by using the Shell object in VBA. Here is the relevant portion of code (bmp is a member of a custom class, and .Width is a property defined as an Integer.)

Set objImg = objShell.Namespace(subfs(sf)).ParseName(bmp.Name)
tmpDim = objShell.Namespace(subfs(sf)).GetDetailsOf(objImg, 162)
tmpDim = Replace(tmpDim, "?", "")
tmpDim = Replace(tmpDim, " pixels", "")
bmp.Width = CInt(tmpDim)

I'm getting a Type Mismatch error on the last line because the value of tmpDim is ?754. For reference, the value of tmpDim after the second line is ?754 pixels.

I have a step included to replace the ? with an empty string, but it clearly does not work. This is driving me nuts. Does anyone know why this is happening and how to get around this problem?

回答1:

The question mark you are seeing is not actually a question mark. The Asc() function will return a value of 63, but AscW() will probably return 8206, the unicode left-to-right mark.

I believe VBA stores strings using two-byte wide characters. The Asc function will only return a value of 0-255 and appears to return 63 for anything outside that range.

This is why your Replace(tmpDim, "?", "") doesn't work. You're trying the equivilent of

Replace(tmpDim, Chr(63), "")

You need

Replace(tmpDim, ChrW(8206), "")

(Assuming the ? is the left-to-right mark, which in my testing it is)