Count of lines in a text object

J

Joseph M. Newcomer

I want to iterate over all the lines in a text object, specifically, exactly the number of
lines in a text object. I have not discovered how to tell how many lines I have. Looking
for an "empty" line won't work because there are instances of empty lines.

I'm doing this through the Automation Interface from another program, so here's the code

//*****************************************************
// Handle indexing items in the Notes page
//*****************************************************
CSlideRange sliderange = slide.get_NotesPage();
CShapes shapes = sliderange.get_Shapes();
CPlaceholders placeholders = shapes.get_Placeholders();
CShape shape = placeholders.Item(2);
CTextFrame frame = shape.get_TextFrame();
if(frame.get_HasText())
{ /* text in frame */
CTextRange textrange = frame.get_TextRange();
int linecount = textrange.get_Count();
// ^^^^^ this line is clearly wrong, it was a guess
for(int i = 1; i <= linecount; i++)
{ /* scan lines */
int slideno;
slideno = slide.get_SlideNumber();
CTextRange lines = textrange.Lines(i, 1);
CString L = lines.get_Text();
if(L.GetLength() > 1 && L[0] == _T('*'))
{ /* maybe keyword */
TRACE(_T("[%d] \"%s\"\n"), slideno, L);
} /* maybe keyword */
} /* scan lines */
} /* text in frame */

The goal here is to identify all lines that start with * as being candidates for some
further analysis, represented here by the TRACE statement (I haven't written that code
yet)

