2009-11-12 5 views
0

J'ai un complément VBA Excel avec une méthode publique dans un fichier bas. Cette méthode crée actuellement un objet COM VB6, qui existe dans un VB6 exe/vbp. L'application VB6 se charge dans les données, puis la méthode d'ajout Excel peut appeler des méthodes sur l'objet COM VB6 pour charger les données dans un Excel xls existant. Tout cela fonctionne actuellement.
Depuis, nous avons converti notre application VB6 en C#.
Ma question est: Quelle est la meilleure façon de reproduire ce comportement avec l'application C#/.NET? Je pense que je ne serai peut-être pas capable d'extraire les données de l'application .NET dans Excel à partir de la méthode du complément, car l'application .Net doit être exécutée avec des données chargées (donc pas d'utilisation d'une bibliothèque de classe C# autonome). Peut-être que nous pouvons, à la place, pousser les données de .NET vers Excel en accédant à la méthode de complément VBA à partir du code C#?
Ce qui suit est la méthode VBA existant accéder à l'application VB6:Appel d'une méthode Excel Add-In à partir de l'application C# ou vice versa

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 

Toute aide serait appréciée.

Répondre

Questions connexes