我如何使用vb.net读取Excel工作表的文本框(尝试使用uipath自动化过程来调用代码活动)

时间:2019-07-03 13:43:36

标签: vb.net interop uipath

我正在尝试使用VB.NET从Excel文本框获取值,但是一切都出错了。 (错误如:““ Forms”不是Interop的成员或“ TextBos不是接口”))

有什么想法吗?

这是我的代码:

Dim xlApp As Application
Dim xlWorkBooks As Workbooks 
Dim xlWorkBook As Workbook 
Dim xlWorkSheet As Worksheet 
Dim xlWorkSheets As Sheets 

Using Microsoft.Vbe.Interop.Forms.TextBox

    Dim tb As Forms.TextBox

    xlApp = New Application

    xlApp.DisplayAlerts = False

    xlWorkBooks = xlApp.Workbooks

    xlWorkBook = xlWorkBooks.Open("C:\Users\pw02318\Documents\UiPath\Accese\exemplu_formular.xlsm")

    'xlApp.Visible = True

    xlWorkSheets = xlWorkBook.Sheets

    xlWorkSheet = CType(xlWorkSheets("New Form"), Worksheet)

    For Each  shp As Shape In xlWorkSheet.Shapes

        Dim oo As OLEObject = CType(xlWorkSheet.OLEObjects(shp.Name), OLEObject)
        tb = CType(oo.Object, Microsoft.Vbe.Interop.Forms.TextBox)
        console.WriteLine(oo.Name.ToString + tb.Text)

    Next shp

 End Using

1 个答案:

答案 0 :(得分:0)

您可以使用ExcelDataReader读取excel文件并将其放入数据读取器中。

首先将OpenXml.Excel.Data添加到引用中,并将其导入代码中,然后可以编写如下代码:

    logDestination = Environment.CurrentDirectory & "\log.txt"

    Dim filePaths As String() = Directory.GetFiles(path, "*.xlsx")

    If filePaths.Length > 1 Then
        File.AppendAllText(logDestination, DateTime.Now & " -----> There Is More Than One xlsx File In The Path Folder" + Environment.NewLine)
    ElseIf filePaths.Length = 0 Then
        File.AppendAllText(logDestination, DateTime.Now & " -----> There Is No xlsx File In The Path" + Environment.NewLine)
        Environment.[Exit](0)
    End If

    Dim dt = New DataTable()

    Dim reader As var = New ExcelDataReader(filePaths(0), 1, True)

    dt.Load(reader)