1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UiPathでExcelを操作する(活用編 : Excelファイルが他人に開かれているか判定する)

Posted at

UiPathでExcelを操作する(活用編 : Excelファイルが他人に開かれているか判定する)

概要

UiPathでExcelのファイルが他の人に開かれているか判定する方法をご紹介します。
他の人が開いている場合、IsOnlyMe がFalseの値です。

お約束事項(免責事項)

  • この記事は2020年6月時点の情報を基に作成しております。
  • 記事の内容は私個人の見解であり、所属する組織の公式見解ではありません。

スキル

この記事には、C#によるカスタムアクティビティの開発が含まれます。
カスタムアクティビティの開発方法は、UiPath公式のドキュメントを参照ください。
(本記事では最低限のみ記載します。)
https://docs.uipath.com/activities/lang-ja/docs/creating-a-custom-activity

必要なソフトウェア

  • Visual Studio
  • Nuget または Nuget Package Explorer
  • Microsoft Office

実装

using Microsoft.Office.Interop.Excel;
using System;
using System.Activities;
using System.ComponentModel;


namespace UiPathCustomLibrary
{
    public class UserStatus : CodeActivity
    {

        [Category("Input")]
        [RequiredArgument]
        public InArgument<Object> WorkbookApplication { get; set; }
        [Category("Output")]
        public OutArgument<Boolean> IsOnlyMe { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            // 引数(InArgument)より、WorkbookApplicationのオブジェクトを取得する
            // WorkbookApplicationの属性に必須(RequireArgument)を指定しているため、workbookappのnullチェックは不要
            object workbookapp = WorkbookApplication.Get(context);

            // WorkbookApplicationから、Microsoft.Office.Interop.Excel.Workbook型として(キャストして)、CurrentWorkbookを読み込む
            Workbook workbook = workbookapp.GetType().GetProperty("CurrentWorkbook").GetValue(workbookapp) as Workbook;

            // 型変換する 
            object[,] userStatus = (workbook.UserStatus as object[,]);

            // ブック共有のユーザ数を取得する
            int userCount = userStatus.GetLength(0);

            // ユーザ数が1の場合、自分自身のみとして判定する。
            // https://docs.microsoft.com/ja-jp/office/vba/api/excel.workbook.userstatus
            IsOnlyMe.Set(context,  userCount == 1);
        }
    }
}


1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?