Consulting

Results 1 to 17 of 17

Thread: Solved: how can I remove carriage returns in text window

  1. #1
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location

    Solved: how can I remove carriage returns in text window

    I have the following problem:
    Often I copy text from a scientific publication into a text window of PowerPoint. By doing so the carriage returns from the publication is also copied. That means, if I copy 10 lines in the publication I get 10 lines in the PowerPoint text window. I don't like that. I would rather like to have the text in one line.

    I envision the following approach to solve this problem:
    After pasting the text from the publication into the text window I would like to run a macro which removes the carriage returns in the active text window.

    Work outline
    1. write the macro that removes the carriage returns
    2. Add a button to the PowerPoint menu bar that executes the macro
    3. save macro as add-in and activate add-in in PowerPoint

    For points 1 and 2 in the work outline above I need help. If somebody could get me started with the macro that would be great. I have Excel VBA but no PowerPoint VBA experience. In regards to the button , I have done that before in Excel. I am trying to adapt this method to PowerPoint. After having had a quick look I figured that adapting the method will not be straight forward. Hints would be appreciated.

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    You need to check whether anything suitable is selected and then whether it's the shape itself or the text before carrying on

    Try this
    [VBA]Sub zapcr()
    Dim oshp As Shape
    Dim otxt As TextRange
    'check for type of selection
    If ActiveWindow.Selection.Type = ppSelectionNone _
    Or ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
    If ActiveWindow.Selection.Type = ppSelectionShapes Then
    Set oshp = ActiveWindow.Selection.ShapeRange(1)
    End If
    If ActiveWindow.Selection.Type = ppSelectionText Then
    Set otxt = ActiveWindow.Selection.TextRange
    Set oshp = otxt.Parent.Parent
    End If
    With oshp.TextFrame.TextRange
    .Text = Replace(.Text, vbCr, " ")
    End With
    End Sub[/VBA]

    You might also want to read this on buttons:

    http://www.pptfaq.com/FAQ00031.htm
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    thank you John. I will give this a try. Currently I am very busy with other stuff. But I will get back to this subject and respond again.

  4. #4
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    Thanks again John. Great help.

    Your script worked without any modifications. In combination with the script for menu buttons given at the link you provided I had a functional add-in in 10 minutes.

    I have one more question:
    Do you know where I can find a faceID table?
    I searched online but couldn't the right one.

  5. #5
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    There's one here
    http://officeone.mvps.org/faceid/

    Shyam Pillai has a similar one here
    http://skp.mvps.org/faceid.htm

    If you are using 2007 then you should really use ribbonX to add to the ribbon
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  6. #6
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    Thanks for the tip. I will look into ribbonX next.

  7. #7
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    John, I have one more question for you. Is it possible to modify your code so that carriage returns are removed in highlighted text?

    BTW, I created a menu button in the Home menu tab. The macro can now be executed from there. I attached the PowerPoint file with the macro. It can be used as a PowerPoint addin by saving it as an addin file.

    To create the button I had to modify the .rels file and create a customUI.xml file. I attached both files. To see both files in the Excel file add the extension .zip and look at the content.
    Last edited by dschmitt; 06-03-2010 at 05:32 AM.

  8. #8
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    Quote Originally Posted by dschmitt
    John, I have one more question for you. Is it possible to modify your code so that carriage returns are removed in highlighted text?
    This will limit the replacement to the selected text only:
    [vba]Sub zapcr()
    Dim oshp As Shape
    Dim otxt As TextRange
    'check for type of selection
    If ActiveWindow.Selection.Type = ppSelectionNone _
    Or ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
    If ActiveWindow.Selection.Type = ppSelectionShapes Then
    Set oshp = ActiveWindow.Selection.ShapeRange(1)
    Set otxt = oshp.TextFrame.TextRange
    ElseIf ActiveWindow.Selection.Type = ppSelectionText Then
    Set otxt = ActiveWindow.Selection.TextRange
    Set oshp = otxt.Parent.Parent
    End If
    With otxt
    .Text = Replace(.Text, vbCr, " ")
    End With
    End Sub[/vba]

  9. #9
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    wow, that was a quick response. Thanks Cosmo. I will package that macro tomorrow in the PowerPoint file together with the other macro and create an additional button. I will post the new PowerPoint file.

  10. #10
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    Quote Originally Posted by dschmitt
    wow, that was a quick response. Thanks Cosmo. I will package that macro tomorrow in the PowerPoint file together with the other macro and create an additional button. I will post the new PowerPoint file.
    What I posted updates the original code, and will process the selection whether it's a shape or selected text, so you shouldn't need an additional button unless you want one which will remove returns from the entire text box when text is selected. If so, you will need to change the name of one of the Subs.

  11. #11
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    Cosmo, I can use both macros. John's macro is useful after I copied text into an empty text window. Yours is useful if I add some text to an existing text window.

    I build in your macro into my previous PowerPoint addin. To do so I added one more button in the Home menu bar.

    I attached the new Excel file. Save as .ppam and activate the addin in PowerPoint options.

  12. #12
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    This code will replace all cr UNLESS text is highlighted when it will only replace those cr's
    [VBA]Sub zapcr()
    Dim oshp As Shape
    Dim otxt As TextRange
    'check for type of selection
    'replaces all text in shape unless
    'text is highlighted
    If ActiveWindow.Selection.Type = ppSelectionNone _
    Or ActiveWindow.Selection.Type = ppSelectionSlides Then Exit Sub
    If ActiveWindow.Selection.Type = ppSelectionShapes Then
    Set oshp = ActiveWindow.Selection.ShapeRange(1)
    Set otxt = oshp.TextFrame.TextRange
    End If
    If ActiveWindow.Selection.Type = ppSelectionText Then
    Set otxt = ActiveWindow.Selection.TextRange
    If Len(otxt) < 1 Then
    Set otxt = otxt.Parent.Parent.TextFrame.TextRange
    End If
    End If
    With otxt
    .Text = Replace(.Text, vbCr, " ")
    End With
    End Sub
    [/VBA]
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  13. #13
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    RibbonX is good! One point it should be GroupEditing not GroupEditingExcel. Still works because this is the default end position but it's not correct.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  14. #14
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    John, thanks for the combined macro. I thought it would be possible but didn't want to hassle with it any longer. I would have tried myself but PowerPoint VBA scripting is still too new to me. Too bad that there isn't a PowerPoint VBA recorder for MS Office 2007.

    Thanks also for editing the RibbonX code. I guess it was obvious that I copied the code from Excel

  15. #15
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    Now that I am using the "delete carriage return" macro successfully in PowerPoint I found that it would also be nice to have this tool in MS Word.

    How does the code for such a Word macro look like?
    I guess in the Word case appropriate would be that carriage returns are removed from highlighted text.

    John, Cosmo ?

  16. #16
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    just guessing I'm not a Word coder
    [VBA]Sub zapcr()
    With Selection
    .Text = Replace(.Text, vbCr, " ")
    End With
    End Sub[/VBA]
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  17. #17
    VBAX Regular
    Joined
    May 2010
    Posts
    65
    Location
    great. Thank you John. Your script does the job. I will post your solution at Word forum.

    I also asked the question at the Word forum. There Tinbenr suggested to use Find and Replace. It also does the job but a addin is faster.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •