Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to increase stack-space?

Status
Not open for further replies.

nesplb

Programmer
Jul 29, 2003
109
NO
Hi!
I have a heavy macro that calls it self several times, does different loops etc. But when the text is large I get the message "out of stack space". Is there a way to avoid this? Perhaps a way to increase the stack space?
thanx in advance!

Pål Nesteby

PDC-Tangen
Norway
 
I would've thought this is to do with non cleared variables cluttering up memory

1st step - make sure that all object variables are set to nothing when you are done with them:

Code:
set myRange = range("A1")

'do stuff

set myRange = nothing

Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
The problem is that the recursion level becomes to deep.
Can you please post the code to help us helping you ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank you Geoff!
I'm testing your suggestion now...Hope it works!

To PHV:
Here is the code, it may look a bit messy but I hope you understand the meaning of it...I will be ver thankful for any tips!
The code is supposed to look for some stylesets that have a startstyle and an endstyle. The startstyle ends with "Start" and the endstyle ends with "Slutt". (Slutt = End in Norwegian). Within each styleset there may be another styleset that is built the same way. And in this styleset there may also be another similar styleset. Like this:

Style1 Start
(some text)
style2 Start
(some text)
Style 3 Start
(some text)
Style 3 Slutt
(some text)
Style 2 Slutt
(some text)
Style 1 Slutt

I the code I want to change the stylenames of the styles that is inside of another styleset so that the result will be like this:

Style1 Start
(some text)
style2 Start Niv2
(some text)
Style 3 Start Niv 3
(some text)
Style 3 Slutt Niv 3
(some text)
Style 2 Slutt Niv 2
(some text)
Style 1 Slutt

Niv means level in my code! (Nivaa is the same as level in Norvegian)
I'm doing this because I want to know if a styleset is on a top level, 2nd level or 3rd level.
You see?

And here's the code:
Code:
Sub startCheckElement()
checkElement(1)
End Sub

Function checkElement(ByVal counter As Long)
Dim p As Paragraph

Set p = ActiveDocument.Paragraphs(counter)
    'if you come to an Start
    If Right(p.Style, 5) = "Start" Then
        checkInner (counter + 1)
    End If
counter = counter + 1
'Checks that we don't run into an eternal loop
If counter >= ActiveDocument.Paragraphs.Count Then
    Exit Function
End If

'the function calls itself with counter as arg.
Set p = Nothing
checkElement(counter)
End Function


Function checkInner(ByVal counter As Long)
Dim p As Paragraph

Set p = ActiveDocument.Paragraphs(counter)

If Right(p.Style, 5) = "Start" Then
    insideAStyleSet = True
    checkInner2 (counter + 1)

    On Error GoTo feil
    'making the new style
    Dim styleName2
    styleName2 = p.Style + " Niv2"

    makeStyle styleName2, p.Style
feil:
    'setting the style
    setStyle styleName2, counter
End If

If Right(p.Style, 5) = "Slutt" Then
    If insideAStyleSet = False Then
        Exit Function
    Else
        On Error GoTo feil2
        'Making new style
        styleName2 = p.Style + " Niv2"
        makeStyle styleName2, p.Style
feil2:
        'setting the style
        setStyle styleName2, counter
        insideAStyleSet = False
        'checking for more stylesets         
        Set p = Nothing
        checkInner (counter + 1)
    End If
End If

counter = counter + 1
Set p = Nothing
checkInner (counter)

End Function

Function makeStyle(ByVal styleName As String, ByVal s As String)

       With ActiveDocument
            .Styles.Add Name:=styleName, Type:=wdStyleTypeParagraph
            .Styles(styleName).BaseStyle = s
    End With
End Function


Function setStyle(ByVal name As String, ByVal counter As Long)
Dim p As Paragraph
Set p = ActiveDocument.Paragraphs(counter)
p.Style = ActiveDocument.Styles(name)
End Function



Function checkInner2(ByVal counter As Long)
Dim p As Paragraph

Set p = ActiveDocument.Paragraphs(counter)

If Right(p.Style, 5) = "Start" Then
    insideAStyleSet2 = True

    On Error GoTo feil
    'Making new style
    Dim styleName2
    styleName2 = p.Style + " Niv3"

    makeStyle styleName2, p.Style
feil:
    'setting the style
    setStyle styleName2, counter
End If
If Right(p.Style, 5) = "Slutt" Then
    If insideAStyleSet2 = False Then
        Exit Function
    Else
        On Error GoTo feil2
        'making a new style
        styleName2 = p.Style + " Niv3"
        makeStyle styleName2, p.Style
feil2:
        'setting the style
        setStyle styleName2, counter
        insideAStyleSet2 = False
        'checking for more stylesets
        Set p = Nothing
        checkInner2 (counter + 1)
        Exit Function
    End If
End If

counter = counter + 1
Set p = Nothing
checkInner (counter)

End Function





Pål Nesteby

PDC-Tangen
Norway
 
You may change your Functions to Subs as you don't care return value.
Just a though: have you tried to code a single loop (For Each p in ActiveDocument.Paragraphs) maintening yourself an level array ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for the tip!
I started this project with an for each statement but I wasn't able to nest these the right way...I don't know why. I thought this was the easiest way but....

Pål Nesteby

PDC-Tangen
Norway
 
Just for information...
I found the error in the code...
instead of making the functions call themselves I just used the GoTo function and sat a mark at the top of the function.
This prevents the recursion level from becoming too deep.
:)
for more info see:

Pål Nesteby

PDC-Tangen
Norway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top