4

我应该将大量用 VBA (Excel) 编写的代码转换为 VB6。但我真的不知道我必须照顾什么或从哪里开始。因此,如果能从 VB6 专家那里得到一些提示,那就太好了。

我已经安装了 MS Visual Studio 并玩了一下。但我不是 VB6 专家,也不知道我必须做什么。

最终目标是将当前放置在一个 excel vba 宏中的所有 VBA 代码放入 VB6 项目中,并从中创建一个 .dll。这个 .dll 应该被 excel 引用,并且 excel 应该像现在一样运行 :-)

例如,我必须做什么才能将此 vba 代码转换为 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

编辑:是的,如果可以将 vba 代码转换为 .dll 也可以。然后我就不必转换代码了。但我认为只能用 vb6 代码创建一个 .dll。

4

3 回答 3

5

@Tom

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

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


In Visual Studio 2005 open a new Class Library Project Then remove all the garbage already 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 output is a .DLL and a .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 an excel workbook that contains a reference 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/

于 2010-01-11T19:10:28.700 回答
2

转换为 VB6 很容易。

  1. 创建一个 VB6 DLL 项目。在 Web 上搜索有关如何执行此操作以及如何公开方法、类和函数的说明。

  2. 添加对“Microsoft Office Excel ## library”的引用。

  3. 在将作为 DLL 中的方法公开的项目过程中,添加以下代码:

    Dim E As Excel.Application
    Set E = GetObject(, "Excel.Application")
    'or if Excel is not running use CreateObject("Excel.Application")
    'You can use error handling to figure out which one you need.
    
  4. 继续使用您的普通 VBA 代码,进行一项修改:全局访问的对象,例如ActiveSheetorActiveWorkbookSheetsmust 变为E.ActiveSheet和。E.ActiveWorkbookE.Sheets

如果您的 VBA 项目中有表单,则转换它们需要做更多的工作,因为 VB6 和 VBA 中的表单完全不同(它们的工作方式长期不同)。

于 2012-12-21T20:09:39.397 回答
0

@汤姆汤姆

将代码从 .VBA 转换为 vb6 应该没有问题。事实上,您几乎不必这样做。

问题是在 VB6 上下文中,语言无法理解是什么

“Application.Worksheets(a).range("C3").Value)”的意思是,

对象Application在VB6中有不同的含义

VBA(您拥有的 VBA 版本)几乎是 VB6 在 Excel(或 word 或 MSO 附带的任何东西)中的自定义实现。

尝试从 VB6 访问 Excel UI 是个坏主意(我什至不确定它是否可能)

您应该做的是将业务逻辑与代码分开,然后将其放入 VB6 库中。

例如,您的代码(据我所知)返回单元格 C3 的值

它与 Excel UI 密切相关,因此如果不是不可能将代码转换为 VB6,那将是非常适得其反的。

因为即使你转换了代码,像 (Application.Worksheets(a).range("C3").Value) 这样的大部分变量都必须从 VBA 中调用,这是没有意义的

但是,如果您有任何其他纯业务逻辑,则可以轻松移植(这很有趣,因为真的没有什么可移植的)

于 2010-01-11T11:30:41.617 回答