使用VBA在Excel 2013中复制/处理文本框中的格式化文本

Ros*_*oss 1 excel vba textbox excel-vba excel-2013

我正在尝试使用VBA在Excel 2013中做两件事:

  1. 从文本框中获取FORMATTED文本,然后对其进行操作(也许像HTML)。和
  2. 将格式化的文本从一个文本框中复制到另一个文本框中。

我尝试了两件事:

'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
Run Code Online (Sandbox Code Playgroud)

第二种方法与上面类似:

'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
Run Code Online (Sandbox Code Playgroud)

请帮忙。

Tim*_*ams 5

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
Run Code Online (Sandbox Code Playgroud)