Const FILE_NAME="GrepAddin.xlam"

Call Exec

Sub Exec()
    Dim objExcel
    Dim strAdPath
    Dim strMyPath
    Dim strAdCp
    Dim strMyCp
    Dim objFileSys
    Dim oAdd

    Set objExcel   = CreateObject("Excel.Application")
    Set objFileSys = CreateObject("Scripting.FileSystemObject")

    strAdPath = objExcel.Application.UserLibraryPath
    strMyPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
    strAdCp   = objFileSys.BuildPath(strAdPath, FILE_NAME)
    strMyCp   = objFileSys.BuildPath(strMyPath, FILE_NAME)

    If objFileSys.FileExists(strAdCp) Then
        result = MsgBox("同名のアドインファイルが存在します。上書きしますか？", vbYesNo + vbQuestion, "上書き確認")
        If result = vbNo Then
            WScript.Quit
        End If
    End If

    objFileSys.CopyFile strMyCp, strAdCp

    objExcel.Workbooks.Add
    Set oAdd = objExcel.AddIns.Add(strAdCp,True)
    oAdd.Installed = True
    objExcel.Quit

    Set objExcel   = Nothing
    Set objFileSys = Nothing

    MsgBox "インストールしました。", vbOkOnly + vbInformation, "完了"
End Sub
