PDA

View Full Version : Solved: how can I remove carriage returns in text window



dschmitt
05-28-2010, 06:11 PM
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.

John Wilson
05-29-2010, 01:59 AM
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
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

You might also want to read this on buttons:

http://www.pptfaq.com/FAQ00031.htm

dschmitt
05-30-2010, 05:32 AM
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.

dschmitt
06-01-2010, 06:54 AM
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.

John Wilson
06-01-2010, 07:07 AM
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

dschmitt
06-01-2010, 07:35 AM
Thanks for the tip. I will look into ribbonX next.

dschmitt
06-03-2010, 05:14 AM
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.

Cosmo
06-03-2010, 05:21 AM
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:
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

dschmitt
06-03-2010, 05:30 AM
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.

Cosmo
06-03-2010, 06:09 AM
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.

dschmitt
06-03-2010, 09:02 PM
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.

John Wilson
06-04-2010, 05:59 AM
This code will replace all cr UNLESS text is highlighted when it will only replace those cr's
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

John Wilson
06-04-2010, 06:15 AM
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.

dschmitt
06-04-2010, 07:13 AM
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 :)

dschmitt
06-08-2010, 04:45 AM
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 ?

John Wilson
06-08-2010, 10:36 AM
just guessing I'm not a Word coder
Sub zapcr()
With Selection
.Text = Replace(.Text, vbCr, " ")
End With
End Sub

dschmitt
06-08-2010, 05:41 PM
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 (http://word.mvps.org/FAQs/General/DeleteParaMarksAtEndOfLines.htm). It also does the job but a addin is faster.