Home » excel » excel – Replace() question mark in image dimensions string from Shell

excel – Replace() question mark in image dimensions string from Shell

Posted by: admin March 9, 2020 Leave a comment


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 to replace the ? with an empty string, but it does not work. How can I get rid of the question mark character?

How to&Answers:

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)