TextRange.Select: Invalid request

  • Thread starter Örjan Skoglösa
  • Start date
Ö

Örjan Skoglösa

(Newbie with Powerpoint and first posting here)

I´m trying to go through some ppt-files and replace some of the
paragraph marks with blanks.

As it not seems to be possible to do so with normal S&R (?) I´m
trying to use a VBA-code that iterates through all the characters in
the presentation and checks them for being carriage returns (ASC =
13). If so, the character is replaced by a blank.

It works well on the first slide, but when it should go to the next
slide it stops at the line ' myChar.Select' and shows the following
error code:

Run time error '-2147188160 (80048240)
TextRange.Select: Invalid request.
The window must be in slide or note view.

If I add 'On Error Resume Next' at the top of the code, the code
execution does loop infinitely on the last para mark in the first
slide.

Any help very much appreaciated.

Örjan

Sub Replace_CR()
' On Error Resume Next
For Each sld In Application.ActivePresentation.Slides
For Each shp In sld.Shapes
With shp.TextFrame
If .HasText Then
For Each myChar In .TextRange.Characters
If Asc(myChar) = 13 Then
myChar.Select
MsgBox "test"
ActiveWindow.Selection.TextRange.Delete
ActiveWindow.Selection.TextRange.InsertBefore (" ")
MsgBox "test"
End If
Next myChar
End If
End With
Next shp
Next sld
End Sub
 
S

Steve Rindsberg

(Newbie with Powerpoint and first posting here)

Welcome!

One little tip: It's a good idea to start a new post rather than reply to an
existing series of messages when you want to start a new topic like this.
It works well on the first slide, but when it should go to the next
slide it stops at the line ' myChar.Select' and shows the following
error code:

Run time error '-2147188160 (80048240)
TextRange.Select: Invalid request.
The window must be in slide or note view.

If I add 'On Error Resume Next' at the top of the code, the code
execution does loop infinitely on the last para mark in the first
slide.

Any help very much appreaciated.

Örjan

Sub Replace_CR()
' On Error Resume Next
For Each sld In Application.ActivePresentation.Slides
For Each shp In sld.Shapes
With shp.TextFrame
If .HasText Then

' Try this instead:
sh.TextFrame.TextRange.Text = _
Replace(sh.TextFrame.TextRange.Text, Chr$(13), "", , , vbBinaryCompare)

' And comment out from here:
For Each myChar In .TextRange.Characters
If Asc(myChar) = 13 Then
myChar.Select
MsgBox "test"
ActiveWindow.Selection.TextRange.Delete
ActiveWindow.Selection.TextRange.InsertBefore (" ")
MsgBox "test"
End If
Next myChar
' ... to here
End If
End With
Next shp
Next sld
End Sub

I'm not sure I understand why you're getting the specific error message you've
seen; I suspect the problem has more to do with your loop:

At the beginning of the loop, VBA sets an implicit counter to the number of
characters in the string (for each mychar in ....etc)

So suppose it's a 10 character string; vba expects to loop ten times.
But partway through the loop, you delete a character from the string, so when
vba references the tenth character of a nine-or-less character string, it
throws an error.

If you're going to delete things in a loop, the usual trick is to:

Dim X as Long
For X = Whatever.Count to 1 Step -1
Test and delete if need be
Next X
 
D

David M. Marcovitz

Try this:

Sub Replace_CR()
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
With shp.TextFrame
If .HasText Then
Do
Set tmpRange = .TextRange.Replace(Chr$(13), " ")
Loop Until tmpRange Is Nothing
End If
End With
Next shp
Next sld
End Sub


--
David M. Marcovitz, Ph.D.
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
Ö

Örjan Skoglösa

I did not know I could use the Replace function like this. It works
like a charm.

Thanks David.

There is only one minor drawback. The reason I used the select command
was (besides not knowing the replace command well enough) that I do
not want to replace all carriage returns. Therefore I need to see the
CR before manually (e.g. with a userform) deciding if I want to
replace it.

Do you think that would be possible?

TIA
Örjan

PS. See also my follow up to Steves answer

Thanks also
 
