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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT-How do I kill Auto Open in Word vb 2

Status
Not open for further replies.

pluz

Instructor
Jan 26, 2004
20
0
0
US
I have a document that has an auto open macro in it that pulls up a form. That part works great. My Problem is when the document is saved as a new document, the auto open macro is saved with and runs in the new document -- I don't want that to happen. How do I disable or delete the auto open macro after the form has been filled out? I can add code to remove the form itself, but can't for the life of me figure out how to remove the Document_Open macro from Microsoft Word Objects, "ThisDocument". Please help!
 
Pluz:

I can't remember how to reference the VBA code but I have come across it somewhere. You can rename the VBE Project to something specific and find a reference to it in the browser.

Otherwise, you could add a switch to the auto_open routine. Place an exit_if at the begining of your auto_open code and check the switch. At the end of your auto_open code or in your save or close methods you can flip the switch and prevent the auto_open code from running the next time the file is opened.

Ron
 
Hi pluz,

Code is not the easiest of objects to work with, but this should do it:

Code:
Dim StartLine As Long
Dim EndLine As Long

With ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule

    StartLine = CountOfDeclarationLines + 1
    If .Find("Document_Open", StartLine, 1, -1, -1, True) Then
        EndLine = StartLine
        .Find "End Sub", EndLine, 1, -1, -1, True
        .DeleteLines StartLine, EndLine - StartLine + 1
    End If
    
End With

Enjoy,
Tony

------------------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading FAQ222-2244 before you ask a question.
 
The above probably won't work if you're using Office XP because by default you are denied access to the VBProject object.

Have you tried turning your original document into a template? If you use this function:

Code:
Private Sub Document_New()
    
    ' Detach document from this template
    ActiveDocument.AttachedTemplate = ""
    
End Sub

it should break the link between your new document and the template so you shouldn't end up with any of its modules.

Nelviticus
 
Fantastic! Thanks, Tony. This worked great, thank you!! Ron, Nelviticus - thanks for your input also.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top