views:

822

answers:

2

Hi,

I should convert a huge load of code which was written in VBA (Excel) to VB6. But I really do not know what I have to take care of or where to begin. Therefore it would be great to get some hints here from the VB6 experts.

I installed already MS Visual Studio and played a bit around. But I'm not a VB6 expert and do not really know what I have to do.

The final goal is to have all the VBA code, which is currently placed in one excel vba macro into a VB6 project and create a .dll out of it. This .dll should be referenced by the excel and the excel should run like it does now :-)

For example what do I have to do to convert this vba code to VB6.

Public Function getParameterNumberOfMaterial() As Integer
10        On Error Resume Next
          Dim a As String
20        a = Sheets("Parameters").name

30        If IsNumeric(Application.Worksheets(a).range("C3").Value) Then
40            If Application.Worksheets(a).range("C3").Value > 0 Then

50                getParameterNumberOfMaterial = Application.Worksheets(a).range("C3").Value
60            Else
70                MsgBox "Please check cell C3 in the sheet 'Parameters'. It should include a numeric value which is greater than zero"
80                MsgBox "Parameter Number of Material/Cost is set to the default value of 10"
90                getParameterNumberOfMaterial = 10
100           End If
110       Else
120           MsgBox "Please check cell C3 in the sheet 'Parameters'. It should include a numeric value which is greater than zero"
130           MsgBox "Parameter Number of Material/Cost is set to the default value of 10"
140           getParameterNumberOfMaterial = 10
150       End If
160       On Error GoTo 0
End Function

Thanks for your help!

Edit: Yes and if it is possible to convert vba code into a .dll this would be fine also. Then I would not have to convert the code. But I think it is only possible to create a .dll out of vb6 code.

A: 

@Tom Tom

You should have no trouble in converting the code from .VBA to vb6.as a matter of fact you virtually don't have to.

The problem is in VB6 context, the langugae cannot understand what

"Application.Worksheets(a).range("C3").Value)" means,

the object Application has a different meaning in VB6

VBA (the VBA version you have) is almost a customized implementation of VB6 in Excel(or word , or anything that comes with MSO).

It would be a bad Idea to try to access Excel UI from VB6 (I'm not even sure if its possible )

what you should do is seperate the bussiness logic from the Code then make it into a VB6 library.

For example your code (as I understand) returns the value of the cell C3

It is very much tied to the Excel UI, so it would be very counter productive if not impossible to convert you code to VB6.

because even if you convert the code, most of the variables like (Application.Worksheets(a).range("C3").Value) must be called from VBA, which is pointless

however if you have any other pure Bussiness logic, that can be ported (its funny because theres really nothing to port) easily

Vivek Bernard
Tom Tom
Vivek Bernard
Yes I do understand what you mean. But they also state that :)"Also, note that we did not use anything in Excel's object model in the above example. Most of your code will doubtless use many objects within Excel's and Office's libraries. Converting your VBA to VB code is usually as simple as adding a reference to Excel and then fully qualifying your objects. You will need to distribute the lib and exp files along with your dll as well."
Tom Tom
Most of the code is actually tightly bound to excel. Nevertheless it would be great to create a .dll out of it, since the update process would be much more easy. Imagine on the one hand I have to gather all excel workbooks out there and on the other hand I just update the referenced .dll and all works fine.
Tom Tom
Hello Tom Tom Just realized, it is viable, please go ahead,and create a VB6 class library, just tried it with VB.Net it worked...Serves me right, :)
Vivek Bernard
If u need help creating i'll try to help u, I left vb6 ages ago..
Vivek Bernard
Hi Vivek, would be great if you could help me. Actually if this serves my purposes I could use VB.net also. The only thing is that I never used VB.net before. More a JAVA and excel automation with vba guy ;-) It would help me a lot if you would post your VB.Net code which you were talking about in a new answer + add some comments which could help me to implement it by myself. Thanks a lot.
Tom Tom
+1  A: 

@Tom

Ok, I actually learning this with you, so here goes,

VB.Net code (I am using .net 2.0)


In Visual Studio 2005 a new Class Library Project is opned Then remove all the garbage alreadt written there and paste the code

'First thing to do is add a reference the Excel Runtime

Imports Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices


Namespace ExcelExample

' the following is an Attribute spcifying that the class can be accesses in a unmanaged (non-.net way)

Imports Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices


 Public Class ExcelVB


    Public Function getParameterNumberOfMaterial() As Integer
        On Error Resume Next
        Dim a As String
        Dim appInst As New Microsoft.Office.Interop.Excel.Application
        a = appInst.Sheets("Parameters").name

        If IsNumeric(appInst.Worksheets(a).range("C3").Value) Then
            If appInst.Worksheets(a).range("C3").Value > 0 Then

                getParameterNumberOfMaterial = appInst.Worksheets(a).range("C3").Value
            Else
                MsgBox("Please check cell C3 in the sheet 'Parameters'. It should include a numeric value which is greater than zero")
                MsgBox("Parameter Number of Material/Cost is set to the default value of 10")
                getParameterNumberOfMaterial = 10
            End If
        Else
            MsgBox("Please check cell C3 in the sheet 'Parameters'. It should include a numeric value which is greater than zero")
            MsgBox("Parameter Number of Material/Cost is set to the default value of 10")
            getParameterNumberOfMaterial = 10
        End If
        On Error GoTo 0
    End Function
End Class

End Namespace



Build the solution by pressing F6 go to Project->Project Proerties and Check Register for COm interop

SO the out put is a .DLL and .tlb , the Excel file should reference the .tlb file,

you have to register the DLL by regasm /codebase c:\Excel\dllname.dll

Then you can access the Function from Excel.

Heres a link to my project folder unrar it, and you'll find a A excel workbook that ontains a referecnce to the .dll via the .tlb

http://cid-4af152a1af4d7db8.skydrive.live.com/self.aspx/Documents/Debug.rar

Heres another great article

http://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/

Vivek Bernard
Awesome!!!!!!!!!!!!!! Vivek, thank you soo much!!!!! I just can say: WOW. Can I pay you a beer or something? ;-)I hope you can need this knowledge by yourself. I will give it a try tomorrow.
Tom Tom
I would love to have a beer, but its illegal for me to drink...thank you actually, you made me poke around a lot..
Vivek Bernard
So .. no beer ;-) Hei thanks again. That works really good + the article will help me to start. I will actually begin in February to refactor the code. If you are interested in insights that I take I can send them to you. Actually.. 8000 loc .. probably I will have some insights ;-) Thanks again for help me to start. I was dealing with this topic already a few days.
Tom Tom