How can VBA replicate formatted text from one textbox to another?

B

Bruce Cooley

Using PowerPoint 2002 with SP3, I have two presentations with identical
slide layouts bot different text in the textboxes. I want to copy formatted
text from a textbox on my source presentation to the same textbox on my
range presentation. I can do it manually with no problem.

I tried various methods but I always got an extra blank line in the textbox.
I finally found a way to avoid the extra line, but my current code adds
ephemeral dotted-line rectangles around most, but not all, of the textranges
I paste into. It doesn't matter whether the new text is larger or smaller
than the textbox they are being pasted into. They are not your usual
selection dotted lines. They disappear when I grab any anchor and adjust
the size. Not only that, they remain when I undo the operation and the text
reverts to what it was before. Does anyone have any idea what these are,
why they appear, and how I can make them not appear?

Looping through slides in my target presentation....
Looping through shapes....
With .Shapes(c)

If .Name = "KnowByDem1" Then
.TextFrame.TextRange.Characters.Copy
With TC.Slides(8).Shapes("KnowByDem1").TextFrame ' TC is
the source presentation
.DeleteText
.TextRange.InsertAfter.Paste
End With
End If

If I use InsertBefore, or if I copy the textrange but not the characters, I
get the extra blank line at the bottom of the target textbox. All I really
want to do is create an exact replica of what was in the other box. Why am
I having such a hard time doing it in VBA? What am I missing? The macro
recorder is not helping at all.

Thanks in advance,
Bruce
 
S

Steve Rindsberg

Using PowerPoint 2002 with SP3, I have two presentations with identical
slide layouts bot different text in the textboxes. I want to copy formatted
text from a textbox on my source presentation to the same textbox on my
range presentation. I can do it manually with no problem.

I tried various methods but I always got an extra blank line in the textbox.
I finally found a way to avoid the extra line, but my current code adds
ephemeral dotted-line rectangles around most, but not all, of the textranges
I paste into. It doesn't matter whether the new text is larger or smaller
than the textbox they are being pasted into. They are not your usual
selection dotted lines. They disappear when I grab any anchor and adjust
the size. Not only that, they remain when I undo the operation and the text
reverts to what it was before. Does anyone have any idea what these are,
why they appear, and how I can make them not appear?

Looping through slides in my target presentation....
Looping through shapes....
With .Shapes(c)

If .Name = "KnowByDem1" Then
.TextFrame.TextRange.Characters.Copy
With TC.Slides(8).Shapes("KnowByDem1").TextFrame ' TC is
the source presentation
.DeleteText
.TextRange.InsertAfter.Paste
End With
End If

If I use InsertBefore, or if I copy the textrange but not the characters, I
get the extra blank line at the bottom of the target textbox. All I really
want to do is create an exact replica of what was in the other box. Why am
I having such a hard time doing it in VBA? What am I missing? The macro
recorder is not helping at all.

Silly question, maybe, but why not copy the text box from one presentation to
the other? Is there formatting on the "destination" presentation that you need
to preserve?
 
B

Bruce Cooley

: In article <[email protected]>, Bruce Cooley
wrote:
: > Using PowerPoint 2002 with SP3, I have two presentations with identical
: > slide layouts bot different text in the textboxes. I want to copy
formatted
: > text from a textbox on my source presentation to the same textbox on my
: > range presentation. I can do it manually with no problem.
: >
: > I tried various methods but I always got an extra blank line in the
textbox.
: > I finally found a way to avoid the extra line, but my current code adds
: > ephemeral dotted-line rectangles around most, but not all, of the
textranges
: > I paste into. It doesn't matter whether the new text is larger or
smaller
: > than the textbox they are being pasted into. They are not your usual
: > selection dotted lines. They disappear when I grab any anchor and
adjust
: > the size. Not only that, they remain when I undo the operation and the
text
: > reverts to what it was before. Does anyone have any idea what these
are,
: > why they appear, and how I can make them not appear?
: >
: > Looping through slides in my target presentation....
: > Looping through shapes....
: > With .Shapes(c)
: >
: > If .Name = "KnowByDem1" Then
: > .TextFrame.TextRange.Characters.Copy
: > With TC.Slides(8).Shapes("KnowByDem1").TextFrame ' TC
is
: > the source presentation
: > .DeleteText
: > .TextRange.InsertAfter.Paste
: > End With
: > End If
: >
: > If I use InsertBefore, or if I copy the textrange but not the
characters, I
: > get the extra blank line at the bottom of the target textbox. All I
really
: > want to do is create an exact replica of what was in the other box. Why
am
: > I having such a hard time doing it in VBA? What am I missing? The
macro
: > recorder is not helping at all.


