エラーメッセージ

  • Deprecated function: Array and string offset access syntax with curly braces is deprecated in include_once() (line 20 of /home/users/0/ciao.jp-rpa/web/includes/file.phar.inc).
  • Deprecated function: Function create_function() is deprecated GeSHi->_optimize_regexp_list_tokens_to_string() (/home/users/0/ciao.jp-rpa/web/sites/all/libraries/geshi/geshi.php ファイル 4698行).
  • Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters drupal_get_feeds() (/home/users/0/ciao.jp-rpa/web/includes/common.inc ファイル 394行).

UiPath Snippet - Excelファイルのシート名一覧を取得する

ありそうでなかった(?)、Excelファイルの「シート名の一覧」を取得するVB.NETコードです。

引数の設定は画像を参照してください。System.String[] は、String型の配列(複数の変数を束ねたもの、と理解すればいいです)です。
UiPathの画面で変数のTypeとして選択するには、Array of [T] を選択すると、[T](変数の種類)を選択する画面が出るので、そこでStringを選択してください。

配列で受け取ることで、For Eachアクティビティと併用すると、「特定のExcelファイルの中にある、Excelのシートを順番に見ていく」といった処理が簡単に作れます。

 

注意点
このコードは「Microsoft Excelがインストールされている環境」でないと動きません。
UiPathのExcel Application ScopeはMicrosoft Excelがインストールされていなくても値の読み取り等ができますが、このコードはExcel自体の機能を使用しているため、インストールが必須になります。
また、Orchestrator経由で本コードを使用したWorkflowを使用する場合は、Excelファイルの処理を含む全体を、Main.xamlとは別のWorkflowにして、Invoke Workflowではなく、Launch Workflow Interactiveを使用することを推奨します。
(参考:Invoke Workflowと、Launch Workflow Interactiveの違い
※「Excelファイルの処理を含む全体」としているのは、呼び出し元と呼び出し先で、Excelの別セッションを開いてしまい、片方が「ダイアログが出る上、読み取り専用でしか開けない」というトラブルになるからです。

Source Code: 
Dim bindFlag As Reflection.BindingFlags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.IgnoreCase
 
Dim GetProperty As Func(Of Object, String, Object(), Object) =
    Function(target, propertyName, args) As Object
        Return target.GetType().InvokeMember(propertyName, bindFlag Or Reflection.BindingFlags.GetProperty, Nothing, target, args)
    End Function
 
Dim ReleaseCom As Action(Of Object) =
    Sub(target)
        If Not IsNothing(target) Then
            Runtime.InteropServices.Marshal.ReleaseComObject(target)
        End If
    End Sub
 
Dim ret As New List(Of String)
 
Try
    Dim xlBook As Object = GetObject(targetFile)
 
    Dim workSheets As Object = GetProperty(xlBook, "WorkSheets", Nothing)
    Dim wbCount As Int32 = CType(GetProperty(workSheets, "Count", Nothing), Int32)
 
    For i As Integer = 1 To wbCount
        Dim wSheet As Object = GetProperty(workSheets, "Item", New Object() {i})
        ret.Add(GetProperty(wSheet, "Name", Nothing).ToString())
        ReleaseCom(wSheet)
    Next
 
    ReleaseCom(workSheets)
    ReleaseCom(xlBook)
Catch ex As Runtime.InteropServices.COMException
    Throw New ApplicationException("Excelファイルの取得に失敗しました")
End Try
 
result = ret.ToArray()