[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Word 2003 VBA: '<Range>.Style'  does not return the style of the final paragraph of a table cell

Posted on 2007-10-11
32
Medium Priority
?
979 Views
Last Modified: 2016-08-29

Hi all,

I am using Word 2003 VBA.

I think I have found a little bug in Word. When I have a Range type variable, e.g.,

   Dim RangePara as Range

and it is set to the final paragraph of a table cell, then using  'RangePara.Style'  does not return the style. Instead it returns 'Nothing'.

(Btw, when I place the cursor on that final paragraph, when I look at the 'Style' field on the Formatting toolbar of the main document window, I do see that the correct style name appears.)

Does anybody know any command I can use to return me the style of the final paragraph of a table cell?

tia,

- avi

0
Comment
Question by:progtw
  • 15
  • 14
30 Comments
 

Author Comment

by:progtw
ID: 20062420
And how do you test if  'RangePara.Style'  contains Nothing?

tnx,

- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20064085
This is my code, which works as expected.

Sub ListParas()
Dim para As Paragraph
For Each para In ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs
    Debug.Print para.Range.Text, para.Style
Next para
End Sub

What does your code look like?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20064101
This uses a variable & still works OK

Sub ListParas()
Dim para As Paragraph
Dim RangePara As Range
For Each para In ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs
Set RangePara = para.Range
    Debug.Print RangePara.Text, RangePara.ParagraphFormat.Style
Next para
End Sub

0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 

Author Comment

by:progtw
ID: 20079934

Hi Graham,

Thanks for the response. When I saw your alternative command, ie with ParagraphFormat, I felt sure that that would be a successful work around to my problem. Alas, it wasn't.

Here is my code (accessing  '.Style'   the way you do it):

------------------------------------------------------------------------------------------------------------
Sub StyleApply(RangePara As Range, ListType As ListTypes)
   
    If Not InTable Then
        If IsListContinueStyle(RangePara.ParagraphFormat.Style) Then
                -
                -
       Else
           If IsListContinueStyle(RangePara.ParagraphFormat.Style) Then   '***

The statement marked by *** always blows up when I reach the final paragraph in the cell.

Well .., I have now meditated a bit on the difference betwene your code and mine, and there is a difference, as follows:

You processing loop accesses all the paras in the table cell by going thru Tables().Cell( , ).

My processing loop processes paragraphs only:

     For Each Parag In RangeSelect.Paragraphs
          Set RangePara = Parag.Range
          -
          -
    Next Parag

And I know when the loop reaches a table (I have to determine this since I have to do slightly different things in a table) I use the following:
    InTable = RangeSelect.Information(wdWithInTable)
    if InTable then
        -
        -
   End If

But I access the paragraphs inside the table using the paragraph range from the outermost processing loop, i.e.,

     For Each Parag In RangeSelect.Paragraphs
          Set RangePara = Parag.Range

So now I really DO think that I have uncovered a bug that only appears when you process paragrahs within a table merely as a sequence of paragraphs, i.e., without using the table( , ).cell( , ) 'path' to access the paragraphs. This is a real nuisance.

You see I first wrote code that worked for regular 'Body Text' paragraphs. And then once I got that working I had to think how I was going to get it to work in tables. So I decided that I could write and duplicate a lot LESS code if I simply write the aforementioned test for 'being inside a table'. Drat! I am going to have to write a whole new mechanism (partly copying a whole lot of code) just because of this bug case of the paragraph at the end of a cell.

Thanks.

- avi


 
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20080655
Ah I now see what you mean, thought I'm not sure that Microsoft would call it a bug. There is always a trailing paragraph in a table cell. It's visibility is not particularly consistent. It is not, for instance included in the paragraphs collection returned by Cell.Paragraphs. Nor does it show as ¶ when displaying formatting marks.

Also the length of text in a Cells range is always two more that it appears from the text, So if you do:
 
Cell.Range.Text = "a"
Debug.print Cell.Range.Text.Length

you will get 3, because the range includes the paragraph mark and the cell termination character, ASCII 13 and 7 respectively. If you want to use the text from a cell, you have to chop off the last two characters.

Incidentally, on my system the unexpected paragraph's style is Normal.
0
 

Author Comment

by:progtw
ID: 20080780

>> It is not, for instance included in the paragraphs collection returned by Cell.Paragraphs
But in the solution you presented, you said it DID work for you, ie you could return the style name. Or - do you mean that now you understand what I meant you also could not return the final paragraph's style? I.e., even if rewrite my code solution so that when I am inside a table I process Table.Cell...Range.Style, you are saying that even then it will not work - ie I can expect to have the same problem (but maybe not since it is inconsistent, as you say.

- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20080992
I can always get the style name of the last paragraph in the cells paragraphs collection, but I wasn't then referring to the 'extra' paragraph which is visible if we get all the paragraphs in the table range.
Since the cell collection does not return the extra paragraph, we obviously cannot deduce its style by that method.

When I said inconsistent, I meant to say that not all methods will show it, though I am sure that any one method will always return the same result.

I don't know exactly what you are doing in  your program so I can't be sure about what you need to change.
0
 

Author Comment

by:progtw
ID: 20082008
>> When I said inconsistent, I meant to say that not all methods will show it, though I am sure that any one method will always return the same result.
Yes, I understood that.

>> I can always get the style name of the last paragraph in the cells paragraphs collection, but I wasn't then referring to the 'extra' paragraph which is visible if we get all the paragraphs in the table range.
>> There is always a trailing paragraph in a table cell.
Now I understand - I think.  You are saying that when I loop thru paragraphs, and I happen to be inside a table, then I pick up 'hidden paragraphs' at the end of each cell? Well, I will have to check if that actually happens. But my impression was that it is NOT happening, since I already noticed in the IDE that the text in that final paragraph range is the text as it appears in the Word doc of the final list item. Or on the other hand, maybe the text that is shown is just a duplication of the previous, REAL list item. That could be. I will have to carefuly follow the processing loop and see what happens at the end. I will count the number of loops to see if it's adding an extra one.

Thanks,

- avi





0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20083059
If you display (some of) the paragraph text and the text length, the style and the count of tables in the range, you will see that the paragraphs in question have no text apart from the paragraph mark.

     For Each Parag In RangeSelect.Paragraphs
          Set RangePara = Parag.Range
          debug.print Left$(parag.range.text.length,5), parag.range.text.length, parag.style, parag.range.tables.count
          -
    Next Parag
0
 

Author Comment

by:progtw
ID: 20083922

And how would I test if  'RangePara.Style'  contains Nothing?

- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20085095
Using the word 'Nothing' might be a bit confusing, because it can refer to an empty object variable.

And I don't need help in getting confused: I seem to have done it myself yesterday. I guess that you must be a bit puzzled.

My experiments now show that each cell has at least one paragraph. The only odd thing about the last one is that it does not display as ¶.

It can contain text, just like any other paragraph.

However there is an extra empty paragraph at the end of each row, with style called Table Grid. I don't know why it looked so different yesterday.

This is the code that I am using today.

Sub Analyse()
    Dim para As Paragraph
    For Each para In ActiveDocument.Paragraphs
        Debug.Print para.Range.Text, para.Range.Style
    Next para
End Sub

0
 

Author Comment

by:progtw
ID: 20089428
This time I am quoting and commenting on myself ...
>> You are saying that when I loop thru paragraphs, and I happen to be inside a table, then I pick up 'hidden paragraphs' at the end of each cell?

I just realised there is not much point in testing that. I already note that the code crashes on a 'Nothing' style where my final visible paragraph in the table cell does NOT get processed. So it must be the final visible paragraph that gets stuck, ie the VBA can't properly read its style. This is NOT coming from any extra invisible paragraph at the end of the cell (although there may also be such a thing as that - but that's not my problem).