: Silly question, maybe, but why not copy the text box from one presentation
to
: the other? Is there formatting on the "destination" presentation that you
need
: to preserve?
: -----------------------------------------
: Steve Rindsberg, PPT MVP
: PPT FAQ: www.pptfaq.com
: PPTools: www.pptools.com
: ================================================


In the past there was some reason I copied all the text and not the textbox
manually; maybe it had to do with placeholder settings in the target
presentation or something. I will abandon my past reasoning and go forward
in the name of progress:

If .Name = "KnowByDem1" Then
TC.Slides(8).Shapes("KnowByDem1").TextFrame.TextRange = .TextFrame.TextRange

Much shorter code, but again I get the strange dotted-line rectangle around
the textbox, which I mentioned in my OP. The only way to make it go away
them is to adjust the size of the box. I'm back to square 1. In the
meantime I have learned how very little I understand about how to move
between two open presentations, and how precious little information is out
there on the web about that. Any further ideas?

Bruce
 
B

Bruce Cooley

: In article <[email protected]>, Bruce Cooley
wrote:
: > Using PowerPoint 2002 with SP3, I have two presentations with identical
: > slide layouts bot different text in the textboxes. I want to copy
formatted
: > text from a textbox on my source presentation to the same textbox on my
: > range presentation. I can do it manually with no problem.
: >
: > I tried various methods but I always got an extra blank line in the
textbox.
: > I finally found a way to avoid the extra line, but my current code adds
: > ephemeral dotted-line rectangles around most, but not all, of the
textranges
: > I paste into. It doesn't matter whether the new text is larger or
smaller
: > than the textbox they are being pasted into. They are not your usual
: > selection dotted lines. They disappear when I grab any anchor and
adjust
: > the size. Not only that, they remain when I undo the operation and the
text
: > reverts to what it was before. Does anyone have any idea what these
are,
: > why they appear, and how I can make them not appear?
: >
: > Looping through slides in my target presentation....
: > Looping through shapes....
: > With .Shapes(c)
: >
: > If .Name = "KnowByDem1" Then
: > .TextFrame.TextRange.Characters.Copy
: > With TC.Slides(8).Shapes("KnowByDem1").TextFrame ' TC
is
: > the source presentation
: > .DeleteText
: > .TextRange.InsertAfter.Paste
: > End With
: > End If
: >
: > If I use InsertBefore, or if I copy the textrange but not the
characters, I
: > get the extra blank line at the bottom of the target textbox. All I
really
: > want to do is create an exact replica of what was in the other box. Why
am
: > I having such a hard time doing it in VBA? What am I missing? The
macro
: > recorder is not helping at all.
:
: Silly question, maybe, but why not copy the text box from one presentation
to
: the other? Is there formatting on the "destination" presentation that you
need
: to preserve?
:
: -----------------------------------------
: Steve Rindsberg, PPT MVP
: PPT FAQ: www.pptfaq.com
: PPTools: www.pptools.com
: ================================================
:

OK, I think I know why I am having the dotted line problem. When I manually
select all the text in the target textbox, delete it, and then deselect the
box, I get the placeholder which says "Click to add text". When I manually
paste text there I have to click the placeholder first, which somehow
changes it, maybe by overlaying a textframe or something. When I use VBA to
paste the text from the clipboard, I am not selecting the placeholder first,
so I get the placeholder's dotted lines PLUS the text I pasted. I think VBA
is not merging the new text into the placeholder. How would I do the VBA
equivalent of "Click to add text"? Meaning how would I select that
placeholder in VBA?

