Results 1 to 11 of 11
  1. #1
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Which Sub called my Sub? (Win98 NL / Word97 NL)

    I have a number of generic Subs for common tasks that can be called from a large number of other Sub's. One of them is Sub ProtOff, that lifts the doc protection. I encounter a problem with opening a doc by double-clicking on it in Explorer, while Word is not active.
    Word97 starts, no doc is visible yet and then Sub ProtOff fires, detects that there is no doc open and displays a messagebox.
    I wonder which piece of code is calling Sub ProtOff. Is it possible to identify the calling Sub, using VBA code?

  2. #2
    4 Star Lounger
    Join Date
    Dec 2000
    Location
    Quakertown, PA, Pennsylvania, USA
    Posts
    517
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    If it's attempting to fire before a document is even open then it MUST be in the Document_New of the template and/or Document_Open event of the template or document.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL

    I've always heard that VBA does not provide any visibility into the call stack. I think there are add-ons that can display the call stack while debugging, but frankly it's a very fuzzy recollection.

  4. #4
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    Thanks Dreamboat and Jefferson.

    I had another chat with the user who reported this. It turns out that he wasn't using his Win98/Office97 PC (98PC), but was testing on a PC with Windows XP and Ofice 2003 (both Dutch versions)(XPPC). So I took the doc he was testing with and opened it by double-clicking in Explorer, both on a 98PC and a XPPC. There is a difference in behaviour. On a 98PC8, the doc is opened without any problems. On a XPPC the error in Sub ProtOff occurs, with no doc visible yet.
    The doc, nor the template that it's based on, contain any VBA code. All code is sitting in a global template in the Word startup folder. All start-up code is triggered by application events. In this case wdApp_DocumentChange in the Word97 version and wdApp_DocumentOpen in the Word2003 version. In both cases the same Sub DocumentStart is called immediately (after testing that the opened doc is really a doc and not a template). Towards the end of the (long) Sub DocumentStart there's a call to a Sub that needs to lift protection, so there is where Sub ProtOff is called.
    Apparently, there is a difference between Word97 and Word 2003 for the moment that the doc is actually "there", going through the same sequence of code. I wonder why and what I can do, if anything, to make the doc visible earlier in the process. As there is no document yet, anything with ActiveDocument obviously isn't going to work.
    Of course there is a work-around by adding, at the beginning of Sub ProtOff, the line
    If Documents.Count < 1 Then Exit Sub

    But of course this is not very satisfiying. Ideas anyone?

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    In Excel I would change the direct call to ProtOff to a call that uses the OnTime method. Using this method ensures all files are loaded (and their respective event code fired) before the code scheduled by the OnTime method is run. Unfortunately Word can only schedule one OnTime event, which is always the last one that has been scheduled (already scheduled Ontime events get "overwritten"), but it might just solve your problem.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    I doubt that this has anything to do with the Word version.

    More likely due to different add-ins/apps that play with Word.
    These could affect timings of when the various critters get invoked.

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    In my app_DocumentChange event I always check for Application.Documents.Count > 0 before doing anything with the ActiveDocument.

    StuartR

  8. #8
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    Thanks for the suggestion. I already use OnTime, so I'm planning to migrate some code to the sub called by OnTime. Because a certain sequence of activities is needed, unfortunately this takes a lot of re-thinking and re-designing I'm afraid...

  9. #9
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    <hr>I doubt that this has anything to do with the Word version.<hr>
    Thanks Howard.
    But yes, there is a difference between Word versions. In Word97 the only (useable) application event is wdApp_DocumentChange. So I used that and by keeping track of the number of docs in a global variable, I could determine whether the event was triggered by creating / opening a doc (.Count = var + 1), closing a doc (.Count = var - 1) or a change between docs (.Count = var). This worked fine for 4 years. Word97 shows the doc immediately after creating / opening, before the start-up code is finished.

    Now we are in the final stage of preparing an update from Win98 / Office97 to WinXP / Office 2003 (both Dutch). And in the process I found that wdApp_DocumentChange behaves strangely. Sometimes the event-procedure is looped throught many times (up to 150 I recall) before the code is finished. And creating a new doc when no other doc is loaded ("empty" Word app) causes the event-procedure to go into an infinite loop. (I reported this before, see my <post#=360433>post 360433</post#> .)
    That's why I reverted to the wdApp_NewDocument and wdApp_DocumentOpen events in Word2003. They work fine, except for the fact that the doc is not shown until the start-up code in the event-procedure is finished, causing the described problems.

  10. #10
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    That's what I do too, Stuart. But in Word2003 I'm not using that anymore. See my reply to Howard in this thread.

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Which Sub called my Sub? (Win98 NL / Word97 NL)

    I would expect that you need to redesign your code.

    One of the big changes from Word 97 to the Word 2000, etc.was the SDI/MDI issue.
    This can cause grief with macro design.

    See the SDI example in http://www.standards.com/OhMyWord/VBACodeSnippets.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •