Need help with Excel VBA code to close a Word document.

I'm using 2010 versions of Excel and Word.
When my Excel workbook, TT.xlsm, is opened, Workbook_Open() calls a local Sub, OpenAWordDocument, which, in turn, opens a Word document.

Sub OpenAWordDocument(Optional ByVal DocName As String = "", Optional ByVal DocPath As String = "")
Dim objWord As Object
  If Len(Trim(DocName)) = 0 Then
    DocName = "Word Document.docx"
  End If
  If Len(Trim(DocPath)) = 0 Then
    DocPath = "D:\Documentation"
  End If
  If Right(DocPath, 1) <> Application.PathSeparator Then
    DocPath = DocPath & Application.PathSeparator
  End If
'Open an existing Word Document from Excel
  Set objWord = CreateObject("Word.Application")
  With objWord
    .Visible = True
    .Documents.Open DocPath & DocName
  End With
  Set objWord = Nothing
End Sub[/indent][/indent][/indent]

Open in new window


I would appreciate some help developing a procedure to call from Workbook_BeforeClose() which will Save and Close the Word document without requiring operator intervention.

Thanks,
Tonkawa Jingles
LVL 1
J2FAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
You'll need module-level variables for the object, so to check it has been set later. I tested the following, and it worked:
Option Explicit
Public mobjDoc As Object
Public mobjWord As Object
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Not mobjWord Is Nothing Then
        If mobjDoc.Saved = False Then
            mobjDoc.Save
        End If
        mobjWord.Quit
    End If
    Set mobjDoc = Nothing
    Set mobjWord = Nothing
End Sub
Private Sub Workbook_Open()
    Set mobjDoc = Nothing
    Set mobjWord = Nothing
    OpenAWordDocument "your.doc", "N:\YourPath"
End Sub
Public Sub OpenAWordDocument(Optional ByVal DocName As String = "", Optional ByVal DocPath As String = "")
    If Len(Trim(DocName)) = 0 Then
        DocName = "Word Document.docx"
    End If
    If Len(Trim(DocPath)) = 0 Then
        DocPath = "D:\Documentation"
    End If
    If Right(DocPath, 1) <> Application.PathSeparator Then
        DocPath = DocPath & Application.PathSeparator
    End If
    'Open an existing Word Document from Excel
    Set mobjWord = CreateObject("Word.Application")
    With mobjWord
        .Visible = True
        Set mobjDoc = .Documents.Open(DocPath & DocName)
    End With
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
J2FAuthor Commented:
Works like a charm!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office Productivity

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.