Ö

Örjan Skoglösa

Welcome! Thanks Steve.

One little tip: It's a good idea to start a new post rather than reply to an
existing series of messages when you want to start a new topic like this.

Sorry. It was not my intention to reply.

As said in my follow-up to David, the replace function works very
well, but I need to see the CR before replacing it.

I therefore tried your suggestion and rewrote the "select part" of the
code (see below), but sadly it did not work out. I got the same error
at the same position.

TIA
Örjan

Sub Replace_CR_Nyversion()
'On Error Resume Next
For Each sld In Application.ActivePresentation.Slides
For Each shp In sld.Shapes
With shp.TextFrame
If .HasText Then
Dim X As Long
For X = .TextRange.Characters.Count To 1 Step -1
If Asc(.TextRange.Characters(X)) = 13 Then
.TextRange.Characters(X, 1).Select 'error here
MsgBox "test"
End If
Next X
End If
End With
Next shp
Next sld
End Sub
 
D

David M. Marcovitz

The following runs in Slide Show mode (where I usually work) so it must
be attached to a button on a slide, and the button must be clicked while
running the show. It basically does what you want by changing the color
of the line your about to change to red and then back to what it was.
This works as long as the color of each line is always the same.

Sub Replace_CR()
Dim shp As Shape
Dim tmpRange As TextRange
Dim sld As Slide
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
With shp.TextFrame
If .HasText Then
Set tmpRange = .TextRange
Do
If Not tmpRange.Find(Chr$(13)) Is Nothing Then
ActivePresentation.SlideShowWindow.View.GotoSlide _
sld.SlideIndex
Set newTmpRange = tmpRange _
.Paragraphs _
(2, tmpRange.Paragraphs.Length - 1)
tmpColor = tmpRange.Paragraphs(1).Font.Color.RGB
tmpRange.Paragraphs(1).Font.Color.RGB = vbRed
answer = _
MsgBox("Do you want to change this?", vbYesNo)
If answer = vbYes Then
tmpRange.Paragraphs(1).Font.Color.RGB = tmpColor
tmpRange.Replace Chr$(13), " "
Else
tmpRange.Paragraphs(1).Font.Color.RGB = tmpColor
End If
Set tmpRange = newTmpRange
End If
Loop Until tmpRange Is Nothing Or _
tmpRange.Find(Chr$(13)) Is Nothing
End If
End With
Next shp
Next sld
End Sub

--
David M. Marcovitz, Ph.D.
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
S

Shyam Pillai

David,
The routine is not entirely correct and could create an endless loop.
For example: a simple test - replace every 1 with a underscore before it
will create an endless loop.

Set tmpRange = .TextRange.Replace("1", "_1")

The Replace method returns the textrange containing the replaced string. i.e
if it replaces 1 with _1, tmpRange will contain the value "_1" hence when
you perform the next replace operation in the loop, you need to advance the
position of the character (in the specified text range) after which you want
to search for the next occurrence which would be the last search position +
the number of characters in the textrange returned (tmprange). This is set
by the argument - After of the Replace method.

Also, the code should check if the shape has textframe before you check for
HasText.

A complete example is provided here: http://www.mvps.org/skp/ppt00025.htm#2

Regards
Shyam Pillai
 
D

David M. Marcovitz

Shyam,

Good points, but he was replacing a carriage return with a space so there
was no danger of an infinite loop. However, for future replaces, I'll
keep that in mind. Thanks.

--David

--
David M. Marcovitz, Ph.D.
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
D

David M. Marcovitz

So why did the original code not work. I think the problem has to do
with selecting characters that are not on the currently visible slide.
That is, when he went to the next slide, he tried to select a character
while the first slide was showing and PowerPoint didn't automatically
jump to the next slide so it complained. Is this true?
--David

--
David M. Marcovitz, Ph.D.
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
S

Shyam Pillai

Örjan,
Run time error '-2147188160 (80048240)
TextRange.Select: Invalid request.
The window must be in slide or note view.

