I have vba code which opens a word document based on a template and when finished, runs the code below:
Public Sub Destroy(doc As Word.Document, app As Word.Application)
If Not (doc Is Nothing) Then doc.Close SaveChanges:=wdDoNotSaveChanges
If app.Documents.Count = 0 Then app.Quit wdDoNotSaveChanges
Set app = Nothing
End Sub
(this means that the app only closes if there are no other documents open and doesnt leave a blank application loaded when finished either)
I would like to extend this to excel and possibly other applications in the future; but rather than write a separate function for every different application, I wondered if I could have one function do-it-all.
The problem I'm finding is the declaration of arguments "doc As Word.Document" and "app As Word.Application".... is there a way of declaring what "doc" and "app" are in the calling program, and then getting the type definition of them inside my function to decide what to do depending on what type of application I choose to destroy()?
Edit:
I'm happy with the code, but on running a quick test in the code below, I founf that byref and byval both didnt effect the value of myval:
Private Sub Command12_Click()
Dim myval As Integer
myval = 1
MsgBox "the value of myval is " & myval
doByVal (myval)
MsgBox "the value of myval is " & myval
doByRef (myval)
MsgBox "the value of myval is " & myval
End Sub
Private Sub doByVal(ByVal a As Integer)
a = a + 1
MsgBox "byVal gives " & a
End Sub
Private Sub doByRef(ByRef a As Integer)
a = a + 1
MsgBox "byRef gives " & a
End Sub