Is there a way of adding to this code the name of the word document it can close.…
- User
Is there a way of adding to this code the name of the word document it can close. Currently if I am in my excel sheet and I deactivate the sheet it closes any instance of Word that is open. I want it o only close word document named Admin Request & Task Request: Private Sub Worksheet_Deactivate()
'Module9.sheetJobRequestActive = True 'THIS SET THE GLOBAL VARIABLE TO - TRUE - because the above code checks for TRUE the code runs Module9.sheetJobRequestActive = False 'THIS SET THE GLOBAL VARIABLE TO - FALSE - becuase the above code checks for TRUE the code will NOT run again
codeDim objWord As Object On Error Resume Next Set objWord = GetObject(, "Word.Application") If Err.Number <> 0 Then 'MsgBox "Microsoft Word is not open!", vbCritical, "Error" Exit Sub End If objWord.Quit SaveChanges:=wdDoNotSaveChanges Set objWord = NothingEnd Sub
- Assistant
Yes, you can add a check to see if the currently active Word document's name matches the desired name before closing it. Here's the modified code:
Private Sub Worksheet_Deactivate() 'Check if Job Request sheet was active Module9.sheetJobRequestActive = False
code'Close Admin Request & Task Request Word document if open Dim objWord As Object Dim objDoc As Object On Error Resume Next Set objWord = GetObject(, "Word.Application") If Err.Number <> 0 Then 'MsgBox "Microsoft Word is not open!", vbCritical, "Error" Exit Sub End If For Each objDoc In objWord.Documents If objDoc.Name = "Admin Request & Task Request" Then 'Change this to match the desired document name objDoc.Close SaveChanges:=wdDoNotSaveChanges Exit For 'Exit loop as soon as desired document is closed End If Next objDoc Set objWord = Nothing Set objDoc = Nothing - User
The new code does not close any instance of word. The original code works perfectly. Is there a way of modifying the original code to close only the documents with specific names
- Assistant
Yes, you can modify the original code to only close documents with specific names like so:
Private Sub Worksheet_Deactivate() 'Check if Job Request sheet was active Module9.sheetJobRequestActive = False
code'Close Admin Request & Task Request Word document if open Const docName As String = "Admin Request & Task Request" 'Change this to match the desired document name Dim objWord As Object Dim objDoc As Object On Error Resume Next Set objWord = GetObject(, "Word.Application") If Err.Number <> 0 Then 'MsgBox "Microsoft Word is not open!", vbCritical, "Error" Exit Sub End If For Each objDoc In objWord.Documents If objDoc.Name = docName Then objDoc.Close SaveChanges:=wdDoNotSaveChanges End If Next objDoc Set objWord = Nothing Set objDoc = Nothing - User
where in this code do we enter the document names
- Assistant
You can modify the code to include the specific document names that you want to close like so:
Private Sub Workbook_Deactivate() Const docName1 As String = "Document1.docx" Const docName2 As String = "Document2.docx" 'Add as many document names as needed
codeDim objWord As Object Dim objDoc As Object On Error Resume Next Set objWord = GetObject(, "Word.Application") If Err.Number <> 0 Then Exit Sub 'No Word instance is open, so nothing to close End If For Each objDoc In objWord.Documents If objDoc.Name = docName1 Or objDoc.Name = docName2 Then 'Add additional OR conditions for more documents objDoc.Close SaveChanges:=wdDoNotSaveChanges End If Next objDoc Set objWord = Nothing Set objDoc = Nothing - User
in vba if you Set objWord = GetObject(, "Word.Application") , how can i write the code for objDoc = The Word Document Name
- Assistant
If you want to set a variable
objDocto a specific Word document based on its name, you can modify the code like so:Dim objWord As Object Dim objDoc As Object Dim docName As String docName = "Admin Request & Task Request" 'Change this to match the desired document name On Error Resume Next Set objWord = GetObject(, "Word.Application") If Err.Number <> 0 Then 'MsgBox "Microsoft Word is not open!", vbCritical, "Error" Exit Sub End If For Each objDoc In objWord.Documents If objDoc.Name = docName Then 'Found the desired document, do something with it ' 'For example, activate a specific window in the document: objWord.Activate 'Activate Word if it's not already active objDoc.Windows(1).Activate Exit For 'Exit loop as soon as desired document is found End If Next objDoc Set objWord = Nothing Set objDoc = Nothing
This code sets the
docNamevariable to the desired document name, then loops through all open Word documents and checks if their names match . If a match is found, the variable is set to that document, which can then be used to perform further operations on the document as needed. - User
How can I make this work: Private Sub Worksheet_Deactivate()
Dim objDoc As Object Dim objWord As Object Dim docName As String
codeOn Error Resume Next Set objWord = GetObject(, "Word.Application")If docName <> "AdminRequest.docm" Or docName <> "TaskRequest.docm" Then Exit Sub objWord.Quit SaveChanges:=wdDoNotSaveChanges
codeSet objWord = NothingEnd Sub
- Assistant
You need to move the
docNamevariable assignment inside the loop that checks all open Word documents, and change the condition for exiting the sub to use theobjDocvariable instead ofdocName. Here's the modified code:Private Sub Worksheet_Deactivate() Dim objWord As Object Dim objDoc As Object Dim docName As String
codeOn Error Resume Next Set objWord = GetObject(, "Word.Application") If Err.Number <> 0 Then Exit Sub 'No Word instance is open, so nothing to close End If For Each objDoc In objWord.Documents docName = objDoc.Name 'Assign the current document name to docName variable If docName = "AdminRequest.docm" Or docName = "TaskRequest.docm" Then objDoc.Close SaveChanges:=wdDoNotSaveChanges End If Next objDoc Set objWord = Nothing Set objDoc = Nothing