Bruce
 
S

Steve Rindsberg

: In article <[email protected]>, Bruce Cooley
wrote:
: > Using PowerPoint 2002 with SP3, I have two presentations with identical
: > slide layouts bot different text in the textboxes. I want to copy
formatted
: > text from a textbox on my source presentation to the same textbox on my
: > range presentation. I can do it manually with no problem.
: >
: > I tried various methods but I always got an extra blank line in the
textbox.
: > I finally found a way to avoid the extra line, but my current code adds
: > ephemeral dotted-line rectangles around most, but not all, of the
textranges
: > I paste into. It doesn't matter whether the new text is larger or
smaller
: > than the textbox they are being pasted into. They are not your usual
: > selection dotted lines. They disappear when I grab any anchor and
adjust
: > the size. Not only that, they remain when I undo the operation and the
text
: > reverts to what it was before. Does anyone have any idea what these
are,
: > why they appear, and how I can make them not appear?
: >
: > Looping through slides in my target presentation....
: > Looping through shapes....
: > With .Shapes(c)
: >
: > If .Name = "KnowByDem1" Then
: > .TextFrame.TextRange.Characters.Copy
: > With TC.Slides(8).Shapes("KnowByDem1").TextFrame ' TC
is
: > the source presentation
: > .DeleteText
: > .TextRange.InsertAfter.Paste
: > End With
: > End If
: >
: > If I use InsertBefore, or if I copy the textrange but not the
characters, I
: > get the extra blank line at the bottom of the target textbox. All I
really
: > want to do is create an exact replica of what was in the other box. Why
am
: > I having such a hard time doing it in VBA? What am I missing? The
macro
: > recorder is not helping at all.

: Silly question, maybe, but why not copy the text box from one presentation
to
: the other? Is there formatting on the "destination" presentation that you
need
: to preserve?
: -----------------------------------------
: Steve Rindsberg, PPT MVP
: PPT FAQ: www.pptfaq.com
: PPTools: www.pptools.com
: ================================================

In the past there was some reason I copied all the text and not the textbox
manually; maybe it had to do with placeholder settings in the target
presentation or something. I will abandon my past reasoning and go forward
in the name of progress:

If .Name = "KnowByDem1" Then
TC.Slides(8).Shapes("KnowByDem1").TextFrame.TextRange = .TextFrame.TextRange

Much shorter code, but again I get the strange dotted-line rectangle around
the textbox, which I mentioned in my OP. The only way to make it go away
them is to adjust the size of the box. I'm back to square 1. In the
meantime I have learned how very little I understand about how to move
between two open presentations, and how precious little information is out
there on the web about that. Any further ideas?

Can you post a one or two-slide example presentation? Better yet, one with an
example bit of code that adds a new slide and does your copy to create one of
these spectrally rectangulated ghosties?
 
B

Bruce Cooley

