Class properties or function?
- From: Ken K <KenK@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 16 May 2006 04:37:01 -0700
Hi all
I've got a COM add in for Word and I'd like to use VBA macros to process
information gathered by a form in the addin. The form collects information,
creates a couple of arrays and some additional variables that are used to
fill in information in a document template. I can of course do that entirely
within the DLL, but I'd like to expose the arrays and variables to VBA so
that users (with appropriate training) can use the information in their own
macros. (Working in entirely VBA is not an option.)
I've created property get/let pairs and UDTs in the DLL and can get that
information in VBA if I assign fixed values to the variables/properties. But
when I try to get the information generated by the form across into VBA I end
up with empty variables/properties.
Now I'm wondering if I should get the information into VBA by using a
function instead - the Word VBA macro would call a function in the DLL that
would open the form, collect the information, create the arrays and then
return them as an array of arrays to be used in the macro. That just seems a
bit clunky to me and there must be a better way. (I'm learning on the job -
no structured training to fall back on so there's major holes in my
understanding of what I'm doing.)
Here's an example of what I've got.
In a class in my DLL I've got the following get/let to store the value of
the DLL form's tag (eg OK or Cancel):
Public Property Get myTag() As Variant
myTag = tempTag
End Property
Public Property Let myTag(ByVal strReturnValue As Variant)
tempTag = strReturnValue
End Property
This works fine until I try to call that information from VBA:
str = objMyClass.myTag
results in myTag being empty.
There's a lot more I'd like to do than just get the form's tag but I reckon
if I can get my head around how to do that then I can figure out how to do
the rest. Am I even thinking about this in the right way? Am I missing
something basic like using a Set property as well - if so, how should I do
that?
Here's a code sample that shows what I'm trying to do in context (there's a
valid reference to the addin in the Word template):
<<in the ThisDocument module of the Word template>>
Option Explicit
Public Property Get myClass() As clsMyClass
Set myClass = CreateObject("MyAddIn.clsMyClass")
End Property
Public Sub Document_New()
On Error GoTo errorhandler
myClass.DLLFormShow
If myClass.myTag = "Cancel" Then 'myTag results in "empty"
ThisDocument.Saved = True
ThisDocument.Close wdDoNotSaveChanges
Exit Sub
Else
'do stuff
End If
Exit Sub
errorhandler:
ThisDocument.Saved = True
ThisDocument.Close wdDoNotSaveChanges
End Sub
<<in clsMyClass in MyAddIn DLL>>
Option Explicit
Private tempTag As Variant
'using Variant because MS KB articles said that
'variants are the only way to expose arrays
'although in this instance I suppose a string would
'be fine?
Public Property Get myTag() As Variant
myTag = tempTag
End Property
Public Property Let myTag(ByVal strReturnValue As Variant)
tempTag = strReturnValue
End Property
Public Sub DLLFormShow()
On Error GoTo errorhandler
Dim frmForm As Form
Set frmForm = New frmMyForm
frmForm.Show 1
formTag = frmForm.Tag
Unload frmForm
Set frmForm = Nothing
'at this point formTag = OK or Cancel as appropriate
Exit Sub
errorhandler:
formTag = "Cancel"
If frmForm Is Nothing Then
Else
Unload frmForm
Set frmForm = Nothing
End If
End Sub
<<the form's OK and Cancel buttons each set the form's tag to OK or Cancel
as appropriate>>
Thanks for any help...
.
- Follow-Ups:
- Re: Class properties or function?
- From: R. MacDonald
- Re: Class properties or function?
- Prev by Date: Re: copymemory help
- Next by Date: Re: query problem
- Previous by thread: VB-Process does not terminate after HTTP POST
- Next by thread: Re: Class properties or function?
- Index(es):
Relevant Pages
|