PDA

View Full Version : Errors in Microsoft code for pasting?



RandomGerman
06-06-2015, 10:46 AM
Hi all,

this is strange! The following piece of code I found on an official microsoft site: https://msdn.microsoft.com/de-de/library/office/ff745532.aspx (at the bottom of the page)

But ... it doesn't work. (Only cutting, but not pasting)

What's wrong with the pasting command?
And: Are the MS websites not reliable?


Sub CutAndPasteText()

With ActivePresentation.Slides(1)
.Shapes(1).TextFrame.TextRange.Cut
.Shapes(2).TextFrame.TextRange.Words(1).InsertAfter.Paste
End With

End Sub

John Wilson
06-06-2015, 11:06 PM
Some (many) of the code samples on MSFT leave a lot to be desired. I wish they would declare variables for a start!

Try this instead of their code


Sub CutAndPasteText()
Dim strChosen As String
Dim otxR As TextRange
With ActivePresentation.Slides(1)
strChosen = .Shapes(1).TextFrame.TextRange.Text
If .Shapes(2).TextFrame.TextRange.Words.Count > 0 Then
Set otxR = .Shapes(2).TextFrame.TextRange.Words(1)
otxR.InsertAfter strChosen
'optional maybe
.Shapes(1).TextFrame.DeleteText
End If
End With
End Sub

RandomGerman
06-07-2015, 02:47 AM
That's interesting. So, you copy the text and delete it in the source shape. Is there a special reason to avoid the command "cut"?

Apart from that: In case shape 1 has bullets, they get lost. Bullets seem to be a feature making things complicated at any time, aren't they?

John Wilson
06-07-2015, 04:26 AM
The use of InsertAfter.Paste appears in several places in MSFT help but it is not correct.

InsertAfter requires a String to be specified - Paste (when pasting text) returns a TextRange. A TextRange is not the same as a string.

I don't understand the bullets comment if you InsertAfter the result will have the bullets of existing text in the second shape. Maybe you don't need to use InsertAfter, is there actually text in the second shape? Try and explain EXACTLY what you want to happen.

RandomGerman
06-08-2015, 03:52 AM
Well, after successfully testing your code I made some small changes, because one never knows how many words there might be in a TextBox. It seemed to be easier to me to paste the text of shape 1 in front of the text of shape 2 than the text of shape 2 after the one of shape 1 - and then change the position of the shapes to simulate a merge of the two textboxes. This works - but when I tried with bullets, they got lost.

My example:
Shape 1 is a TextBox, (Arial, FontSize12) filled with

Text 1


Bullet
Bullet



Shape 2 is a TextBox, too, but Calibri 18, and filled with

Text2: Paste in front of this


And the code:

Sub Merge()
Dim strChosen As String
Dim otxR As TextRange
Dim shp1 As Shape
Dim shp2 As Shape

With ActivePresentation.Slides(1)
strChosen = .Shapes(1).TextFrame.TextRange.Text
If .Shapes(2).TextFrame.TextRange.Words.Count > 0 Then
Set otxR = .Shapes(2).TextFrame.TextRange.Words(1)
otxR.InsertBefore strChosen
End If
End With

Set shp1 = ActiveWindow.Selection.ShapeRange(1)
Set shp2 = ActiveWindow.Selection.ShapeRange(2)

shp1.PickUp
shp2.Apply

shp2.Left = shp1.Left
shp2.Top = shp1.Top

shp1.Delete
End Sub

John Wilson
06-08-2015, 10:54 AM
There is little point in insertBefore word(1). Just insert before the text range.

Your code seems to jumble object variables and objects coded by zorder.

This works for me and keeps the bullet, does it not for you?


Sub Merge()
Dim strChosen As String
Dim otxR As TextRange
Dim shp1 As Shape
Dim shp2 As Shape

Set shp1 = ActiveWindow.Selection.ShapeRange(1)
Set shp2 = ActiveWindow.Selection.ShapeRange(2)
strChosen = shp1.TextFrame.TextRange.Text
Set otxR = shp2.TextFrame.TextRange
otxR.InsertBefore strChosen

shp1.PickUp
shp2.Apply

shp2.Left = shp1.Left
shp2.Top = shp1.Top

shp1.Delete
End Sub

RandomGerman
06-08-2015, 01:33 PM
13634

I just followed, what I found on the Microsoft page, that's why I used "Word (1)". Your code looks much more structured and less complicated.

But it is not keeping the bullets in my document, no matter which shape I choose first.