Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

_ComObject to type Integer

Status
Not open for further replies.

obfuscate

Programmer
Feb 8, 2010
29
I am using the following code, which works fine in VBA but not in VB...grrrrr

ClaimLines needs to be a number since mathmatical fucntions are performed based off its value or values < or > ClaimLines. I could stand to have it grabbed from extra as another data type as long as i can then convert that in to a true number
Code:
Dim ClaimLines as Integer
ClaimLines = Sess0.Screen.Area(21, 8, 21, 9).Value
Say the number in that area of the screen is 32, well, the Excel VBA macro can get it and have it stored directly to ClaimLines (which is Dim'med as Integer). When I use a VB version of this macro I get the following error:

"Conversion from type '_ComObject' to type 'Integer' is not valid"

The Exception text is below:
Code:
************** Exception Text **************
System.InvalidCastException: Conversion from type '_ComObject' to type 'Integer' is not valid.
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value)
   at HCFMacro.HCFMainForm.RunMacrobutton_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///c:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
HCF Central Macro - for net 2
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Documents%20and%20Settings/ab94883/Desktop/HCF%20Central%20Macro%20v2.01.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 8.0.0.0
    Win32 Version: 8.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------

I guess what im most confused about is why in Excel VBA it works fine but then in VB I have issues....once again, i can use and work in VBA at work but anything VB has to be done away from a system i can test on...any help would be greatly appreciated
 



Hi,

Because ALL the stuff you scrape from a screen is TEXT -- every bit of it.

You must TEST and CONVERT.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip, I understand that....but can you provide an answer as to why in VBA this works fine but then in VB as a standalone app it screws up. The only thing i can think of is that in Excel VBA it doesnt care that your setting a variable you dimmed as an Integer to a text value (and then will let you perform calculations with it), unless there is somthing different about the way excel handles grabbing the data

like i said earlier, i cant code in VB and test the VB version til tomorrow but im thinking something along the lines of the following, surely someone has done this in VB before (PLEASE TAKE NOTE THAT THE CODE IN THE FIRST POST WORKS FINE IN VBA AND DOES NOT CAUSE A CONFLICT, ONLY A CONFLICT IS CAUSED IN THE VB STANDALONE VERSION)
Code:
Dim ClaimLines as Integer
Dim ClaimLinesString as String

ClaimLinesString = Sess0.Screen.Area(21, 8, 21, 9).Value
ClaimLines = Val(ClaimLinesString)

FYI, the number that will end up being assinged to ClaimLines will always be between 1-99 and will always be a whole number
 



It relates to the EDITOR/COMPILER that you're working in.

I would NEVER assume that the editor or compiler will handle my coding sloppiness, although, I'm sure that there ARE some things I overlook. I try not to let those thing occur.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 


BTW, it used to be that

1) you would code your program on a code sheet.

2) you would very carefully check your code for proper syntax and logic flow

3) you would punch a program deck and submit at the control desk

4) several hours later, you would get a printout. Either it had nasty error messages or an output, that may or may not be what you were expecting.

So don't complain about what your VB compiler will not do for you.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
skip,

"It relates to the EDITOR/COMPILER that you're working in."
Great.....what about them is causing this error

you cant give me a specific reason as to why Excel VBA handles it fine but VB standalone doesnt.....all you do is call me sloppy and tell me how 'old-school' you are

you didnt even bother responding to if the
Code:
ClaimLines =val(ClaimLinesString)
is correct and will work.....why does it seems like you like pointing out my 'oh so obvious' errors instead of actually answering my questions
 

Code:
If IsNumeric(Trim(Sess0.Screen.Area(21, 8, 21, 9).Value)) then
  ClaimLines = CInt(Trim(Sess0.Screen.Area(21, 8, 21, 9).Value))
Else
  'whatever you want this condition to do.
End if

Skip,
[sub]
[glasses]Just traded in my old subtlety...
[b]for a NUANCE![/b][tongue][/sub]
 

Here's some actual code I use on a regular basis. In this case, since I am writing data to Excel, even if a field defined as CHAR contains numeric data, I prefix it with an APOSTROPHY, to convert it to a STRING.
Code:
    Select Case arySpecs(TP, i)
        Case "CHAR"
            If IsNumeric(Trim(Mid(sIn, (iFR + nRow - 1) * 80 + iCol, iLen))) Then
               GetField = "'" & Trim(Mid(sIn, (iFR + nRow - 1) * 80 + iCol, iLen))
            Else
               GetField = Trim(Mid(sIn, (iFR + nRow - 1) * 80 + iCol, iLen))
            End If
        Case "NUM"
            GetField = Trim(Mid(sIn, (iFR + nRow - 1) * 80 + iCol, iLen))
    End Select
I actually have a table of data that defines the fields on each of my IMS screens. But this shows how I routinely test for numeric values.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
thanks for the tips skip

it appears though that it all goes back to a syntax error in my VB code vs my VBA code. Apparently i didnt end the claimLines = statement with .value in the VB version (cant believe i misssed that)

it works fine now with .value and claimLines dimmed as Int but i figured it is best practice to trap the variable first as a string with claimLinesString and then convert to NUM by using claimLines= Val(claimLinesString)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top