Class properties or function?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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...
.



Relevant Pages

  • Re: Class properties or function?
    ... alive long enough to work with them in VBA that would be great. ... You are assigning a variable named formTag with the value of your form's ... VBA is based on the variable tempTag. ... Public Property Let myTag ...
    (microsoft.public.vb.general.discussion)
  • Re: Passing class as an object to ActiveX dll
    ... In a VBA class module, ... Public Property Let ShowSQLX ... In a class called clsSQLite in the ActiveX dll: ... obviously there is no intellisense and I need to know ...
    (microsoft.public.vb.general.discussion)
  • Re: Passing class as an object to ActiveX dll
    ... In the VBA class I for example this: ... Public Property Let StartTimeX(ByVal lStartTimeX As Long) ... So this passes back to VBA variables that were altered in the ActiveX dll. ...
    (microsoft.public.vb.general.discussion)
  • Re: Passing class as an object to ActiveX dll
    ... Remember the class to get intelli-sense on is in VBA. ... Public Property Let ShowSQLX ... obviously there is no intellisense and I need to know ... Normally we get this information by including a Reference to a component. ...
    (microsoft.public.vb.general.discussion)
  • Passing class as an object to ActiveX dll
    ... When you pass a class (defined in VBA) as an object to an ActiveX dll is it possible to ... Public Property Let ShowSQLX ... Sub Test() ...
    (microsoft.public.vb.general.discussion)