2009-11-12 38 views
0

我有一个Excel VBA加载项在bas文件中带有公共方法。此方法当前创建一个VB6 COM对象,该对象存在于正在运行的VB6 exe/vbp中。 VB6应用程序加载数据,然后Excel加载项方法可以调用VB6 COM对象上的方法将数据加载到现有的Excel xls中。这一切都正在工作。
我们已经将我们的VB6应用程序转换为C#。
我的问题是:什么是最好/最简单的方式来模仿C#/。NET应用程序的这种行为? 我想我可能无法将数据从.NET应用程序从加载项方法导入到Excel中,因为.Net应用程序需要与数据加载一起运行(所以不需要使用独立的C#类库)。也许我们可以通过访问C#代码中的VBA加载项方法将数据从.NET推送到Excel?
以下是现有VBA方法访问VB6应用程式:从C#应用程序或反之亦然调用Excel加载项方法

Public Sub UpdateInDataFromApp() 
    Dim wkbInData As Workbook 
    Dim oFPW As Object 
    Dim nMaxCols As Integer 
    Dim nMaxRows As Integer 
    Dim j As Integer 
    Dim sName As String 
    Dim nCol As Integer 
    Dim nRow As Integer 
    Dim sheetCnt As Integer 
    Dim nDepth As Integer 
    Dim sPath As String 
    Dim vData As Variant 
    Dim SheetRange As Range 

    Set wkbInData = wkbOpen("InData.xls") 

    sPath = g_sPathXLSfiles & "\" 

    'Note: the following will bring up fpw app if not already running 
    Set oFPW = CreateObject("FPW.CProfilesData") 

    If oFPW Is Nothing Then 
     MsgBox "Unable to reference " & sApp 
    Else 
      . 
      . 
      .  
     sheetCnt = wkbInData.Sheets.Count 'get number of sheets in indata workbook 
     For j = 2 To sheetCnt 'set counter to loop over all sheets except the first one which is not input data fields 
      With wkbInData.Worksheets(j) 
      Set SheetRange = .UsedRange 
      End With 
      With SheetRange 
      nMaxRows = .Rows.Count 'get range of sheet(j) 
      nMaxCols = .Columns.Count 'get range of sheet(j) 
      Range(.Cells(2, 2), .Cells(nMaxRows, nMaxCols)).ClearContents 'Clears data from data range (51 Columns) 
      Range(.Cells(2, 2), .Cells(nMaxRows, nMaxCols)).ClearComments 
      End With 
      With oFPW 'vb6 object 
      For nRow = 2 To nMaxRows ' loop through rows 
       sName = SheetRange.Cells(nRow, 1) 'Field name 
       vData = .vntGetSymbol(sName, 0) 'Check if vb6 app identifies the name 

       nDepth = .GetInputTableDepth(sName) 'Get number of data items for this field name from vb6 app 
       nMaxCols = nDepth + 2 'nDepth=0, is single data item 
       For nCol = 2 To nMaxCols 'loop over deep screen fields 
        nDepth = nCol - 2  'current depth 
        vData = .vntGetSymbol(sName, nDepth) 'Get Data from vb6 app 
        If LenB(vData) > 0 And IsNumeric(vData) Then 'Check if data returned 
         SheetRange.Cells(nRow, nCol) = vData 'Poke the data in 
        Else 
         SheetRange.Cells(nRow, nCol) = vData 'Poke a zero in 
        End If 

       Next 'nCol 
      Next 'nRow 
      End With 
      Set SheetRange = Nothing 
     Next 'j 

    End If 

    Set wkbInData = Nothing 
    Set oFPW = Nothing 
Exit Sub 
    . 
    . 
    . 
End Sub 

任何帮助,将不胜感激。

回答

相关问题