: :: In article <[email protected]>, Bruce Cooley
: wrote:
:: > Using PowerPoint 2002 with SP3, I have two presentations with identical
:: > slide layouts bot different text in the textboxes. I want to copy
: formatted
:: > text from a textbox on my source presentation to the same textbox on my
:: > range presentation. I can do it manually with no problem.
:: >
:: > I tried various methods but I always got an extra blank line in the
: textbox.
:: > I finally found a way to avoid the extra line, but my current code adds
:: > ephemeral dotted-line rectangles around most, but not all, of the
: textranges
:: > I paste into. It doesn't matter whether the new text is larger or
: smaller
:: > than the textbox they are being pasted into. They are not your usual
:: > selection dotted lines. They disappear when I grab any anchor and
: adjust
:: > the size. Not only that, they remain when I undo the operation and the
: text
:: > reverts to what it was before. Does anyone have any idea what these
: are,
:: > why they appear, and how I can make them not appear?
:: >
:: > Looping through slides in my target presentation....
:: > Looping through shapes....
:: > With .Shapes(c)
:: >
:: > If .Name = "KnowByDem1" Then
:: > .TextFrame.TextRange.Characters.Copy
:: > With TC.Slides(8).Shapes("KnowByDem1").TextFrame ' TC
: is
:: > the source presentation
:: > .DeleteText
:: > .TextRange.InsertAfter.Paste
:: > End With
:: > End If
:: >
:: > If I use InsertBefore, or if I copy the textrange but not the
: characters, I
:: > get the extra blank line at the bottom of the target textbox. All I
: really
:: > want to do is create an exact replica of what was in the other box.
Why
: am
:: > I having such a hard time doing it in VBA? What am I missing? The
: macro
:: > recorder is not helping at all.
::
:: Silly question, maybe, but why not copy the text box from one
presentation
: to
:: the other? Is there formatting on the "destination" presentation that
you
: need
:: to preserve?
::
:: -----------------------------------------
:: Steve Rindsberg, PPT MVP
:: PPT FAQ: www.pptfaq.com
:: PPTools: www.pptools.com
:: ================================================
::
:
: OK, I think I know why I am having the dotted line problem. When I
manually
: select all the text in the target textbox, delete it, and then deselect
the
: box, I get the placeholder which says "Click to add text". When I
manually
: paste text there I have to click the placeholder first, which somehow
: changes it, maybe by overlaying a textframe or something. When I use VBA
to
: paste the text from the clipboard, I am not selecting the placeholder
first,
: so I get the placeholder's dotted lines PLUS the text I pasted. I think
VBA
: is not merging the new text into the placeholder. How would I do the VBA
: equivalent of "Click to add text"? Meaning how would I select that
: placeholder in VBA?
:
: Bruce


Ha! It took me a day and a half, but this seems to do the trick:

If .Name = "ActionKnowStrat" Then

' Lose the last "character" that throws an extra blank line at the
bottom
CopyTextLength = .TextFrame.TextRange.Characters.Count - 1
.TextFrame.TextRange.Characters(Start:=1,
Length:=CopyTextLength).Copy

' Make contact with the PlaceHolder
With TC.Slides(11).Shapes.Placeholders(2).TextFrame

' Replace its contents
.TextRange = BR.Slides(b).Shapes(c).TextFrame.TextRange

' Delete its contents
.TextRange.Delete

' Paste new contents
.TextRange.Paste
End With
End If


Thank goodness for the coordinates exporter at
http://www.rdpslides.com/pptfaq/FAQ00482.htm which always allows me to
identify the shapes on each slide.

I will try to post something about those disembodied placeholders borders
soon, just so you can see what I was talking about. Maybe there will be a
more efficient way to do this. At least this works and I can move on. To
think I estimated 2 hours for this code that was simply going to copy
formatted text from one presentation to another....

Bruce
 
B

Bruce Cooley

Bruce Cooley wrote:
: > Much shorter code, but again I get the strange dotted-line rectangle
around
: > the textbox, which I mentioned in my OP. <snip>


: Can you post a one or two-slide example presentation? Better yet, one
with an
: example bit of code that adds a new slide and does your copy to create one
of
: these spectrally rectangulated ghosties?


Steve,

Thanks for responding to my questions and giving me the imventive to keep
poking around. I finally figured out it's a timing issue. The
TextRange.Delete method works fast enough on a Placeholder to accept a
subsequent paste operation, but the DeleteText method seems to be too slow
and the Placeholder isn't ready yet before the text gets pasted. If you put
this sample code into a module in a New Blank presentation and run
SetupPres(), you should be able to see the problem and solutions in action.

Bruce


Sub SetupPres()
' Open a New Blank presentation
' Insert a module and paste this code in it
' Run the SetupPres macro

Application.CommandBars("Task Pane").Visible = False
ActivePresentation.Slides(1).Shapes.Placeholders(2).TextFrame.TextRange.Text
= _
"These text characters live in a textrange inside a textframe embedded
in a Subtitle placeholder. This is the target TextRange."
With ActivePresentation
.Slides(1).Shapes.Placeholders(1).Delete