Also testing for Nothing will only help me catch the error, but I still will not know what the style is for that paragraph.

So just because of this I will have to redesign the code.
Before I waste my time are you SURE that when you process paragraphs in the table, like in your :

      For Each para In ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs

you can really read the style of the final line?

You know what - I tried it myself. I used basically your code and selected my test table and have my test list in cell(2,2), so I specified my test items in your code as follows:

          Sub ListParas()
          Dim para As Paragraph
          Dim RangePara As Range
                    For Each para In Selection.Tables(1).Cell(2, 2).Range.Paragraphs
                    Set RangePara = para.Range
                    Debug.Print RangePara.Text, RangePara.ParagraphFormat.Style
                    Next para
          End Sub

And guess what ... at least on my system, the SAME THING happens, i.e., it crahes on trying to read the style of the final paragraph in the cell. So it's good that I tried this - I won't waste my time.

So what do I do now...?

- avi
0
 

Author Comment

by:progtw
ID: 20089431
And, btw, I would definitely call this a bug...
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20089709
I have uploaded a small document zipped with the debug output of both macros pasted into notepad

https://filedb.experts-exchange.com/incoming/ee-stuff/5047-tableparagraphs.zip

See if they give you the same result as I get.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20089716
Off to bed, so I can't guarantee to follow up till morning.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20091369
I created a document with a table and ran both macros, copying the debug ouput into a Notepad file. I have zipped up the Notepad and the document files and uploaded them to ee-stuff.
See what happens if you run the macros.
If you get the same as me then it is worth investigating your document as that is where the differences lie.

