无法将字符串转换为整数或双精度

时间:2016-08-26 16:20:56

标签: excel vba

这是代码:

Sub imagesize()
Dim selection As Range
Dim rng As Range
Dim objShell As Object
Dim objFolder As Object
Dim objFile As Object
Dim dimen As String
Dim xmm As Double
Dim ymm As Integer
Set objShell = CreateObject("Shell.Application")
Set selection = Application.selection

For Each rng In selection.Cells
    Set objFolder = objShell.Namespace("C:\Users\Desktop\")
    Set objFile = objFolder.ParseName("try.tif")
    dimen = objFile.ExtendedProperty("Dimensions")
    xmm = Val(Left(dimen, InStr(dimen, "x") - 2))
    Cells(rng.Row, rng.Column).Value = xmm
Next rng

End Sub

我已经获得了图片的尺寸,但我无法将该字符串转换为整数,也不能将CIntVal转换为整数,但它只包含数字。我在这里错过了什么?谢谢你的回答。

1 个答案:

答案 0 :(得分:1)

替换此行xmm = Val(Left(dimen, InStr(dimen, "x") - 2))

以下代码

If dimen <> "" Then
    dimen = Mid(dimen, 2, Len(dimen) - 2)
    xmm = CDbl(Split(dimen, "x")(0))
    ymm =  CDbl(Split(dimen, "x")(1))
End If

或者,您只需插入具有原始尺寸的图像,然后从那里读取高度和宽度。 https://stackoverflow.com/a/39151519/3961708