1

I am trying to do two things in Excel 2013 using VBA:

  1. Get FORMATTED text from a Textbox, and manipulate it (like HTML perhaps). And
  2. Copy FORMATTED text from a Textbox, to another Textbox.

I have tried two things:

'Copies text only. No formatting, but with proper line breaks
Dim txtContent As String
txtContent = Worksheets("TextBox").Shapes("TextBox1").TextFrame.Characters.Text
Worksheets("TextBox").Shapes("TextBox 3").TextFrame.Characters.Text = txtContent

The second approach was similar to above:

'Does not do anything. Produces Run-time error 91
Dim myFrame As TextFrame
myFrame = Worksheets("TextBox").Shapes("TextBox1").TextFrame
Worksheets("TextBox").Shapes("TextBox 3").TextFrame = myFrame

Please help.

1
  • 1
    Results are in the comments in the code. Commented Feb 13, 2014 at 21:24

2 Answers 2

6
Sub Tester()
    CopyText ActiveSheet.Shapes("txtOne").TextFrame, _
             ActiveSheet.Shapes("txtTwo").TextFrame
End Sub



Sub CopyText(tf1 As TextFrame, tf2 As TextFrame)

    Dim n, f As Font

    tf2.Characters.Text = tf1.Characters.Text

    For n = 1 To tf1.Characters.Count
        Set f = tf1.Characters(n, 1).Font
        With tf2.Characters(n, 1).Font
            .Bold = f.Bold
            .Color = f.Color
            .Italic = f.Italic
            'add other properties as needed...
        End With
    Next n
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

You can either set or retrieve the formatting of text characters in a TextBox as follows:

Sub durall()
    ActiveSheet.Shapes("TextBox 1").TextFrame.Characters.Font.ColorIndex = 3
End Sub

1 Comment

This actually led me to find out that there's a bunch of options (or things to copy) from the Font object. Thanks. msdn.microsoft.com/en-us/library/office/ff840959.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.