excel vba - VBA Declaring arguments outside a function -
excel vba - VBA Declaring arguments outside a function -
i have vba code opens word document based on template , when finished, runs code below:
public sub destroy(doc word.document, app word.application) if not (doc nothing) doc.close savechanges:=wddonotsavechanges if app.documents.count = 0 app.quit wddonotsavechanges set app = nil end sub (this means app closes if there no other documents open , doesnt leave blank application loaded when finished either)
i extend excel , perchance other applications in future; rather write separate function every different application, wondered if have 1 function do-it-all.
the problem i'm finding declaration of arguments "doc word.document" , "app word.application".... there way of declaring "doc" , "app" in calling program, , getting type definition of them within function decide depending on type of application take destroy()?
edit:
i'm happy code, on running quick test in code below, founf byref , byval both didnt effect value of myval:
private sub command12_click() dim myval integer myval = 1 msgbox "the value of myval " & myval dobyval (myval) msgbox "the value of myval " & myval dobyref (myval) msgbox "the value of myval " & myval end sub private sub dobyval(byval integer) = + 1 msgbox "byval gives " & end sub private sub dobyref(byref integer) = + 1 msgbox "byref gives " & end sub
sure. may declare generic objects args of function, , verify actual type in function. basically, code frame like:
public sub destroy(byval doc object, byval app object) if (typeof doc word.document) , (typeof app word.application) ' word related stuff elseif (typeof doc excel.workbook) , (typeof app excel.application) ' excel related stuff ' ... else ' mixed cases, unhandled types etc. end if end sub and here's passing args destroy function, (a bit exaggerated) example:
dim my_doc excel.workbooks dim my_app excel.application set my_app = excel.application set my_doc = my_app.workbooks("iwantyouclosed.xlsx") phone call destroy(my_doc, my_app) vba excel-vba access-vba word-vba
Comments
Post a Comment