Search code examples
excelvbatextboxuserformsave-as

Saving textbox content as image file


While I found a way of saving the content of a textbox located in a worksheet as an image file (png, bmp, jpeg), I am not able to achieve the same thing for a textbox located in a userform. The attached code returns a blank picture. Could somebody point me in the right direction, please?

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

Solution

  • I'm afraid that user form text box does not have the necessary CopyPicture property. Even for a sheet ActiveX text box, Copy does not return the object picture...

    So, you can accomplish what you wan, only using a trick: Create such a text box clone on the sheet and use it to export the picture:

    Private Sub CommandButton1_Click()
      Dim ob As OLEObject, sh As Worksheet, tb As msforms.TextBox, ch As ChartObject, pictName As String
    
      Set sh = ActiveSheet
      pictName = ThisWorkbook.path & "\TextBoxImage.jpg"
        Set ob = sh.OLEObjects.Add(ClassType:="Forms.TextBox.1", link:=False, _
            DisplayAsIcon:=False, left:=383.4, top:=29.4, width:=Me.TextBox1.width, height:=Me.TextBox1.height)
        Set tb = ob.Object
        DoEvents
        With tb
            .Text = Me.TextBox1.Text
            .BackColor = Me.TextBox1.BackColor
            .ForeColor = Me.TextBox1.ForeColor
            .Font = Me.TextBox1.Font
            .Font.Size = Me.TextBox1.Font.Size
        End With
        DoEvents
        Set ch = sh.ChartObjects.Add(left:=1, _
           top:=1, width:=tb.width, height:=tb.height)
    
           tb.CopyPicture: ch.Activate: ActiveChart.Paste
           ch.Chart.Export pictName, "JPEG"
          ch.Delete
          ob.Delete
    End Sub
    

    If necessary, some other text box properties can be copied in the same way (Bold, Italics etc.).

    Please, test it and send some feedback.