How to copy shape text including contained fields using VBA

Started by rezingg, December 06, 2022, 08:54:17 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

rezingg

I need to copy text from one shape to another. The text contains custom fields, pulling data from the Document Shape Data.
When manually using copy & paste everything is fine, the fields transfer just fine.
But in VBA the field formulas do not get transferred (the "Text Fields" section in the ShapeSheet is not transferred).
I currently use:
  shpTwo.Text = shpOne.Text

Is there a way to copy the text and with it also the fields and field formulas from one shape to another, using VBA?

Thanks in advance for any pointers!

rezingg

Answering my own question:
before copying the shape text, check if the ShapeSheet of the source shape has a "Text Fields" section. If it does, copy that section row by row, cell by cell to the destination shape first, then copy the text.
Here is what worked for me:


      If shpFrom.SectionExists(visSectionTextField, False) Then ' if the source shape text contains any fields, then first copy "Text Field" section of ShapeSheet to new shape
            rowCount = shpFrom.rowCount(visSectionTextField)
            For rowIdx = 0 To rowCount - 1 ' iterate through all rows of section
                ' copy content of all cells of current row
                intRow2 = shpTo.AddRow(visSectionTextField, visRowLast, visTagDefault)
                shpTo.CellsSRC(visSectionTextField, intRow2, visFieldCell).FormulaU = shpFrom.CellsSRC(visSectionTextField, rowIdx, visFieldCell).FormulaU
                shpTo.CellsSRC(visSectionTextField, intRow2, visFieldCalendar).FormulaU = shpFrom.CellsSRC(visSectionTextField, rowIdx, visFieldCalendar).FormulaU
                shpTo.CellsSRC(visSectionTextField, intRow2, visFieldFormat).FormulaU = shpFrom.CellsSRC(visSectionTextField, rowIdx, visFieldFormat).FormulaU
                shpTo.CellsSRC(visSectionTextField, intRow2, visFieldObjectKind).FormulaU = shpFrom.CellsSRC(visSectionTextField, rowIdx, visFieldObjectKind).FormulaU
            Next rowIdx
        End If
        shpTo.Text = shpFrom.Text