Problem quitting Word after extracting OLE document

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to extract a Word OLE document, write to it, then save it as a pdf document;

Code:
'Extract File
If ActiveWorkbook Is Nothing Then Exit Sub
Application.ScreenUpdating = False
DestFolder = ThisWorkbook.Path & "\Toolbox Files\121 Files"
Set wsActive = ActiveSheet
lCnt = 0
Sheets("Setup").Activate
Set ws = Sheets("Setup")
For Each oOleObj In ws.OLEObjects
If oOleObj.OLEType = xlOLEEmbed Then
sDocType = oOleObj.progID
sDocType = Left(sDocType, InStr(sDocType, ".") - 1)
If sDocType = "Word" Then
oOleObj.Select
oOleObj.Verb xlVerbPrimary
Set oDoc = oOleObj.Object

oDoc.Bookmarks("Staff").Range.InsertAfter NewMeetingFrm.Combobox1.Value
oDoc.Bookmarks("Manager").Range.InsertAfter NewMeetingFrm.TextBox2.Value
oDoc.Bookmarks("MeetingDate").Range.InsertAfter Format(NewMeetingFrm.TextBox3, "dd mmmm yyyy")
oDoc.Bookmarks("Welfare").Range.InsertAfter NewMeetingFrm.txtwelfare.Value
oDoc.Bookmarks("PDR").Range.InsertAfter NewMeetingFrm.txtpdr.Value
oDoc.Bookmarks("Performance").Range.InsertAfter NewMeetingFrm.txtperformance.Value
oDoc.Bookmarks("Competencies").Range.InsertAfter NewMeetingFrm.txtcompetencies.Value
oDoc.Bookmarks("SelfInspection").Range.InsertAfter NewMeetingFrm.txtinspection.Value
oDoc.Bookmarks("PDRS").Range.InsertAfter NewMeetingFrm.txtsupervision.Value
oDoc.Bookmarks("Attendance").Range.InsertAfter NewMeetingFrm.txtattendance.Value
oDoc.Bookmarks("Career").Range.InsertAfter NewMeetingFrm.txtcareer.Value
oDoc.Bookmarks("Project").Range.InsertAfter NewMeetingFrm.txtproject.Value
oDoc.Bookmarks("Comments").Range.InsertAfter NewMeetingFrm.txtcomments.Value
oDoc.Bookmarks("Date").Range.InsertAfter Format(now, "dd mmmm yyyy")
oDoc.Bookmarks("Date2").Range.InsertAfter Format(now, "dd mmmm yyyy")

dt = Format(NewMeetingFrm.TextBox3, "dd_mmmm_yyyy")
strFile = "1-2-1 Meeting Record - " & dt & ".pdf"
oDoc.SaveAs ThisWorkbook.Path & "\Toolbox Files\121 Files\" & strFile, FileFormat:=17

oDoc.Close savechanges:=False
lCnt = lCnt + 1
End If
End If
Next oOleObj
ActiveWindow.WindowState = xlMaximized

It's a bit of a mess because I've been playing around with different bits of code I've found on here.

It works OK, but the issue I'm having is that is leaves an instance of Word open. I need it to close that instance of Word but leave any others open, (ones that may be open when this is run).
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Modify your code...

Code:
'
[COLOR=#ff0000]Dim appWord as Object[/COLOR]

[COLOR=#a9a9a9]'Extract File
If ActiveWorkbook Is Nothing Then Exit Sub
Application.ScreenUpdating = False
DestFolder = ThisWorkbook.Path & "\Toolbox Files\121 Files"
Set wsActive = ActiveSheet
lCnt = 0
Sheets("Setup").Activate
Set ws = Sheets("Setup")
For Each oOleObj In ws.OLEObjects
If oOleObj.OLEType = xlOLEEmbed Then
sDocType = oOleObj.progID
sDocType = Left(sDocType, InStr(sDocType, ".") - 1)
If sDocType = "Word" Then
oOleObj.Select
oOleObj.Verb xlVerbPrimary
Set oDoc = oOleObj.Object[/COLOR][COLOR=#a9a9a9]

[/COLOR][COLOR=#ff0000]If appWord Is Nothing Then
    Set appWord = oDoc.Parent
End If[/COLOR][COLOR=#a9a9a9]

[/COLOR][COLOR=#a9a9a9]oDoc.Bookmarks("Staff").Range.InsertAfter NewMeetingFrm.Combobox1.Value
oDoc.Bookmarks("Manager").Range.InsertAfter NewMeetingFrm.TextBox2.Value
oDoc.Bookmarks("MeetingDate").Range.InsertAfter Format(NewMeetingFrm.TextBox3, "dd mmmm yyyy")
oDoc.Bookmarks("Welfare").Range.InsertAfter NewMeetingFrm.txtwelfare.Value
oDoc.Bookmarks("PDR").Range.InsertAfter NewMeetingFrm.txtpdr.Value
oDoc.Bookmarks("Performance").Range.InsertAfter NewMeetingFrm.txtperformance.Value
oDoc.Bookmarks("Competencies").Range.InsertAfter NewMeetingFrm.txtcompetencies.Value
oDoc.Bookmarks("SelfInspection").Range.InsertAfter NewMeetingFrm.txtinspection.Value
oDoc.Bookmarks("PDRS").Range.InsertAfter NewMeetingFrm.txtsupervision.Value
oDoc.Bookmarks("Attendance").Range.InsertAfter NewMeetingFrm.txtattendance.Value
oDoc.Bookmarks("Career").Range.InsertAfter NewMeetingFrm.txtcareer.Value
oDoc.Bookmarks("Project").Range.InsertAfter NewMeetingFrm.txtproject.Value
oDoc.Bookmarks("Comments").Range.InsertAfter NewMeetingFrm.txtcomments.Value
oDoc.Bookmarks("Date").Range.InsertAfter Format(now, "dd mmmm yyyy")
oDoc.Bookmarks("Date2").Range.InsertAfter Format(now, "dd mmmm yyyy")

dt = Format(NewMeetingFrm.TextBox3, "dd_mmmm_yyyy")
strFile = "1-2-1 Meeting Record - " & dt & ".pdf"
oDoc.SaveAs ThisWorkbook.Path & "\Toolbox Files\121 Files\" & strFile, FileFormat:=17

oDoc.Close savechanges:=False
lCnt = lCnt + 1
End If
End If
Next oOleObj[/COLOR][COLOR=#a9a9a9]

[/COLOR][COLOR=#ff0000]If Not appWord Is Nothing Then
    appWord.Quit
End If[/COLOR][COLOR=#a9a9a9]
[/COLOR][COLOR=#ff0000]Application.ScreenUpdating = True[/COLOR][COLOR=#a9a9a9]

[/COLOR][COLOR=#a9a9a9]ActiveWindow.WindowState = xlMaximized[/COLOR]
[COLOR=#d3d3d3]'
'[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top