@ECHO off
REM V1.0 2022-01-27 
cd /d %windir%
ECHO ----------------------------------------------------------------
ECHO                MS Office 2021 Activation Program(V1.2)
ECHO ----------------------------------------------------------------
ECHO.
REM Check OS edition
For /F "tokens=2 delims=[]" %%G in ('ver') Do (set _version=%%G) 
For /F "tokens=2 delims=. " %%G in ('echo %_version%') Do (set _major=%%G) 
if "%_major%"=="5" (
goto actxp
) else (
goto goact
)

REM Windows XP without IP check and isact
:actxp
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(v1.0) >>"%systemdrive%\office19.log"
ECHO ***************************************************** >>"%systemdrive%\office19.log"
ECHO ===WinXP=== >>"%systemdrive%/office19.log"
ECHO Activating, please waite...
cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /sethst:cckms.nccu.edu.tw >> "%systemdrive%\office19.log"
cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /act >>"%systemdrive%\xpact.log"
type "%systemdrive%\xpact.log">>"%systemdrive%\office19.log"
type "%systemdrive%\xpact.log"
cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /dstatus >>"%systemdrive%\office19.log"
ECHO.
ECHO   Office 2021 activation finish
ECHO.
ECHO   If it shows【Product activation successful】means your product has been activated,
ECHO   otherwise, 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 .
START %systemdrive%\
ECHO   Please notice that office19rpt.pdf is unreadable which is normal.
ECHO.
DEL /q "%systemdrive%\xpact.log"
ipconfig >>"%systemdrive%\office19.log"
cd "%systemdrive%\"
ren office19.log office19rpt.pdf> nul
pause
exit

:goact
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(v1.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 2021
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 2021
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:NMMKJ-6RK4F-KMJVX-8D9MJ-6MWKP >>"%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 2021 detected on this computer, 
ECHO     please check your program list in the Control Panel.
ECHO.
ECHO     If you've installed Office 2021 Pro Plus, please follow the solution below:
ECHO     "https://support.video.nccu.edu.tw/p/casfaq-en"
ECHO.
START microsoftedge.exe "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 2021 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 2021 activation successful
ECHO.
pause
exit
)