With .Slides(1).Shapes.AddTextbox(msoTextOrientationHorizontal, 100,
100, 300, 40)
.Name = "FormTex"
.TextFrame.AutoSize = ppAutoSizeShapeToFitText ' Why doesn't this
work?
.TextFrame.TextRange = "This formatted text is in a basic TextBox
containing the source TextRange."
With .TextFrame.TextRange.Characters(Start:=6, Length:=10).Font
.Name = "Times New Roman"
.Bold = msoTrue
.Color = RGB(255, 0, 0)
End With
With .TextFrame.TextRange.Characters(Start:=58, Length:=7).Font
.Bold = msoTrue
.Color = RGB(255, 0, 0)
End With
End With
End With
SpeedLimit
End Sub

Sub SpeedLimit()
Dim WAIT As Double

MsgBox "Watch the lower textbox. If I delete the text from the Placeholder"
With ActivePresentation.Slides(1)
.Shapes.Placeholders(1).TextFrame.DeleteText

MsgBox "and then set the Placeholder TextRange equal to the source
TextRange, the formatting will be lost."
.Shapes.Placeholders(1).TextFrame.TextRange =
..Shapes("FormTex").TextFrame.TextRange
End With

MsgBox "If I delete the Placeholder text using .DeleteText"
With ActivePresentation.Slides(1)
.Shapes("FormTex").TextFrame.TextRange.Characters.Copy
With .Shapes.Placeholders(1).TextFrame
.DeleteText
MsgBox "pause for a MsgBox, and then paste the source TextRange
using InsertAfter..."
.TextRange.InsertAfter.Paste
End With
End With
MsgBox "then it works fine. However, if I delete the Placeholder text using
..DeleteText and immediately paste the source TextRange using InsertAfter..."

With ActivePresentation.Slides(1)
.Shapes("FormTex").TextFrame.TextRange.Characters.Copy
With .Shapes.Placeholders(1).TextFrame
.DeleteText
.TextRange.InsertAfter.Paste
End With
End With
MsgBox "...then the placeholder border will appear around the text."

With ActivePresentation.Slides(1)
With .Shapes.AddTextbox(msoTextOrientationHorizontal, 400, 100, 300, 40)
.Name = "FormTex2"
.TextFrame.TextRange = "This formatted text is in a basic TextBox
containing the target TextRange."
With .TextFrame.TextRange.Characters(Start:=6, Length:=10).Font
.Name = "Times New Roman"
.Bold = msoTrue
.Color = RGB(0, 0, 255)
End With
With .TextFrame.TextRange.Characters(Start:=58, Length:=7).Font
.Bold = msoTrue
.Color = RGB(0, 0, 255)
End With
End With
.Shapes("FormTex").TextFrame.TextRange.Characters.Copy

MsgBox "With a normal textbox this is not a problem. The placeholder border
never appears around the text, whether you use .TextRange.Delete"
With .Shapes("FormTex2").TextFrame
.TextRange.Delete
.TextRange.InsertAfter.Paste
End With
End With
MsgBox "or .DeleteText"
With ActivePresentation.Slides(1)
.Shapes("FormTex").TextFrame.TextRange.Characters.Copy
With .Shapes("FormTex2").TextFrame
.DeleteText
.TextRange.InsertAfter.Paste
End With
End With

MsgBox "If I delete the Placeholder text using .TextRange.Delete and
immediately paste the source TextRange using InsertAfter..."
With ActivePresentation.Slides(1)
.Shapes("FormTex").TextFrame.TextRange.Characters.Copy
With .Shapes.Placeholders(1).TextFrame
.TextRange.Delete
.TextRange.InsertAfter.Paste
End With
End With
MsgBox "then it works fine."

MsgBox "Let's put the border back."
With ActivePresentation.Slides(1)
.Shapes("FormTex").TextFrame.TextRange.Characters.Copy
With .Shapes.Placeholders(1).TextFrame
.DeleteText
.TextRange.InsertAfter.Paste
End With
End With

MsgBox "Now, if I put a slight delay after the .DeleteText, "
With ActivePresentation.Slides(1)
.Shapes("FormTex").TextFrame.TextRange.Characters.Copy
With .Shapes.Placeholders(1).TextFrame
.DeleteText
WAIT = Timer
While Timer < WAIT + 1
DoEvents 'do nothing
Wend
.TextRange.InsertAfter.Paste
End With
End With
MsgBox "...then the Placeholder has a chance to get ready to receive a
paste."

