0

I'm trying (and failing) to fill out a text box (TextFrame) in Publisher using a macro from content in a Word table. I'm trying to do something along the lines of:

With doc.Pages(1).shapes(1)
    .GroupItems.Item(8).TextFrame.TextRange = table.Cell(2, 3).Range.FormattedText
End With

The source text from table has a bunch of font formatting that I need in the text box but it won't seem to be able to copy over the formatting and I just get the plain text. Any ideas on how to get this working properly?

Edit: It seems like TextFrame can't accept formatted text at all. Is there any way around this?

TimO
  • 131
  • 1
  • 8

1 Answers1

0

In Word TextFrame.TextRange returns a range which has a FormattedText property. The usage for that would be:

.GroupItems.Item(8).TextFrame.TextRange.FormattedText = table.Cell(2, 3).Range.FormattedText

In your code you haven't specified which property of the TextRange you want to assign the formatted text to. This means it will be assigned to the default property, Text, which is just a string and cannot contain any formatting.

Golden Rule: never rely on default properties, always specify the property you want to use.

Given that you appear to be taking a value from Word into Publisher you should be looking at the documentation for VBA in Publisher which shows you that the TextRange object in Publisher does not have a FormattedText property, so you cannot take formatting across using this method.

Timothy Rylatt
  • 7,221
  • 2
  • 10
  • 14
  • Ahh, that explains some things. The answer you gave won't work due to your reasoning at the end. Due to ```doc``` being a Publisher doc, how can I get formatted text from word to publisher? This may have been better as a question as the answer you gave would work solely in Word, not Publisher as well. As such, I can't mark this answer as correct – TimO Jul 21 '22 at 21:51
  • @TimO - the first part of my answer was to show you that even in Word your code wouldn’t work. The second part answers your question directly. – Timothy Rylatt Jul 22 '22 at 07:10
  • So, just to be clear, there is no way at all to get formatted text from word into a publisher text box? – TimO Jul 22 '22 at 20:58
  • @TimO - have you tried copy and paste? If you’re copy/paste is part if a loop you’ll need to be aware that the clipboard is an OS function and will respond more slowly than your code executes. Eventually your code will error as it attempts to paste from an empty clipboard. Using `Do Events` in your code can help, up to a point. – Timothy Rylatt Jul 23 '22 at 08:10