2015-08-08 57 views
0

我看到this post但我無法修改用於PPT演示的VBA腳本。幾乎每個幻燈片在文本框中都有文本。但是,在某些文本框的末尾有多個換行符(輸入命中),在某些地方大約爲1-3。我想要一個宏來刪除那些不必要的換行符。告訴我我在做什麼錯在這裏(2個腳本):刪除Powerpoint VBA中的換行符

Sub RemoveSpaces(osh As Shape) 

Dim oSl As Slide 
    Dim osh As Shape 


    With ActivePresentation 

For Each oSl In .Slides 
    For Each osh In oSl.Shapes 
     With osh 
      If .HasTextFrame Then 
       If .TextFrame.HasText Then 
        If Right$(osh.TextFrame.TextRange.Characters(osh.TextFrame.TextRange.Length, 2)) = vbCrLf Then 
        osh.TextFrame.TextRange.Text = Left$(osh.TextFrame.TextRange.Text, Len(osh.TextFrame.TextRange.Text) - 2) 
        End If 
       End If 
      End If 
     End With 
    Next 
Next 

    End With 
End Sub 

Sub RemoveSpaces() 

Dim oSl As Slide 
    Dim osh As Shape 


    With ActivePresentation 

For Each oSl In .Slides 
    For Each osh In oSl.Shapes 
     With osh 
      If .HasTextFrame Then 
       If .TextFrame.HasText Then 
        If osh.TextFrame.TextRange.Characters(osh.TextFrame.TextRange.Length - 2, 2).Text = vbCrLf Then 
        osh.TextFrame.TextRange.Characters(osh.TextFrame.TextRange.Length - 2, 2).Delete 
        End If 
       End If 
      End If 
     End With 
    Next 
Next 

    End With 
End Sub 

回答

0

當我按在PowerPoint中輸入,它顯然增加了一個垂直選項卡是11.嘗試的ASCII碼如下:

Sub RemoveSpaces() 

Dim oSl As Slide 
    Dim osh As Shape 


    With ActivePresentation 

For Each oSl In .Slides 
    For Each osh In oSl.Shapes 
     With osh 
      If .HasTextFrame Then 
       If .TextFrame.HasText Then 
        Do While osh.TextFrame.TextRange.Characters(osh.TextFrame.TextRange.Length - 1, 1).Text = Chr(11) 
         osh.TextFrame.TextRange.Characters(osh.TextFrame.TextRange.Length - 1, 1).Delete 
        Loop 
       End If 
      End If 
     End With 
    Next 
Next 

    End With 
End Sub 
+0

出於某種原因,該腳本只能部分。在一些地方,它刪除所有額外的空間,而在其他地方沒有。當我將文本複製到即時窗口時,我看到那些空格用'男性符號'簽名。也許某些條件,如「字母數字」或TRIM(characters.length -1,1).text =「」會工作嗎? –

1

Powerpoint的這種方式有點怪異;行和段落結尾可能會有所不同,具體取決於您擁有的PPT版本以及形狀是標題佔位符還是其他類型的形狀。

我得在PowerPoint中常見問題我認爲,更詳細地解釋一個頁面:

段落結束和換行符 http://www.pptfaq.com/FAQ00992_Paragraph_endings_and_line_breaks.htm

+0

我正在使用PowerPoint 2013.不確定此信息仍然有效。當粘貼到即時窗口時,我看到'男性標誌'。就像這裏:http://stackoverflow.com/questions/4091345/what-is-this-strange-character-and-how-can-i-get-rid-of-it –

+0

該信息仍然有效。男性的標誌是Chr $(11)。我更新了常見問題解答頁面,以反映這些信息適用於PPT 2007 *及以後*。感謝您的推動。 –