Also, the explanation about how "removing the slide image" means I should use
placeholders(1) instead of placeholders(2) [chapter 6 of the Office 97 book on the Web at
http://msdn.microsoft.com/archive/en_us/office97/html/006.asp
is missing a lot, such as what is the slide image, how do I know it has been removed, and
how do I know whether I should use (1) or (2)?
joe

Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
 
S

Steve Rindsberg

I want to iterate over all the lines in a text object, specifically, exactly the number of
lines in a text object. I have not discovered how to tell how many lines I have. Looking
for an "empty" line won't work because there are instances of empty lines.

I'm doing this through the Automation Interface from another program, so here's the code

//*****************************************************
// Handle indexing items in the Notes page
//*****************************************************
CSlideRange sliderange = slide.get_NotesPage();
CShapes shapes = sliderange.get_Shapes();
CPlaceholders placeholders = shapes.get_Placeholders();
CShape shape = placeholders.Item(2);
CTextFrame frame = shape.get_TextFrame();
if(frame.get_HasText())
{ /* text in frame */
CTextRange textrange = frame.get_TextRange();
int linecount = textrange.get_Count();

The textrange has a .Lines.Count property that returns the lines displayed on screen.
In other words, if word wrap is on and you make the text box smaller forcing it to display
more lines, .Lines.Count increases.
// ^^^^^ this line is clearly wrong, it was a guess
for(int i = 1; i <= linecount; i++)
{ /* scan lines */
int slideno;
slideno = slide.get_SlideNumber();
CTextRange lines = textrange.Lines(i, 1);
CString L = lines.get_Text();
if(L.GetLength() > 1 && L[0] == _T('*'))
{ /* maybe keyword */
TRACE(_T("[%d] \"%s\"\n"), slideno, L);
} /* maybe keyword */
} /* scan lines */
} /* text in frame */

The goal here is to identify all lines that start with * as being candidates for some
further analysis, represented here by the TRACE statement (I haven't written that code
yet)

Also, the explanation about how "removing the slide image" means I should use
placeholders(1) instead of placeholders(2) [chapter 6 of the Office 97 book on the Web at
http://msdn.microsoft.com/archive/en_us/office97/html/006.asp
is missing a lot, such as what is the slide image, how do I know it has been removed, and
how do I know whether I should use (1) or (2)?

This will give you the type of each placeholder:
ActivePresentation.Slides(1).NotesPage.Shapes.Placeholders(x).PlaceholderFormat.Type

Notes pages are sort of an illegitimate child of the slide they're part of.
The slide image will be a placeholder type = 1 (title placeholder normally).
The notes text will be a type 2 (normally body text placeholder).

Sometimes the safest thing to do if you're worried about missing placeholders is to write a
function that iterates through all of the shapes or placeholders and returns the one you're
after or nothing. The calling code can do the equivalent of:

Set oSh = GetNotesTextPlaceholder(ThisSlide)
If Not oSh Is Nothing Then
' have your way with oSh
End If
 
J

Joseph M. Newcomer

Turns out there is another way: if the textrange.Lines(i,1) returns the empty string ""
then there are no more lines. A "blank line" turns out to return "\r\n".
joe

I want to iterate over all the lines in a text object, specifically, exactly the number of
lines in a text object. I have not discovered how to tell how many lines I have. Looking
for an "empty" line won't work because there are instances of empty lines.

I'm doing this through the Automation Interface from another program, so here's the code

//*****************************************************
// Handle indexing items in the Notes page
//*****************************************************
CSlideRange sliderange = slide.get_NotesPage();
CShapes shapes = sliderange.get_Shapes();
CPlaceholders placeholders = shapes.get_Placeholders();
CShape shape = placeholders.Item(2);
CTextFrame frame = shape.get_TextFrame();
if(frame.get_HasText())
{ /* text in frame */
CTextRange textrange = frame.get_TextRange();
int linecount = textrange.get_Count();

The textrange has a .Lines.Count property that returns the lines displayed on screen.
In other words, if word wrap is on and you make the text box smaller forcing it to display
more lines, .Lines.Count increases.
// ^^^^^ this line is clearly wrong, it was a guess
for(int i = 1; i <= linecount; i++)
{ /* scan lines */
int slideno;
slideno = slide.get_SlideNumber();
CTextRange lines = textrange.Lines(i, 1);
CString L = lines.get_Text();
if(L.GetLength() > 1 && L[0] == _T('*'))
{ /* maybe keyword */
TRACE(_T("[%d] \"%s\"\n"), slideno, L);
} /* maybe keyword */
} /* scan lines */
} /* text in frame */

The goal here is to identify all lines that start with * as being candidates for some
further analysis, represented here by the TRACE statement (I haven't written that code
yet)

Also, the explanation about how "removing the slide image" means I should use
placeholders(1) instead of placeholders(2) [chapter 6 of the Office 97 book on the Web at
http://msdn.microsoft.com/archive/en_us/office97/html/006.asp
is missing a lot, such as what is the slide image, how do I know it has been removed, and
how do I know whether I should use (1) or (2)?

This will give you the type of each placeholder:
ActivePresentation.Slides(1).NotesPage.Shapes.Placeholders(x).PlaceholderFormat.Type

Notes pages are sort of an illegitimate child of the slide they're part of.
The slide image will be a placeholder type = 1 (title placeholder normally).
The notes text will be a type 2 (normally body text placeholder).

Sometimes the safest thing to do if you're worried about missing placeholders is to write a
function that iterates through all of the shapes or placeholders and returns the one you're
after or nothing. The calling code can do the equivalent of:

Set oSh = GetNotesTextPlaceholder(ThisSlide)
If Not oSh Is Nothing Then
' have your way with oSh
End If

joe

Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
 
S

Steve Rindsberg

Turns out there is another way: if the textrange.Lines(i,1) returns the empty string ""
then there are no more lines. A "blank line" turns out to return "\r\n".

Good one. You may also want to check for (I think it's) Chr$(11). PPT uses a different
character for a user-entered forced linebreak.

joe

I want to iterate over all the lines in a text object, specifically, exactly the number of
lines in a text object. I have not discovered how to tell how many lines I have. Looking
for an "empty" line won't work because there are instances of empty lines.

I'm doing this through the Automation Interface from another program, so here's the code

//*****************************************************
// Handle indexing items in the Notes page
//*****************************************************
CSlideRange sliderange = slide.get_NotesPage();
CShapes shapes = sliderange.get_Shapes();
CPlaceholders placeholders = shapes.get_Placeholders();
CShape shape = placeholders.Item(2);
CTextFrame frame = shape.get_TextFrame();
if(frame.get_HasText())
{ /* text in frame */
CTextRange textrange = frame.get_TextRange();
int linecount = textrange.get_Count();

The textrange has a .Lines.Count property that returns the lines displayed on screen.
In other words, if word wrap is on and you make the text box smaller forcing it to display
more lines, .Lines.Count increases.
// ^^^^^ this line is clearly wrong, it was a guess
for(int i = 1; i <= linecount; i++)
{ /* scan lines */
int slideno;
slideno = slide.get_SlideNumber();
CTextRange lines = textrange.Lines(i, 1);
CString L = lines.get_Text();
if(L.GetLength() > 1 && L[0] == _T('*'))
{ /* maybe keyword */
TRACE(_T("[%d] \"%s\"\n"), slideno, L);
} /* maybe keyword */
} /* scan lines */
} /* text in frame */

The goal here is to identify all lines that start with * as being candidates for some
further analysis, represented here by the TRACE statement (I haven't written that code
yet)

Also, the explanation about how "removing the slide image" means I should use
placeholders(1) instead of placeholders(2) [chapter 6 of the Office 97 book on the Web at
http://msdn.microsoft.com/archive/en_us/office97/html/006.asp
is missing a lot, such as what is the slide image, how do I know it has been removed, and
how do I know whether I should use (1) or (2)?

This will give you the type of each placeholder:
ActivePresentation.Slides(1).NotesPage.Shapes.Placeholders(x).PlaceholderFormat.Type

Notes pages are sort of an illegitimate child of the slide they're part of.
The slide image will be a placeholder type = 1 (title placeholder normally).
The notes text will be a type 2 (normally body text placeholder).

Sometimes the safest thing to do if you're worried about missing placeholders is to write a
function that iterates through all of the shapes or placeholders and returns the one you're
after or nothing. The calling code can do the equivalent of:

Set oSh = GetNotesTextPlaceholder(ThisSlide)
If Not oSh Is Nothing Then
' have your way with oSh
End If

joe

Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: (e-mail address removed)
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
 

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