@ECHO off
REM V1.0 2024-10-28 
cd /d %windir%

:: 
NET SESSION >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    CLS
    ECHO.
    ECHO   +--------------------------------------------------------+
    ECHO   ^|       Administrator Privileges Required               ^|
    ECHO   +--------------------------------------------------------+
    ECHO.
    ECHO   This program requires administrator privileges to run.
    ECHO.
    ECHO   Please close this window, and right-click on this .bat file,
    ECHO   then select "Run as administrator".
    ECHO.
    PAUSE
    GOTO :EOF
)


ECHO ----------------------------------------------------------------
ECHO                MS Office 2024 Activation Program(V2.0)
ECHO ----------------------------------------------------------------
ECHO.
if exist "%systemdrive%\office19rpt.pdf" (
cd "%systemdrive%\"
ren office19rpt.pdf office19.log
) >nul
ECHO.  >>"%systemdrive%\office19.log"
ECHO ***************************************************** >>"%systemdrive%\office19.log"
ECHO    %date% %time% Activation Program(v2.0) >>"%systemdrive%\office19.log"
ECHO ***************************************************** >>"%systemdrive%\office19.log"
ECHO Step1: Check Internet
ECHO.
ipconfig >"%systemdrive%\ip.log"
find /C /I "140.119" "%systemdrive%\ip.log" >nul
if %ERRORLEVEL% EQU 0 (
DEL /q "%systemdrive%\ip.log"
goto act
) else (
DEL /q "%systemdrive%\ip.log"
ECHO.                    
ECHO   Please make sure that your computer must connect to the campus internet,
ECHO   or through VPN. Otherwise, failed to activate.
ECHO.
ECHO Exit?
CHOICE /C yn /M "Y)Yes  N)No, I'll keep trying."
if ERRORLEVEL 2 goto act
if ERRORLEVEL 1 goto cnct
)

:act
ECHO Step2: Check Office 2024
ECHO.
if exist "%installdir%\Program Files\Microsoft Office\root\Office16\winword.exe" (
set OfficePath="%installdir%\Program Files\Microsoft Office\Office16\"
ECHO ===SameByte=== >>"%systemdrive%/office19.log"
goto acta
) else if exist "%installdir%\Program Files (x86)\Microsoft Office\root\Office16\winword.exe" (
set OfficePath="%installdir%\Program Files (x86)\Microsoft Office\Office16\"
ECHO ===DifByte=== >>"%systemdrive%/office19.log"
goto acta
) else (
goto msg
)

:acta
ECHO Step3: Set KMS host
ECHO.
cscript %OfficePath%ospp.vbs /sethst:cckms.nccu.edu.tw >> "%systemdrive%\office19.log"
ECHO Step4: Activate Office 2024
ECHO.
cscript %OfficePath%ospp.vbs /act >>"%systemdrive%\isact.log"
type "%systemdrive%\isact.log" >> "%systemdrive%\office19.log"
type "%systemdrive%\isact.log"
cd "%systemdrive%\"
FIND /c /i "No installed product keys detected" "%systemdrive%\isact.log" >nul
if %ERRORLEVEL% EQU 0 (
ECHO Fixing No installed product keys detected >>"%systemdrive%\office19.log"
cscript %OfficePath%ospp.vbs /inpkey:XJ2XN-FW8RK-P4HMP-DKDBV-GCVGB >>"%systemdrive%\office19.log"
cscript %OfficePath%ospp.vbs /act >>"%systemdrive%\isact.log"
ECHO No installed product keys detected problem fixed
)
cscript %OfficePath%ospp.vbs /dstatus >>"%systemdrive%\office19.log"
goto isact

:msg
ECHO ===NoOffice=== >>"%systemdrive%\office19.log"
cd "%systemdrive%\"
ren office19.log office19rpt.pdf
ECHO     No office 2024 detected on this computer, 
ECHO     please check your program list in the Control Panel.
ECHO.
ECHO     If you've installed Office 2024 Pro Plus, please follow the solution below:
ECHO     "https://support.video.nccu.edu.tw/p/casfaq-en"
ECHO.
start "" "https://support.video.nccu.edu.tw/p/casfaq-en"
ECHO.
ECHO Exit Activation Program
pause
exit

:cnct
ECHO ===IP issue=== >>"%systemdrive%\office19.log"
netstat -n -p tcp >>"%systemdrive%\office19.log"
cd "%systemdrive%\"
ren office19.log office19rpt.pdf
exit

:isact
ipconfig >>"%systemdrive%\office19.log"
cd "%systemdrive%\"
ren office19.log office19rpt.pdf
ECHO.
ECHO Report:
FIND /c /i "Product activation successful" "%systemdrive%\isact.log" >nul
if ERRORLEVEL 1 (
DEL /Q "%systemdrive%\isact.log"
ECHO.
ECHO   Office 2024 activation failed
ECHO.
ECHO   Please check the error code solution in the following page:
ECHO   "https://support.video.nccu.edu.tw/p/casfaq-en"
ECHO.
ECHO   Or send the following information and the file office19rpt.pdf on system disk(C:) 
START %systemdrive%
ECHO   to software@nccu.edu.tw .
ECHO   Please notice that office19rpt.pdf is unreadable which is normal.
START %systemdrive%
ECHO.
pause
exit
) else (
DEL /Q "%systemdrive%\isact.log"
ECHO.
ECHO   Office 2024 activation successful
ECHO.
pause
exit
)

:EOF
EXIT /B 1