none
Out of memory/Automation error when application and modeless userform lose focus RRS feed

  • Question

  • Hi,

    I have an add-in that provides a modeless userform that gives the user some options they can apply to text. In its UserForm_Initialize routine I have a line of code that places it relative to the active document:

    If Documents.Count > 0 Then
        Me.Left = ActiveWindow.Left + ActiveWindow.Width - Me.Width
    End if

    I also have some code in my DocumentChange event that unloads the userform (or userforms; it's possible and sensible to have more than one active) if it's open and then re-show it for the document that now has the focus.

    All of that generally works just fine. But I have one user who is getting an odd series of errors that I can't work out. When she switches away from Word, she first gets the message "Error 7 (Out of memory) on line 150 of procedure UserForm_Initialize of Form MyForm." (This is a formatted error message from the add-in that points to the above line of code that sets the form's .Left parameter.) I don't know of any events that fire when Word loses the focus entirely; if there is one and I should be doing some cleanup I'd like to. But I don't think it exists.

    She then gets an automation error related to re-showing the form in the DocumentChange routine. I'm trying to avoid getting into detail on that one because I think solving the first issue would solve this. I can provide more of an explanation if necessary.

    Can anyone shed any light on the out of memory error? Since I can't recreate her problem I can't test any solutions but perhaps if I better understood what would cause that I could find a fix.

    So, two main questions: is there an event I should be watching for when the application loses the focus? And is there a reason for an Out of memory error, other than a stack overflow (which I don't think is the case here)? I'm not adept at API calls, but if that is what I need to make this work I'll give it a whirl. Thanks in advance.

    Monday, December 22, 2014 7:13 PM

All replies

  • I don't have the answer for your question, but I have some points.

    My first guess is that you would get error because the object ActiveWindow would be set to Nothing, thus causing the error, but as you said the error is out of memory type, I don't know what it could be.

    If the position of the userform isn't absolutely important, you just could ignore the error doing something like:

    If Documents.Count > 0 Then
      On Error Resume Next
      Me.Left = ActiveWindow.Left + ActiveWindow.Width - Me.Width
      On Error GoTo 0
    End If

    ---

    The Word.Application object has two different events that you might be interested: WindowsActivate and WindowDeactivate. To trap these events, put a code like this one in you Document module:

    Public WithEvents moWord As Word.Application
    
    Private Sub Document_Open()
      Set moWord = Application
    End Sub
    
    Private Sub moWord_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
      On Error Resume Next
      MsgBox "Window of document " & Doc.Name & " activated. Name of the window: " & Wn.Caption
    End Sub
    
    Private Sub moWord_WindowDeactivate(ByVal Doc As Document, ByVal Wn As Window)
      On Error Resume Next
      MsgBox "Window of document " & Doc.Name & " deactivated. Name of the window: " & Wn.Caption
    End Sub


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br


    Tuesday, December 23, 2014 2:45 PM
  • Thank you for the response. I'll try each of those things and see if I can get a better result. (I probably won't know if any of them work until next month, but if they do I'll come back and mark this as the answer.)
    Tuesday, December 23, 2014 3:12 PM