有没有办法从VBA中的值中提取枚举字符串? [重复]

问题描述 投票:0回答:2

我希望 MsgBox 向我显示 msoPictureAutomatic 而不是 1

因为我想得到一个名称而不是

请注意,1 表示 msoPictureAutomatic

Sub Macro1()

Sheets(1).Pictures.Insert("C:\Users\George\Downloads\MyPicture.png").Name = "Picture1"

'This MsgBox shows me 1. 
'I want this MsgBox shows me msoPictureAutomatic.
MsgBox Sheets(1).Shapes("Picture1").PictureFormat.ColorType

End Sub

请注意,我给出的 msoPictureAutomatic 只是一个例子。我正在寻找一个共同的解决方案。我对任何其他价值观感兴趣。

excel vba ms-office msgbox objectbrowser
2个回答
0
投票

您在 MsgBox 中看到值“1”而不是“msoPictureAutomatic”的原因是因为 MsgBox 函数默认显示常量的数值。

以下是如何修改代码以显示“msoPictureAutomatic”:

子宏1()

Sheets(1).Pictures.Insert("C:\Users\George\Downloads\MyPicture.png").Name = "Picture1"

' 此 MsgBox 显示 msoPictureAutomatic 而不是 1 MsgBox Sheets(1).Shapes("Picture1").PictureFormat.ColorType & vbCrLf & "msoPictureAutomatic"

结束子

在修改后的代码中,我们在 MsgBox 语句中添加了 & vbCrLf & "msoPictureAutomatic"。这会将 Sheets(1).Shapes("Picture1").PictureFormat.ColorType(即 1)的结果与回车符 (vbCrLf) 和字符串“msoPictureAutomatic”连接起来。结果是一个 MsgBox,在单独的行上显示“1”,后跟“msoPictureAutomatic”。


0
投票

您正在查看的属性(以及大多数其他类似属性)包含数值。

MsoColorType.msoPictureAutomatic
和其他枚举用于在设计时提供易于阅读的代码等。

这些值没有向后枚举。

您需要确定想要文本版本的所有值,并创建一个函数将它们映射到该值。

示例:

Function LookupMsoPictureColorType(Value As Variant)
Dim Result As String: Result = "Unknown Value"
Select Case Value
Case MsoPictureColorType.msoPictureAutomatic
    Result = "MsoPictureColorType.msoPictureAutomatic"
Case MsoPictureColorType.msoPictureBlackAndWhite
    Result = "MsoPictureColorType.msoPictureBlackAndWhite"
End Select
LookupMsoPictureColorType= Result
End Function

您需要为每个您想要的类创建类似的东西,因为许多枚举的值为 1(例如

MsoColorType.msoColorTypeRGB

© www.soinside.com 2019 - 2024. All rights reserved.