简体   繁体   中英

Document_open macro in Word isn't triggered until VBA-code is changed (insignificantly)

I have a .docm file with a Document_Open-sub, which calls another sub to open a small userform - or it's supposed to trigger and open - but it doesn't...

If I make changes to the document and save it - it still doesn't work.

If I make in significant changes to the VBA-code in the same document (like adding a space character outside a sub) and save it - it suddenly works.

Why does it not work all the time? - and what can I change to make it work?

Additional info (I have no idea if some of it might be relevant)

  • In most cases the document is downloaded from an internal website and saved locally.
  • The document we've tested with is saved locally in a location which is trusted in Word.
  • In cases - where the sub isn't triggered automatically - the sub can be run with ALT+F8 (but that's not what I want).
  • The functionality works for some user - and not for others.
  • The functionality in the Document_Open-sub depends on a value read in CustomDocumentProperties.

System: Win10 - Word MSO365 build 2110

Edit - code added:

Public Sub Subrutine1()
    ThisDocument.CustomDocumentProperties("prop1").Value = " "      'resetting prop1
    UserForm1.Show  'prop1 is set from the userform
End Sub


Private Sub Document_Open()
    
    If ThisDocument.CustomDocumentProperties("prop1").Value = " " Then
        Call Subrutine1
    End If
End Sub

You could rename your Sub "Subrutine1" to "AutoOpen."

I would recommend the following code.

Private Sub AutoOpen()
    Dim myForm As UserForm1
    Set myForm = New UserForm1
    myForm.Show
    Unload myForm
    Set myForm = Nothing
End Sub
  • I would suggest resetting the document property as part of your userform initialization unless you sometimes use it without resetting that.
  • I would suggest using a document variable rather than a document property.
  • I suspect you would be better served with a document template rather than a document. In that case, the macro would be named "AutoNew."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM