开发者

Error returning objects to VBA from Visual Foxpro COM server

开发者 https://www.devze.com 2023-04-12 19:40 出处:网络
I am getting the following message when trying to return a new object to VBA from my Visual Foxpro COM server.

I am getting the following message when trying to return a new object to VBA from my Visual Foxpro COM server. "Run开发者_StackOverflow-time error '-2147417851 (80010105)':

Method 'ReturnObject' of object 'Itestclass' failed"

If I remove the "Dim ... As" line the error goes away but then I lose intellisense for the COM object.

This is the VBA code:

Sub Test()

'' Removing the following line gets rid of the error but loses intellisense for the COM object
Dim objTest As testcom.TestClass

Set objTest = CreateObject("TestCOM.TestClass")
Set objNew = objTest.ReturnObject   '' This is the line that causes the error

End Sub

I have created a link to the TestCOM type library in Tools > References

Here is the Visual Foxpro (VFP) code: The COM server is being built as an out of process EXE. If I build it as an inprocess .DLL then the VBA code causes Excel to crash.

DEFINE CLASS ObjectToReturn AS SESSION OLEPUBLIC

ENDDEFINE

DEFINE CLASS TestClass AS SESSION OLEPUBLIC

FUNCTION ReturnObject

    RETURN CREATEOBJECT("ObjectToReturn")

ENDFUNC

ENDDEFINE

I have tried changing the RETURN CREATEOBJECT("ObjectToReturn") to RETURN CREATEOBJECT("CUSTOM") but the problem persists.

Please advise how I can get rid of this error without losing the intellisense for the COM object in VBA. Thanks


I don't know why you are going through such difficulties... This should be able to help you... You can define your class as OlePublic and set some properties on it like the samples at the top. You can set these properties anywhere through the other functions that are not HIDDEN.

If you need to are trying to get at certain elements OF some other "object", try creating an instance of the object and stick it into a property on the OlePublic class... see my method

DoSomethingElse

which does a simple scatter NAME call to the "SomeObject" property of the class. Even though you are not explicitly returning it, it should be visible from within your creation of it from VB...

DEFINE CLASS VFPClassForVB as Session  OLEPublic
  cTmpFiles  = ""
  cCOMUser  = ""
  SomeObject  = ""

  FUNCTION Init()
    */ Who is user...  always ignore the machine....
    This.cCOMUser  = SUBSTR( SYS(0), AT( "#", SYS(0)) +1 )
    This.cTmpFiles  = "somepath\"

    */ Unattended mode... any "MODAL" type dialog will throw error / exception
    SYS(2335, 0 )

    */ ALWAYS HAVE EXCLUSIVE OFF FOR COM!!!
    SET EXCLUSIVE OFF

    */ ALWAYS HIDE DELETED RECORDS!!!
    SET DELETED ON
  ENDFUNC 

  */ Error handler at the CLASS level will always be invoked
  */ instead of explicit ON ERROR or TRY/CATCH handlers...
  FUNCTION xError(nError, cMethod, nLine)
    lcMsg  = "User: " + SYS(0) + "  Tmp:" + SYS(2023);
        + "  Method: " + cMethod + "  Error: " + STR( nError,5);
        + "  Line: " + STR( nLine, 6 )
    STRTOFILE( lcMsg, This.cTmpFiles + "COMLog.txt" )

    */ NOW, throw the COM Error...
    COMReturnError( cMethod + '  Error:' + str(nError,5);
        + '  Line:' + str(nline,6);
        + '  Msg:' + message(), _VFP.ServerName )
  RETURN 


  HIDDEN FUNCTION SomeOtherFunction( lcWhat String,;
                     lnThing as Integer ) as String

    */ Do something
    RETURN 1
  ENDFUNC 

  */ Another completely visible function direct form VB
  FUNCTION DoSomethingElse( SomeParameter as String ) as String
    USE SomeTable
    */ Now, this object should be visible as a direct property in VB
    SCATTER MEMO NAME This.SomeObject
  ENDFUNC 

ENDDEFINE 

Your VB side, even from your sample...

Sub Test()
Set objTest = CreateObject("MySampleProject.VFPClassForVB")
objTest.DoSomethingElse( "I dont care" )
dim Something as objTest.SomeObject.ColumnFromTable  
End Sub

You can create as many OlePublic classes in your in-code class libraries that you want to expose and just create those instances as needed. Let me know if this helps you get closer and we'll try to keep working it out.

I've tried all sorts of samples, but looking at what you have of the object where both are VFP OleObject entries, each is exposed, and can be created individually. You don't need to create one to create the other.

Is there some reason SPECIFIC you are trying to create one object from another? You can have one object expose a bunch of methods and properties to perform whatever you need from VFP.

If you want to have multiple object classes exposed, and under a central control, you can always create your primary object for communication and have IT create an instance of each "other" class on it. Then, expose methods on your main class to handle the communications between them to act out whatever it is you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号