Paste Special msoClipboardFormatPlainText creates unwanted line break on Mac

139 views Asked by At

I'm trying to make my vba macros for PowerPoint work on a Mac, too. A lot of them do, but there are little things going wrong here or there.

One macro is for copying text from one selected shape to other selected shapes without formatting.

I use

.TextFrame2.TextRange.PasteSpecial msoClipboardFormatPlainText

The macro does as it should on Windows machines, and so it does on the Mac, only with one little problem: It creates an unwanted line break at the end of the text in the target shapes. Does anyone know a way to avoid this?

The option

.TextFrame2.TextRange.PasteSpecial msoClipboardFormatRTF

does not create this break, but it keeps the font color of the source shape, and same to

.TextFrame2.TextRange.PasteSpecial msoClipboardFormatNative

which keeps font color and font size of the source shape. The PlainText option is closest to my aim at the moment. But of course I wish I could have a perfect solution.

Any hint is appreciated. Thank you!

Edit: This is the complete code. After John's suggestion I added the Line starting with .text, but it made no difference on my Mac.

Sub DubTextOnly()

    Dim shp As Shape
    Dim shp1 As Shape
    Dim i As Integer

    On Error GoTo err

    If ActiveWindow.Selection.ShapeRange.Count < 2 Then
        MsgBox "Please select at least two shapes (no tables)"
        Exit Sub
    End If
    
Set shp1 = ActiveWindow.Selection.ShapeRange(1)

shp1.TextFrame2.TextRange.Copy
DoEvents
shp1.Tags.Add "Deselect", "yes"

    For Each shp In ActiveWindow.Selection.ShapeRange
        If shp.Tags("Deselect") = "yes" Then
        Else
        With shp
        With .TextFrame
            For i = 1 To 9
            With .Ruler
                .Levels(i).FirstMargin = 0
                .Levels(i).LeftMargin = 0
            End With
            Next
            End With
            With .TextFrame2
            With .TextRange
                .ParagraphFormat.Bullet.Type = ppBulletNone
                .PasteSpecial msoClipboardFormatPlainText
                .Text = Replace(.Text, vbCr & vbCr, vbCr)
            End With
        End With
        End With
        DoEvents
        End If
    Next shp
    For Each shp In ActiveWindow.Selection.ShapeRange
        If shp.Tags("Deselect") = "yes" Then
        shp.Tags.Delete "Deselect"
        End If
    Next shp
    Exit Sub
    
err:
    MsgBox "Please select at least two shapes (no tables)"
    
End Sub
1

There are 1 answers

3
John Korchok On

You're seeing the effect of different line endings in macOS and Windows. Windows uses a carriage return plus a line feed (vbCrLf in VBA), while macOS uses only a line feed vbLf. When you paste into PowerPoint, the program translates both characters into separate paragraphs, with the second one being empty.

Give this code a try:

Sub PasteTest()
    With ActivePresentation.Slides(1).Shapes(1).TextFrame2.TextRange
        .PasteSpecial msoClipboardFormatPlainText
        .Text = Replace(.Text, vbCr & vbCr, vbCr)
    End With
End Sub

It shouldn't affect operations in Windows, because double returns aren't created there.