【发布时间】:2021-10-12 14:49:06
【问题描述】:
虽然我找到了一种将位于工作表中的文本框的内容保存为图像文件(png、bmp、jpeg)的方法,但我无法为位于用户窗体中的文本框实现相同的功能。 附加代码返回一张空白图片。 请有人指出我正确的方向吗?
Private Sub CommandButton1_Click()
' save textbox content as image file
Dim cht As ChartObject
Dim ActiveShape As Shape
TextBox1.Text = "12345"
' select the TextBox
TextBox1.SetFocus
' Copy selection
Selection.Copy
'
Application.ScreenUpdating = False
Worksheets("Sheet1").Activate
' paste selection into a picture shape
ActiveSheet.Pictures.Paste(link:=False).Select
Set ActiveShape = ActiveSheet.Shapes(ActiveWindow.Selection.Name)
' Create temporary chart object (same size as shape)
Set cht = ActiveSheet.ChartObjects.Add(Left:=ActiveCell.Left, _
Width:=ActiveShape.Width, Top:=ActiveCell.Top, Height:=ActiveShape.Height)
' Format temporary chart to have a transparent background
cht.ShapeRange.Fill.Visible = msoFalse
cht.ShapeRange.Line.Visible = msoFalse
' Copy/Paste Shape inside temporary chart
ActiveShape.Copy
cht.Activate
ActiveChart.Paste
'Save chart to User's Desktop as image file
cht.Chart.Export Environ("USERPROFILE") & "\Desktop\" & "TextBoxImage" & ".bmp"
'Delete temporary Chart
cht.Delete
ActiveShape.Delete
Application.ScreenUpdating = True
End Sub
【问题讨论】:
标签: excel vba textbox userform save-as