Notification texts go here Contact Us Buy Now!

Closing word application from Excel VBA

Closing Microsoft Word from Excel VBA

VBA provides a seamless way to automate tasks and enhance productivity in Microsoft Office applications. One common requirement is closing Word documents or the entire Word application from within Excel. Here are several methods to achieve this:

1. Close Word Application (Without Saving):
Option Explicit

Sub CloseWordWindows()

Dim objWord As Object

On Error Resume Next
Set objWord = GetObject(, "Word.Application")

' if no active Word is running >> exit Sub
If objWord Is Nothing Then
    Exit Sub
End If

objWord.Quit
Set objWord = Nothing

End Sub
2. Close All Running Word Documents:
Option Explicit

Sub CloseWordDocuments()

    Dim objWord As Object

    Do
        On Error Resume Next
        Set objWord = GetObject(, "Word.Application")
        On Error Go To 0
        If Not objWord Is Nothing Then
            objWord.Quit
            Set objWord = Nothing
        End If
    Loop Until objWord Is Nothing

End Sub
3. Close All Word Documents Without Saving:
Sub CloseWordDocumentsWithoutSave()
Dim objWordApp As Object
Dim objDoc As Object

'Attempt to get an existing Word application object
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo 0

'Check if Word application object is successfully set
If Not objWordApp Is Nothing Then
    'Loop through all open Word documents and close them without saving
    For Each objDoc In objWordApp.Documents
        objDoc.Close False ' Close without saving changes
    Next objDoc

    'Quit Word application
    objWordApp.Quit
    Set objWordApp = Nothing
Else
    MsgBox "No open Word documents found.", vbExclamation
End If
End Sub
4. Close Word via PowerShell Script:
Sub Comesfast()
X = Shell("powershell.exe kill -processname winword", 1)
End Sub
Note: Always handle errors appropriately using On Error statements to prevent unexpected behavior. Additionally, ensure that you have the necessary permissions to close Word documents or the application.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.