1+ #
2+ #
3+ # source: https://github.com/DecimalTurn/VBA-Build/blob/main/scripts/Open-Close-Office.ps1
4+ # author: Martin Leduc
5+ #
6+
7+
8+ # This script will list all the process that are part of the Office suite (and generic Office processes) and close them.
9+ # It will also check if the processes are running and close them if they are.
10+
11+ # Define an array of Office-related process names
12+ $officeProcesses = @ (
13+ " WINWORD" , # Microsoft Word
14+ " EXCEL" , # Microsoft Excel
15+ " POWERPNT" , # Microsoft PowerPoint
16+ " OUTLOOK" , # Microsoft Outlook
17+ " ONENOTE" , # Microsoft OneNote
18+ " MSACCESS" , # Microsoft Access
19+ " MSPUB" , # Microsoft Publisher
20+ " TEAMS" , # Microsoft Teams
21+ " lync" , # Skype for Business
22+ " ONENOTEM" , # OneNote quick notes
23+ " VISIO" , # Microsoft Visio
24+ " OfficeClickToRun" , # Office Click-to-Run
25+ " OfficeC2RClient" # Office Click-to-Run Client
26+ )
27+
28+ # Function to check and close Office processes
29+ function Close-OfficeProcesses {
30+ Write-Host " Checking for running Office processes..." - ForegroundColor Cyan
31+
32+ $runningProcesses = Get-Process | Where-Object { $officeProcesses -contains $_.Name }
33+
34+ if ($runningProcesses.Count -eq 0 ) {
35+ Write-Host " No Office processes are currently running." - ForegroundColor Green
36+ return
37+ }
38+
39+ Write-Host " Found $ ( $runningProcesses.Count ) running Office processes:" - ForegroundColor Yellow
40+ foreach ($process in $runningProcesses ) {
41+ Write-Host " - $ ( $process.Name ) (PID: $ ( $process.Id ) )" - ForegroundColor Yellow
42+ }
43+
44+ Write-Host " Closing Office processes..." - ForegroundColor Cyan
45+ foreach ($process in $runningProcesses ) {
46+ try {
47+ $process | Stop-Process - Force - ErrorAction Stop
48+ Write-Host " Successfully closed $ ( $process.Name ) ." - ForegroundColor Green
49+ }
50+ catch {
51+ Write-Host " Failed to close $ ( $process.Name ) : $ ( $_.Exception.Message ) " - ForegroundColor Red
52+ }
53+ }
54+
55+ # Wait for a moment to ensure processes are closed
56+ Start-Sleep - Seconds 15
57+
58+ Write-Host " Re-checking for any remaining Office processes..." - ForegroundColor Cyan
59+
60+ # Check if any processes remain running
61+ $remainingProcesses = Get-Process | Where-Object { $officeProcesses -contains $_.Name }
62+ if ($remainingProcesses.Count -gt 0 ) {
63+ Write-Host " `n WARNING: $ ( $remainingProcesses.Count ) Office processes could not be closed:" - ForegroundColor Red
64+ # Let's try a second time to close them
65+ foreach ($process in $remainingProcesses ) {
66+ try {
67+ $process | Stop-Process - Force - ErrorAction Stop
68+ Write-Host " Successfully closed $ ( $process.Name ) ." - ForegroundColor Green
69+ }
70+ catch {
71+ Write-Host " Failed to close $ ( $process.Name ) : $ ( $_.Exception.Message ) " - ForegroundColor Red
72+ }
73+ }
74+ # Re-check for any remaining processes
75+ Start-Sleep - Seconds 15
76+ $remainingProcesses = Get-Process | Where-Object { $officeProcesses -contains $_.Name }
77+ if ($remainingProcesses.Count -eq 0 ) {
78+ Write-Host " All Office processes have been successfully closed." - ForegroundColor Green
79+ }
80+ else {
81+ Write-Host " Some Office processes are still running:" - ForegroundColor Red
82+ # List any remaining processes
83+ foreach ($process in $remainingProcesses ) {
84+ Write-Host " - $ ( $process.Name ) (PID: $ ( $process.Id ) )" - ForegroundColor Red
85+ }
86+ }
87+
88+ }
89+ else {
90+ Write-Host " `n All Office processes have been successfully closed." - ForegroundColor Green
91+ }
92+ }
93+
94+
95+ $officeApps = $args [0 ]
96+ if (-not $officeApps ) {
97+ Write-Host " No Office applications specified. Exiting script." - ForegroundColor Red
98+ exit 1
99+ }
100+
101+ # Convert the comma-separated string to an array
102+ $officeApps = $officeApps -split ' ,' | ForEach-Object { $_.Trim () }
103+
104+ # Open each required Office application
105+ foreach ($app in $officeApps ) {
106+ Write-Host " Opening $app application..." - ForegroundColor Cyan
107+ try {
108+ # Create a new COM object for the specified Office application
109+ $comObject = New-Object - ComObject " $app .Application"
110+ Write-Host " $app application opened successfully." - ForegroundColor Green
111+ $comObject = $null
112+ }
113+ catch {
114+ Write-Host " Failed to open $app application: $ ( $_.Exception.Message ) " - ForegroundColor Red
115+ }
116+ }
117+
118+ # Wait for a moment to ensure processes are opened
119+ Start-Sleep - Seconds 5
120+
121+ # Execute the function
122+ Close-OfficeProcesses
0 commit comments