If you upload a similar document and notepad file that you have created, than I will be able to look at it in turn.

0
 

Author Comment

by:progtw
ID: 20103685
I ran your test program on your test table and then on my table .

When I ran your test program I got the same resuls as you did, ie no problems
When I ran your test program on my table I got the same resuls as *I* did with my code, ie on the final list item in a cell the code blows up since the .Style contains 'Nothing'.

Then I tried an experiment. I converted the styles of my Table's list items all to Table Grid to see hjow it would take those. Then I ran your code. This time no problem. Then I asigned another built-in style, List, and tried again. Again no problem. So it seems that Word has a problem with the final list item of a cell when it is a user-defined style.

I uoloaded my test file. Here is the upload info:
           View all files for Question ID: 22888617
           https://filedb.experts-exchange.com/incoming/ee-stuff/5075-TableParagraphs-withMyTable-AM.ziphttps://filedb.experts-exchange.com/incoming/ee-stuff/5047-tableparagraphs.zip
 

           Direct link to your file
           https://filedb.experts-exchange.com/incoming/ee-stuff/5075-TableParagraphs-withMyTable-AM.zip
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20104269
Now that I've seen and understand the problem that you are getting, I can only agree that it is a bug.

I don't expect that I can devise a get-around that you couldn't do yourself.

Unfortunately, like most of the contributors here, I have no link to Microsoft to ask if it is a known problem or if they want to log it as a new one. I expect that they will want to close the 2003 book as soon a possible anyway.
0
 

Author Comment

by:progtw
ID: 20105132
Hi Graham,

>> Now that I've seen and understand the problem that you are getting, I can only agree that it is a bug.
Right.I will just have to read up on how to catch the error. Some command like On Error, isn't it?

>> I expect that they will want to close the 2003 book as soon a possible anyway.
And I expect that core parts of the Word API will be just brought forward from 2003 to 2007. Expect to see the same problem. Hope I am wrong.

Well thanks for all your effort.  Although we didn't find a solution, it is also an achievement to track something down and declare it as a bug . So what is the accustomed and accepted way of closing such a question, ensuring also that it stays on EE for psterity and is not deleted?

- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20105460
Yes.
Something like this
Sub ListParas()
    Dim para As Paragraph
    Dim RangePara As Range
   
    On Error GoTo ListParas_Error 'start programmer error handling
    For Each para In Selection.Tables(1).Cell(2, 1).Range.Paragraphs
        Set RangePara = para.Range
    Next para
   
ListParas_Exit:
On Error GoTo 0 're-enable normal error processing
    Exit Sub
ListParas_Error:
    Select Case Err
        Case 91 'check that it is the expected error
        Resume Next
        'Resume Label2 'Resume Next is not always appropriate
    Case Else
        MsgBox "Unexpected error: " & Err.Description & " in" & vbCrLf & "ListParas procedure"
        End
    End Select
End Sub


It could well be. However rewriting for XML might mean that the error was not even possible with the new structure. I don't plan on buying 2007 to find out.
Your problem has not been solved, except to identify it as a bug, which you did for yourself.
Post a points-free question in Community Support, asking that the question be closed an the points refunded.
0
 

Author Comment

by:progtw
ID: 20105688
Thanks.

Bye for now, until next time.

- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20107435
OK avi. Good luck with the project.
0
 

Author Comment

by:progtw
ID: 20137500
Well, I thought of a simple work around for this - along the lines of "if you can't beat them, join them".

The Style property doesn't work well for the final para of a Table cell, so I decided what I shoudl do is to give it the extra paragraph. Since my program processes user selections, I can immediately 'go to' the end of the selection and add an extra (blank) paragraph immediately *after* the user selection . In this way the processing will always find only non-end-of-cell paragraphs. When processing has been completed I then clean uo by deleting the extra paragraph that I added after the selection.

I haven't gotten around to trying it yet, but I assume it will work.

For those reading this who don't have a user selection, you could do this:

Create an error handler, process as usual until an error is trapped, and then go back add the extra paragraph - you should then be able to read the style of the current paragraph. Then clean up afterwards as above.

- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20137521
Thanks for keeping us posted.
0
 

Author Comment

by:progtw
ID: 20166782
Btw, I just tried my code on Word 2000, and guess what ...

the bug does not occur.

Oh thank you MS, thank you so much <sigh>

Btw, I think maybe I will redesign the algorithm to process the selected cells when it detects it is inside a table, like your first example:

        For Each para In ActiveDocument.Tables(1).Cell(1, 1).Range.Paragraphs
              Debug.Print para.Range.Text, para.Style

I assume that never gives problems.
- avi
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20167650
That makes some sense (not much, but a bit) when you remeber that there were no table styles in Word 2000.

I can't promise 'never gives problems'. It didn't fail in the tests here, but I don't recall previously using code to step through all paragraphs when tables were included.
0
 

Author Comment

by:progtw
ID: 20168297
The s/w code I am writing to deal with this bug is getting uglier and uglier. (My original design didn't take into account such a bug in Word.) That's a sign. When code gets ugly that's a sign you are coding wrong. Actually - same in life, in general. When things start getting very ugly, it's a sign you're doing something wrong big time.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20168511
Well put, but it's a bit of a wrench to abandon all the work that you've already put in.

You really want to be sure that your current direction is the wrong way. The risks of any new plan may be less obvious, but they are still there.

Luckily for us, we are only programmers, not politicians :)
0
 

Author Comment

by:progtw
ID: 20223846
>> Well put, but it's a bit of a wrench to abandon all the work that you've already put in.
Well, I can still use most of it - I think.
The main processing loop currently processes paragraphs, as I said. However even if I change it and start out by testing
"if In Table Then  .... Else .. . "
then if I am not in a table I process parags as before, and even if I am in a table I will can then also reuse the parag processing code, as follows

  For each CellPtr in Selection.Cells
      For Each Parag in CellPtr.Paragraphs
       -  
       - < Original paragraph processing code > 
       -  
      Next Parag
  Next CellPtr

... more or less ...

- avi
0
 

Accepted Solution

by:
ee_auto earned 0 total points
ID: 29432698
Question PAQ'd, 250 points refunded, and stored in the solution database.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question