dynamic text with HTML markup

G

Guest

All,

I'm working on an application that generates a Powerpoint presentation
dynamically (using vba or vbscript).

I've figured out how to create text shapes, and then add or modify the text
in them programmatically.

However, what I'd like to know is whether I can use HTML markup for this
dynamic text? The problem is that, in a given text box, some of the text may
be bold, superscripted, italicized, or bulleted, but I won't know this
beforehand.

Ideally, I'd like to be able to do something like this:

oSlide.Shapes("company").TextFrame.Text = "Some text is <b>BOLD</b>"

but that doesn't work.

Many thanks in advance for any help or insights!

Cheers to all!

Matt Stuehler
 
D

David M. Marcovitz

You can't do just what you want. I can think of two choices, neither of
which are as easy as what you want to do.

(1) You can put the tags in like you want and write a little script to
search all the text that starts with <B> and ends with </B> and change it
to bold.
(2) You can program the bolding in, but the code will be somewhat
convoluted if you have to do it a lot.:

oSlide.Shapes("company").TextFrame.TextRange.Text = _
"Some text is BOLD"
oSlide.Shapes("company").TextFrame.TextRange.Words(4).Font.Bold = True

--David

--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
G

Guest

David,

Many thanks for your response!

Can you provide a little more explanation about how (1) might work?
Possibly, a little code snippet?

No worries if that's too much work.

Cheers,
Matt Stuehler
 
D

David M. Marcovitz

Here is an example that does the bolding for you. Just change .bold to
..italic, <b> to <i>, and </b> to </i> to make it work for italics
instead. Is this what you want?

Sub Htmlize()
Dim oSld As Slide
Dim oShp As Shape
Dim oTxtRng As TextRange
Dim openTag As TextRange
Dim closeTag As TextRange
Dim endRange As Long
Dim startRange As Long

For Each oSld In ActivePresentation.Slides
For Each oShp In oSld.Shapes
If oShp.HasTextFrame Then
Set oTxtRng = oShp.TextFrame.TextRange
Set openTag = oTxtRng.Find(findwhat:="<b>", _
MatchCase:=False)
Do While Not (openTag Is Nothing)
Set closeTag = oTxtRng.Find(findwhat:="</b>", _
MatchCase:=False)
If closeTag Is Nothing Then
endRange = oTxtRng.Length
Else
endRange = closeTag.Start - 1
oTxtRng.Characters(closeTag.Start, _
closeTag.Length).Delete
End If
startRange = openTag.Start
oTxtRng.Characters(startRange, _
endRange - startRange + 1) _
.Font.Bold = True
oTxtRng.Characters(openTag.Start, _
openTag.Length).Delete
Set openTag = oTxtRng.Find(findwhat:="<b>", _
MatchCase:=False)
Loop

End If
Next oShp
Next oSld
End Sub


--
David M. Marcovitz
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
S

Steve Rindsberg

Can you provide a little more explanation about how (1) might work?
Possibly, a little code snippet?

Hi ... I also replied in the .Developer NG, but it seems simplest to follow up
here. Beware the evil LineBreak. This converts the selected text which'd look
like this:

Here is some <B>bolded</B> text. <B>This</B> is bolded too.

Since your text is coming from a database, I'll assume you can assure that all
the tags are uppercase. If not, you'll need to do some fixup before running
this (hint: the Replace method should be able to turn all your </b> to </B> in
one shot)

Sub HTMLBoldingToPPTBolding()

Dim oSh As Shape
Dim lBoldStart As Long
Dim lBoldEnd As Long

Set oSh = ActiveWindow.Selection.ShapeRange(1)

While InStr(UCase(oSh.TextFrame.TextRange.Text), "<B>") > 0
' find the starting point of the bolded text
lBoldStart = InStr(oSh.TextFrame.TextRange.Text, "<B>") + 3
' find the ending point of the bolded text
lBoldEnd = InStr(UCase(oSh.TextFrame.TextRange.Text), "</B>")
' bold the text itself
oSh.TextFrame.TextRange.Characters(lBoldStart, lBoldEnd -
lBoldStart).Font.Bold = msoTrue
' Replace the <B> with ""
oSh.TextFrame.TextRange.Characters(lBoldStart - 3, 3).Text = ""
' Replace the </B> with "" (and factor in the previously removed 3
characters)
oSh.TextFrame.TextRange.Characters(lBoldEnd - 3, 4).Text = ""
Wend

' By using variables instead of hardcoded <B> and </B> tags and by using
' e.g. Len(OpeningTag) instead of hardcoded lengths, you could make this
more universal

End Sub
 
G

Guest

David,

Holy cow... I was hoping for a few pointers, instead, you've written an
elegant function that solves the entire problem perfectly.

Many, many thanks for your help and expertise.

Kind regards,
Matt Stuehler
 
G

Guest

Steve,

Thank you for your all of your replies (both here, and in the Automation
discussion).

The elegant function you're written works perfectly. Based on your example,
I'm in the process of extending it to cover all of the HTML tags I need to
support.

If anyone thinks this might be useful to them, let me know, and I'd be glad
to send you the results.

Many thanks again!

Cheers,
Matt Stuehler
 
S

Steve Rindsberg

Thank you for your all of your replies (both here, and in the Automation
discussion).

Glad to help out. It looked like something I was sure to need sooner or later
anyhow; this way I provide the kickstart and get somebody else to do the hard work.
<g>

And you bet, I'd love to see it when it's done.
 
G

Guest

This topic was helpful, but I encountered another problem regarding the
formatting of text in the textframe. Formatting it to BOLD, ITALICS was
already addessed. But I do need help to format those fonts in outline format
(bulleted) for HTML tags <UL> and <LI>. Is there a way to do this as well?
 
S

Steve Rindsberg

Fjgs27 said:
This topic was helpful, but I encountered another problem regarding the
formatting of text in the textframe. Formatting it to BOLD, ITALICS was
already addessed. But I do need help to format those fonts in outline format
(bulleted) for HTML tags <UL> and <LI>. Is there a way to do this as
well?

I'm sure there is. You'll probably have to walk the text a character at a
time looking for <ol>, <li> and such and maintain a state variable that
tells you how far the text is to be indented (since you can have nested
 

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