MsgBox "I thought using .TextRange.Paste solved the problem..."
With ActivePresentation.Slides(1)
With .Shapes("FormTex")
.TextFrame.TextRange.Characters.Copy
End With
With .Shapes.Placeholders(1).TextFrame
.DeleteText
.TextRange.Paste
End With
End With

MsgBox "but it also has to either wait for the Placeholder to get ready for
pasting."
With ActivePresentation.Slides(1)
With .Shapes("FormTex")
.TextFrame.TextRange.Characters.Copy
End With
With .Shapes.Placeholders(1).TextFrame
.DeleteText
WAIT = Timer
While Timer < WAIT + 1
DoEvents 'do nothing
Wend
.TextRange.Paste
End With
End With

MsgBox "or..."
With ActivePresentation.Slides(1)
With .Shapes("FormTex")
.TextFrame.TextRange.Characters.Copy
End With
With .Shapes.Placeholders(1).TextFrame
.DeleteText
.TextRange.Paste
End With
End With

MsgBox "have the faster .TextRange.Delete method"
With ActivePresentation.Slides(1)
With .Shapes("FormTex")
.TextFrame.TextRange.Characters.Copy
End With
With .Shapes.Placeholders(1).TextFrame
.TextRange.Delete
.TextRange.Paste
End With
End With

' Choose UNDO to start over
End Sub
 
B

Bruce Cooley

:: Can you post a one or two-slide example presentation? Better yet, one
: with an
:: example bit of code that adds a new slide and does your copy to create
one
: of
:: these spectrally rectangulated ghosties?


: Steve,
:
: Thanks for responding to my questions and giving me the imventive to keep
: poking around. I finally figured out it's a timing issue. The
: TextRange.Delete method works fast enough on a Placeholder to accept a
: subsequent paste operation, but the DeleteText method seems to be too slow
: and the Placeholder isn't ready yet before the text gets pasted. If you
put
: this sample code into a module in a New Blank presentation and run
: SetupPres(), you should be able to see the problem and solutions in
action.
:
: Bruce
:
: Sub SetupPres()
<snip>



I found two more differences between these two methods of deleting text from
a TextFrame in a placeholder. One of these is subtle, and one is a big
pain.

1. Even with a delay after the TextFrame.DeleteText method, which allows
that allows a subsequent paste method to work without showing the
Placeholder dotted-line border, UNDO reveals that the border is still there
even when the text reverts to what it was before. I don't know what that's
all about, but it's not clean. I now prefer the TextFrame.TextRange.Delete
approach.

2. However, the latter method seems to leave an extra blank line, as I
mentioned in my OP. To avoid that problem I ended up using a string that is
1 character short of the total number of characters in the TextRange.

CopyTextLength = .TextFrame.TextRange.Characters.Count - 1
.TextFrame.TextRange.Characters(Start:=1,
Length:=CopyTextLength).Copy

Bruce
 
S

Steve Rindsberg

Ha! It took me a day and a half, but this seems to do the trick:

Way to go, sir!

If .Name = "ActionKnowStrat" Then

' Lose the last "character" that throws an extra blank line at the
bottom
CopyTextLength = .TextFrame.TextRange.Characters.Count - 1
.TextFrame.TextRange.Characters(Start:=1,
Length:=CopyTextLength).Copy

' Make contact with the PlaceHolder
With TC.Slides(11).Shapes.Placeholders(2).TextFrame

' Replace its contents
.TextRange = BR.Slides(b).Shapes(c).TextFrame.TextRange

' Delete its contents
.TextRange.Delete

' Paste new contents
.TextRange.Paste
End With
End If

Thank goodness for the coordinates exporter at
http://www.rdpslides.com/pptfaq/FAQ00482.htm which always allows me to
identify the shapes on each slide.

I will try to post something about those disembodied placeholders borders
soon, just so you can see what I was talking about.


I'd like to see an example, definitely. Thanks!
 

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