Пожалуйста, помогите адаптировать код из excel VBA в visual studio 2017
Уважаемые Эксперты,
Я наблюдал необходимый код в vba excel, но не могу переместить его в visual studio и объединить в отдельное exe-приложение. Вы не могли бы мне помочь?
Что я уже пробовал:
'Procedure is called when files are dropped to the TreeView in the user form Private Sub ExportTreeView_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single) Dim fso As New FileSystemObject Dim fld As Folders Dim fls As Files Dim sFileName As String Dim sFilePath As String Dim iTotalTreeElems As Integer Dim sFilesArray() As String Dim iTotalFiles As Long Dim sBufArray(1000) As String Dim sBufSize As Integer Dim sRepName As String Dim iRows As Integer Dim sActiveSheet As String Dim sKey As String Dim sMask As String Dim sStr As String Dim i As Integer Dim j As Integer Application.ScreenUpdating = False sActiveSheet = ThisWorkbook.ActiveSheet.Name ThisWorkbook.Sheets("UserForm").Select sKey = "" 'ExportForm.edtFilterKey.Text iRows = Application.WorksheetFunction.CountA(Range("A:A")) iTotalTreeElems = Data.Files.Count iTotalFiles = 0 sBufSize = 0 'Get list of files from all folders and subfolders For i = 1 To iTotalTreeElems 'Check if dir is folder or filename If (fso.FolderExists(Data.Files(i)) = True) Then Call General.GetFileList(sFilesArray, Data.Files(i)) Else iTotalFiles = General.GetArraySize(sFilesArray) ReDim Preserve sFilesArray(iTotalFiles) sFilesArray(iTotalFiles) = Data.Files(i) End If Next i iTotalFiles = General.GetArraySize(sFilesArray) 'Go through the list of neccessary files and identify if files suits any of them by mask For i = 2 To iRows sMask = ThisWorkbook.ActiveSheet.Cells(i, 4) 'iTotFiles = Data.Files.Count For j = 1 To iTotalFiles If sFilesArray(j - 1) <> "" Then sFileName = fso.GetFileName(sFilesArray(j - 1)) sFilePath = fso.GetParentFolderName(sFilesArray(j - 1)) 'Check if file match current mask If General.CheckMask(sFileName, sMask) = True Then 'ThisWorkbook.ActiveSheet.Cells(i, 3) = sFileName 'ThisWorkbook.ActiveSheet.Cells(i, 6) = sFilePath sBufArray(sBufSize) = sFilesArray(j - 1) sBufSize = sBufSize + 1 'sFilesArray(j - 1) = "" 'Data.Files.Remove (j - 1) 'Exit For End If End If Next j sKey = RemoveFiltrSymbols(sKey) 'If more than one file matched Mask then selecet one that has key word If sBufSize > 1 Then For j = 1 To sBufSize sRepName = RemoveFiltrSymbols(sBufArray(j - 1)) If InStr(UCase(sRepName), UCase(sKey)) Then ThisWorkbook.ActiveSheet.Cells(i, 3) = fso.GetFileName(sBufArray(j - 1)) ThisWorkbook.ActiveSheet.Cells(i, 6) = fso.GetParentFolderName(sBufArray(j - 1)) End If Next j Else If sBufSize = 1 Then ThisWorkbook.ActiveSheet.Cells(i, 3) = fso.GetFileName(sBufArray(0)) ThisWorkbook.ActiveSheet.Cells(i, 6) = fso.GetParentFolderName(sBufArray(0)) End If End If sBufSize = 0 Next i ThisWorkbook.Sheets(sActiveSheet).Select Application.ScreenUpdating = True End Sub
RedDk
Here's how I'd go about doing exactly what it is you wish to do -> convert a VBA "program" into an executable running with methods exposed and accessible through VBNET but it's going to take some work on your part. Open the Excel app and on the Developer tab select the Visual Basic editor (Alt+F11) and launch the interface. Find your VBA code block (as above, we'll assume the editor is where you found that block) and with the code under focus and once visible in the workspace, select Object Browser (F2) to make sure that window is open also. You'll see in the Object Browser a bunch of things not the least of which is the libraries under use in the project, the classes, member classes, etcetera ... and you can choose any of the project libraries in the drop down and see the methods and classes change beneath. This is the key place to begin finding the functionality that crosses over to VBNET.
Putting your cursor on any keyword in your code block and hitting F1, say GetFileList, will either result in an Excel Help item or "Keyword not found" but in whichever case, links at the top of help page, say to "Excel XXXX developer Reference" might provide more insight. I'd suggest finding a code block entry which does have a keyword entry ... and start there. You have to set some breakpoints and step through you VBA to see what works. And ususally something like an error message will mean that a library is missing so you'll have use the Object Browser to find a similar keyword and figure out what that library might be then include by adding it to References (right context click - popup to see the list of potential online libs available).
Короче говоря, после того как вы выяснили, что ваш код Excel работает так, как ожидалось, пройдя через него, у вас будет первый бит, Если вы поймете функциональность, которая (неизбежно) будет доступна в VS vbnet. - Почти гарантирую, что код находится в VS VBNET, в какой-нибудь инкарнации фреймворка или системной базовой библиотеке ... а потом еще немного.
Другого пути нет, мой друг.