PowerPoint API and HTML encoding

B

Brice

Is it possible to insert HTML formatted text directly into a TextRange for a
Shape object without using Paste Special or any other Clipboard functionality?

We would then like to see the PowerPoint shape take on this HTML formatting
and display accordingly on the slide to the end user.

For example, if we insert the following String in a shape using the
following code:

Set myDocument = ActivePresentation.Slides(1)
myDocument.Shapes.AddShape(msoShapeRectangle, 0, 0, 250, 140) _
.TextFrame.TextRange.Text = "Here is some <b>test</b>text
<ul><li>list item1</li><li>list item2</li></ul>"

We would like to keep the HTML formatting (and not display the HTML tags as
text). The only workaround that we have so far is to copy the HTML in the
clipboard, and do a Paste as HTML. But this is innapropriate for reliable
automation.

Is there an API to use that keeps HTML formatting?

Thank you.
 
S

Steve Rindsberg

Is it possible to insert HTML formatted text directly into a TextRange for a
Shape object without using Paste Special or any other Clipboard functionality?

We would then like to see the PowerPoint shape take on this HTML formatting
and display accordingly on the slide to the end user.

For example, if we insert the following String in a shape using the
following code:

Set myDocument = ActivePresentation.Slides(1)
myDocument.Shapes.AddShape(msoShapeRectangle, 0, 0, 250, 140) _
.TextFrame.TextRange.Text = "Here is some <b>test</b>text
<ul><li>list item1</li><li>list item2</li></ul>"

We would like to keep the HTML formatting (and not display the HTML tags as
text). The only workaround that we have so far is to copy the HTML in the
clipboard, and do a Paste as HTML. But this is innapropriate for reliable
automation.

Is there an API to use that keeps HTML formatting?

No, afraid not.

You'll have to more or less interpret the HTML yourself and convert it to PPT-
ese.

Something like this will sort out the <b></b> stuff:

Dim oSh As Shape
Dim oRng As TextRange
Dim lStartChar As Long
Dim lEndChar As Long

' set a reference to the shape you're working with:
Set oSh = ActiveWindow.Selection.ShapeRange(1)

With oSh.TextFrame.TextRange
lStartChar = InStr(.Text, "<b>") + Len("<b>")
lEndChar = InStr(.Text, "</b>")
.Characters(lStartChar, lEndChar - lStartChar).Font.Bold = True
.Characters(lStartChar - Len("<b>"), Len("<b>")).Text = ""
.Characters(lEndChar - Len("<b>"), Len("</b>")).Text = ""
End With

You'd actually need to run that inside a loop that repeats for each instance of
<b> in the string.

And more generically, you'd create an array of the open/close tags and repeat an
outer loop for each element in the array. Pull the open/close tags from the
array rather than hardcoding them as I've done in the example.
 
B

Brice

Thank you Steve for taking the time to reply.

It's very unfortunate that an API call is not available as your suggestion
would work pretty well for simple tags, but could be getting complex quickly
(and open-ended) for things like bullets, etc.
 
S

Steve Rindsberg

It's very unfortunate that an API call is not available as your suggestion
would work pretty well for simple tags, but could be getting complex quickly
(and open-ended) for things like bullets, etc.

Don't I know it. I've at least partially implemented something similar, but in
reverse (PPT formatting to HTML) in one of my add-ins. <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top