PDA

View Full Version : Instruct Macro to delete Userform



DavG63
12-11-2015, 03:14 AM
Hi

This is probably really easy to achieve, but all the examples I have been able to find seem to relate to Excel rather than Word.
Question: Is there a way to stipulate at the end of a macro that the Userform, which has just been used to input data onto the document, should delete itself?

At the moment, I have the following which pulls up the Userform when the document is opened:



Private Sub Document_Open()UserForm1.Show
End Sub



Ideally what I'd like is to have basically a 'one-shot' Userform which once the document has been created, the Userform deletes itself so that on subsequent occasions when the document is opened it behaves like an ordinary Word document.

Any ideas?

Thanks very much

Dav

DavG63
12-11-2015, 04:13 AM
I was able to find a solution which I thought I'd share:-


Sub AutoOpen()
Dim f As Boolean
On Error GoTo ErrHandler
f = ActiveDocument.Variables("HasRun")
If f = True Then
Exit Sub
End If


ContinueHere:
ActiveDocument.Variables("HasRun") = True
UserForm1.Show
Exit Sub


ErrHandler:
ActiveDocument.Variables.Add "HasRun", True
Resume ContinueHere
End Sub