简体   繁体   English

是否可以以编程方式将图像文件添加到 vba 用户窗体中的图像或命令按钮控件? - 解决了

[英]Is it possible to programmatically add an image file to an image or commandbutton control in a vba userform? - SOLVED

I am trying to get a image to load programmatically into image and commandbutton MSForms controls to no avail without the VB editor.如果没有 VB 编辑器,我试图让图像以编程方式加载到图像和命令按钮 MSForms 控件中,但无济于事。 I am using the macro structure below which works well with other controls.我正在使用下面的宏结构,它可以很好地与其他控件配合使用。 Microsoft indicates here that the editor must be used:微软在这里指出必须使用编辑器:

You must use the control's property page to assign a bitmap to the Picture property.您必须使用控件的属性页将 bitmap 分配给 Picture 属性。 You cannot use the Visual Basic LoadPicture function to assign a bitmap to Picture.您不能使用 Visual Basic LoadPicture function 将 bitmap 分配给图片。

The macro below works when the .Picture command is commented out without image of course.当然,当.Picture命令被注释掉而没有图像时,下面的宏就起作用了。 When loading an image into a commandbutton the same problem occurs.将图像加载到命令按钮时,会发生同样的问题。 I have tried various ways to provide the file location but it always errors out.我尝试了多种方法来提供文件位置,但总是出错。 Does someone know a clever work around?有人知道一个聪明的解决方法吗?

Sub NewForm()

Dim TempForm As Object
Dim NewImage As MSForms.Image

Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
With TempForm
 .Properties("Height") = 300
 .Properties("Width") = 300
End With

Set NewImage = TempForm.designer.Controls.Add("Forms.image.1")
With NewImage
 .Picture = "C:\image.jpg"  'Nothing works here it seems
 .Height = 100
 .Left = 100
 .Top = 100
 .Width = 100
End With

End Sub

Below is an example to add Images to Image and Command Button.下面是将图像添加到图像和命令按钮的示例。

Three changes that I made我做的三个改变

  1. I changed Dim NewImage As MSForms.Image to Dim NewImage As Object .我将Dim NewImage As MSForms.Image更改为Dim NewImage As Object Using Dim NewImage As Image will also work.使用Dim NewImage As Image也可以。
  2. I used an image from my pc.我使用了我电脑上的图像。
  3. I used LoadPicture() .我使用LoadPicture()

Is this what you are trying?这是你正在尝试的吗?

Option Explicit

Sub NewForm()
    Dim TempForm As Object
    Dim NewImage As Object

    Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    With TempForm
        .Properties("Height") = 300
        .Properties("Width") = 300
    End With
    
    '~~> Image Control
    Set NewImage = TempForm.designer.Controls.Add("Forms.image.1")
    With NewImage
        .Picture = LoadPicture("C:\Users\routs\Desktop\Start.Bmp")
        .Height = 100
        .Left = 100
        .Top = 10
        .Width = 100
    End With
    
    '~~> Command Button
    Dim ctl_Command As Control
    Set ctl_Command = TempForm.designer.Controls.Add("Forms.CommandButton.1", "CmdXYZ", False)
    
    With ctl_Command
        .Picture = LoadPicture("C:\Users\routs\Desktop\Start.Bmp")
        .Left = 100
        .Top = 140
        .Height = 50
        .Width = 50
        .Visible = True
    End With
End Sub

In Action在行动

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM