none
Word document having around 10k paragraphs throwing error in Office 64 Bit RRS feed

  • Question

  • Hi,

    In 64 Bit office, when trying to execute following VBA code on a word document having 10K or more paragraphs, it is throwing "Out of Stack Error 28" message box.

     

    Public Function ParagCount()
    Dim a As Word.Paragraph
    Dim b As Word.Paragraph
    Dim i As Integer

    Set a = ThisDocument.Paragraphs.First

    For i = 1 To 9999

    Set b = a.Next
    Set a = b
    Next i

    Set b = ThisDocument.Paragraphs.Item(10000)

    Set a = b

    End Function

     

    Can somebody point out the problem or let me know the alternative approach to do the same?

    Thanks

    Uttam


    Monday, April 11, 2011 6:16 AM

All replies

  • Hi Uttam,

    I think this might be caused by a lack of memory? I could reproduce it on my system.

    Why do you need 'set a= b' in the loop?
    If you delete this line, the code runs fine.

    If you need another approach, what is it that you want to archieve with this code?

    All the best,

    Astrid

    Monday, April 11, 2011 2:41 PM
  • Why do you need 'set a= b' in the loop?

    Otherwise it will never change a and a.next will remain the same in each iteration of the loop.

    Monday, April 11, 2011 3:40 PM
  • I understand, but what does this code need to do?
    Monday, April 11, 2011 4:28 PM
  • This is not the complete code. I have removed few lines from the original implementation. The purpose of this code is to go to each paragraph and assign some ID to them. Since somewhere around i = 7050 to 7060, some corruption is being introduced, I want to know the reason of that corruption and what is the problem with this implementation. What's wrong with this code.
    Monday, April 11, 2011 5:10 PM
  • Hi Uttam,

    I think you need an "On Error GoTo CorruptedPara" statement. The Out of Stack is probably happening because the procedure trys to read past what Word thinks is the number of paragraphs in the document.

    To test this you might put is a MsgBox or Debug.Print command to show you what Word believes to be the number of paragraphs.

    Hope this helps


    Regards
    Monday, April 11, 2011 5:33 PM
  • There is nothing wrong with the code. It works just fine on 32-bit (on 32-bit Windows - I don't have a handy 32 bit Office on 64-bit Windows to check, but it would be interesting to know what happens) but fails on 64 bit.
     
    The error seems to occur on the line after "Next i" and if the procedure is run a second time, Word goes into 'not responding' mode and abends in response to any key press (well, I haven't checked them all!)
     
    I'm sorry but I have no suggestions for workarounds at the moment.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, April 11, 2011 6:50 PM
  • Hi Uttam,

    In my previous post I was assuming that the code would work but when the corrupt paragraph was encountered the procedure would receive an unhandled exception. The procedure is setup to loop 10k times but receiving an exception at 7060 might generate the error. I had not tried the code.

    After seeing Tony’s post I wondered why the code didn’t work.

    In testing  on a 32 bit XP 32 bit Office 2007 and a 64 bit Win 7 32 bit Office 2010 machine,  I received an error, the same on both, when it attempted to execute the Set B = a.Next command the second time. The error I received was Object variable or Block variable not set. To restate, the procedure ran once, but then encountered the error on the next loop, the same error in both environments.

    Changing the “ThisDocument” reference to ActiveDocument corrected the problem and all iterations of the For/Next loop worked.

    In reading your note I noticed you wanted to assign an Id to each paragraph. The following code will accomplish that.

    Sub ParagCount()

        Dim paraRange As Range

        Dim x As Integer

        For x = 0 To 9999

            Set paraRange = ActiveDocument.Paragraphs(1).Next(x).Range

            paraRange.Collapse wdCollapseStart

            paraRange.InsertAfter x + 1 & vbTab

        Next x

    End Sub


    Regards
    Monday, April 11, 2011 9:27 PM
  • I must admit to not running the _exact_ same code (I simply had the loop followed by 'End Sub' - and it was on that line that I errored out) but, encountering the error, I was content that what I had was good enough.
     
    It seems to me that you have a different error (was the code in the same document as the paragraphs?), although your test would seem to confirm that the error does not occur on 32-bit Office on 64-bit Windows, which was the environment I didn't have, and is limited to 64-bit Office.
     
    Reading between the lines, I suspect that Uttam may actually have been encountering a slightly different error or, at least, a different symptom, and I would be interested to see the original code.
     

     

    Enjoy,
    Tony
    www.WordArticles.com
    Tuesday, April 12, 2011 8:26 AM
  • Hi Uttam,

    In my previous post I was assuming that the code would work but when the corrupt paragraph was encountered the procedure would receive an unhandled exception. The procedure is setup to loop 10k times but receiving an exception at 7060 might generate the error. I had not tried the code.

    After seeing Tony’s post I wondered why the code didn’t work.

    In testing  on a 32 bit XP 32 bit Office 2007 and a 64 bit Win 7 32 bit Office 2010 machine,  I received an error, the same on both, when it attempted to execute the Set B = a.Next command the second time. The error I received was Object variable or Block variable not set. To restate, the procedure ran once, but then encountered the error on the next loop, the same error in both environments.

    Changing the “ThisDocument” reference to ActiveDocument corrected the problem and all iterations of the For/Next loop worked.

    In reading your note I noticed you wanted to assign an Id to each paragraph. The following code will accomplish that.

    Sub ParagCount()

        Dim paraRange As Range

        Dim x As Integer

        For x = 0 To 9999

            Set paraRange = ActiveDocument.Paragraphs(1).Next(x).Range

            paraRange.Collapse wdCollapseStart

            paraRange.InsertAfter x + 1 & vbTab

        Next x

    End Sub


    Regards
    Thanks Rich for replying. I wanted to set the IDs for each paragraph of the document, not inserting the paragraph number 'x' before the text.
    Tuesday, April 12, 2011 9:05 AM
  • Hi Tony,

    Just to make it more clear, I am not seeing it on

    1. 32 Bit Office on 32 bit system
    2. 32 Bit office on 64 bit system

     

    But only on 64 bit office on a 64 bit system. On running the above mentioned code I am getting an error pop up saying "Out of Stack Space (Error 28)".

    Now, I am only trying to understand, what is wrong in this code which is causing this to throw this error. Why it is working fine on 32 bit office. I take it as some problem in the implementation of 64 Bit Word, paragraphs traversal.

     

    Thanks

    Uttam

    Tuesday, April 12, 2011 9:11 AM
  • Hi Tony,

    Yes I was running the code in the same document. That's probably why I received a response and it didn't just hang up Word.


    Regards
    Tuesday, April 12, 2011 12:00 PM
  •  I take it as some problem in the implementation of 64 Bit Word, paragraphs traversal.

    It looks like that :-(
     
    Can you write your code like this, instead?
     
        For Each Para in ActiveDocument.Paragraphs
            ' whatever
        Next
     

    Enjoy,
    Tony
    www.WordArticles.com
    Tuesday, April 12, 2011 12:36 PM
  • Hi Uttam,

    Have you resolved your problem yet and do the suggestions help you? If you still have any concern on the thread, feel free to follow up. 

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 20, 2011 9:36 AM
  • Hi Bruce, The problem is still not resolved. I still have not got any resolution or root cause analysis of the problem, I reported in my question and previous comments.

    Thanks

    Uttam

    Wednesday, April 20, 2011 9:47 AM
  • I do reproduce this same problem on my machine and it might be a problem with the 64-bit version of Word.  I can log this with the product group so they are aware of the behavior.  As a workaround though, can you do something like what Tony mentioned above?  Something similar to the following seems to work fine giving each paragraph an "ID" when I tried it.

    For Each myPara in ActiveDocument.Paragraphs

           ' do something

    Next myPara

     

    Brandon

    • Proposed as answer by Bruce Song Tuesday, April 26, 2011 1:56 AM
    Thursday, April 21, 2011 11:11 PM
    Moderator