For any selection method to work, the slide, shape/text being selected must
be activated and in the correct view.
You would need to add a couple of lines:
' ---------------------------
ActiveWindow.ViewType = ppViewSlide 'Switch to slide view since we are
working with text
For Each sld In Application.ActivePresentation.Slides
ActiveWindow.View.GotoSlide Index:=sld.SlideIndex ' Move the current slide
being manipulated
For Each shp In sld.Shapes
' ---------------------------
Regards
Shyam Pillai

http://www.mvps.org/skp
 
Ö

Örjan Skoglösa

Hi David,

thanks very much for your concern and helpfulness.

Sadly, it does not really work the way I want it to.

I´m not quiet sure what happens, but if I afterwards paste the text
into Word and have a look at it there, it seems as if the code only
replaces the first CR in each shape. And also replaces some other
characters.

Just for your information, I ran it on 1 shape and then pasted the
result here down below. As the CR returns do not come along when
pasting, I have inserted rubel signs where they appear in Word.
(Please be aware that perhaps some extra CR´s will be inserted by your
browser, especially in the long first line of the second shape).

It looks as if only the first CR is replaced. You can also see that it
has replaced three "normal" characters in the first row with blanks.

Best regards,

Örjan

PS. (I have been thinking about your explanation why my code throws
the error and I wonder whether the line in your code that says:
ActivePresentation.SlideShowWindow.View.GotoSlide _
sld.SlideIndex
could be used in my code, moving the view into the right position
before the select command? Or does that only work in Slide Show mode?


=====================
The original shape:
(Which also has "soft line feeds" and tabulators affecting the
layout:)

Abhilfe: 1. Tausch der verbauten Antriebseinheit (Q4 oder ¤
kleiner) durch die optimierte Antriebseinheit mit ¤
Q5-Stand (oder größer)
¤
2. Anlagefläche der flexiblen ¤
Wellen an der Karosserie ¤
(im Bereich D-Säule) mit Filz¤
abkleben¤
===============
The shape after running the code and answering vbYes every time:

Abhilfe: 1. Tausch der ve b uten Antriebseinheit (Q oder
kleiner) durch die optimierte Antriebseinheit mit
Q5-Stand (oder größer) ¤
¤
2. Anlagefläche der flexiblen ¤
Wellen an der Karosserie ¤
(im Bereich D-Säule) mit Filz ¤
abkleben¤
 
D

David M. Marcovitz

I'm not sure why it didn't work for you. It worked for me. My code is
meant to run in slide show view. Since you are trying to run in Edit
View, you should use Shyam's code (from another response) to jump to
another slide:

ActiveWindow.View.GotoSlide Index:=sld.SlideIndex

This is the format for going to another slide in Normal or Edit view. I
don't have time to play with this any more now, but you might be able to
pop that into your code and get it to work. I'm pretty sure it was one
of the problems you were having. Good luck.

--David
--
David M. Marcovitz, Ph.D.
Director of Graduate Programs in Educational Technology
Loyola College in Maryland
Author of _Powerful PowerPoint for Educators_
http://www.loyola.edu/education/PowerfulPowerPoint/
 
Ö

Örjan Skoglösa

Thanks David and thanks Shyam

I used that "GotoSlide" and now it works wonderful.

BR
Örjan

Just FYI:

Sub Replace_CR_sista()
'On Error Resume Next
Dim X As Long
For Each sld In Application.ActivePresentation.Slides
mySldIndex = sld.SlideIndex
For Each shp In sld.Shapes
With shp.TextFrame
If .HasText Then
For X = .TextRange.Characters.Count To 1 Step -1
If Asc(.TextRange.Characters(X)) = 13 Or _
Asc(.TextRange.Characters(X)) = 11 Or _
Asc(.TextRange.Characters(X)) = 9 Then
'next line was the solution
Application.ActiveWindow.View.GotoSlide mySldIndex
.TextRange.Characters(X, 1).Select
answer = MsgBox("Do you want to change this?", vbYesNo)
If answer = vbYes Then
ActiveWindow.Selection.TextRange.Delete
ActiveWindow.Selection.TextRange.InsertBefore (" ")
End If
End If
Next X
End If
End With
Next shp
Next sld
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top