LoginSignup
0
0

More than 3 years have passed since last update.

C#でExcelブックの「名前」を全消去する

Last updated at Posted at 2020-09-23

動機

古ーいExcelファイルをコピーしてつかっていると、ときどき出るコレがうざすぎて、C#アプリからExcelブックの「名前」を削除するソフトをつくってみた。

手動で消す場合

image.png

image.png

全選択ができず、1つずつ消す羽目になる。数が多いとストレスがえぐい・・
ほんとMS Officeってクソ・・・

注意事項

むやみに「名前」を消すと、数式やマクロが動作しなくなるおそれがあります。
そのエクセル自体で使用していなくても、ほかのツールがそのエクセルファイル内の「名前」の使用を前提にしたシステムになっている場合もありえます。

ソースコード


using System;
using System.Drawing;
using System.Runtime.CompilerServices; // to use [MethodImpl(MethodImplOptions.NoInlining)]
using System.Runtime.InteropServices;
//using System.Text.RegularExpressions;
using System.Windows.Forms;

using Excel = Microsoft.Office.Interop.Excel;
//using Microsoft.Office.Core;

class ExcelNameRemover : Form
{
    Button btnStartTryGet;


    [MethodImpl(MethodImplOptions.NoInlining)]
    void TryGetActiveBook()
    {
        try {
            var oExcelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");

            if (oExcelApp == null) {return;}

            dynamic book;
            book = oExcelApp.ActiveWorkbook;

            var res = MessageBox.Show("Are you sure to delete all names in the excel: \"" + book.Name+"\"", "Confirmation", MessageBoxButtons.OKCancel );
            if (res == DialogResult.OK ) {
                dynamic names = book.Names;
                int count=0;
                foreach(dynamic name in names){
                    name.Delete();
                    count++;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                Console.WriteLine(count.ToString() + " names are deleted.");
            }
        }
        catch(Exception e) {
            if (e is Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ||
                e is COMException ) {

                Console.WriteLine(e);
                // もみ消す
                return;
            }
            throw e;
        }
        finally {
        }
    }


    ExcelNameRemover()
    {
        Controls.Add(btnStartTryGet = new Button(){
            Text = "Remove all names",
            Location = new Point(0, 0),
            Width = 200
        });
        btnStartTryGet.Click += (s,e)=>{
            TryGetActiveBook();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        };
    }

    [STAThread]
    static void Main(string[] args)
    {
        //DumpTextOfActiveSheet();
        Application.Run(new ExcelNameRemover());
    }
}

コンパイル方法

※dllのパスは環境に依存すると思います。

compile.bat

csc ^
 /r:C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll ^
 /r:C:\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\Office.dll ^
 %*

compile.bat ファイル名.cs
でコンパイルできます。

0
0
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
0
0