Link to home
Start Free TrialLog in
Avatar of rdy123 rdy
rdy123 rdy

asked on

macro for closing opened workbook

Hi,

i have a macro as below, it opens the word document as pastes in outlook. i want to close whole word document without saving. i have written this as well,but i am getting a popup  "would you like to keep the last item copied? if so,it may take us a bit longer to exit.
YES NO" it should be No. i have used wd.quit but the document is not closing. how I can close the document without leaving this blank application window?
please suggest.


Sub emailFromDoc(fileLocation As String, x As Variant, y As Variant)
Dim wd As Object, editor As Object
Dim doc As Object
Dim Omail As Object
Dim objOutlook As Object
Dim CompanyName As String

On Error Resume Next
Set wd = GetObject(, "Word.Application")
Set doc = wd.Documents("Followup1.docx")
On Error GoTo 0

If wd Is Nothing Then
    Set wd = CreateObject("Word.Application")
    Set doc = wd.Documents.Open("fileLocation")
    wd.Visible = True
End If
doc.Content.Copy
doc.Close savechanges:=False

Set objOutlook = CreateObject("Outlook.Application")
 
Set Omail = objOutlook.CreateItem(olMailItem)

With Omail
    .HTMLBody = Replace(.HTMLBody, "<<Company1>>", CompanyName)
    Set editor = .GetInspector.WordEditor
    editor.Content.Paste
    .Display
wd.Quit
Set wd = Nothing

End With
End Sub
Avatar of Rgonzo1971
Rgonzo1971

HI,

pls try
Sub emailFromDoc(fileLocation As String, x As Variant, y As Variant)
 Dim wd As Object, editor As Object
 Dim doc As Object
 Dim Omail As Object
 Dim objOutlook As Object
 Dim CompanyName As String

 On Error Resume Next
 Set wd = GetObject(, "Word.Application")
 Set doc = wd.Documents("Followup1.docx")
 On Error GoTo 0

 If wd Is Nothing Then
     Set wd = CreateObject("Word.Application")
     Set doc = wd.Documents.Open("fileLocation")
     wd.Visible = True
 End If
 doc.Content.Copy
 wd.DisplayAlerts = wdAlertsNone
 doc.Close savechanges:=False
 wd.DisplayAlerts = wdAlertsAll

 Set objOutlook = CreateObject("Outlook.Application")
  
 Set Omail = objOutlook.CreateItem(olMailItem)

 With Omail
     .HTMLBody = Replace(.HTMLBody, "<<Company1>>", CompanyName)
     Set editor = .GetInspector.WordEditor
     editor.Content.Paste
     .Display
 wd.Quit
 Set wd = Nothing

 End With
 End Sub

Open in new window

Regards
Avatar of rdy123 rdy

ASKER

Thank you,but i am still getting the same pop up,i don't want to get that pop up and it should close the whole word document. please suggest.
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
still getting the same,no idea why..
then try
with reference to ms.forms
Sub emailFromDoc(fileLocation As String, x As Variant, y As Variant)
 Dim wd As Object, editor As Object
 Dim doc As Object
 Dim Omail As Object
 Dim objOutlook As Object
 Dim CompanyName As String

 On Error Resume Next
 Set wd = GetObject(, "Word.Application")
 Set doc = wd.Documents("Followup1.docx")
 On Error GoTo 0

 If wd Is Nothing Then
     Set wd = CreateObject("Word.Application")
     Set doc = wd.Documents.Open("fileLocation")
     wd.Visible = True
 End If
 doc.Content.Copy
 wd.Visible = False

 Set objOutlook = CreateObject("Outlook.Application")
  
 Set Omail = objOutlook.CreateItem(olMailItem)

 With Omail
     .HTMLBody = Replace(.HTMLBody, "<<Company1>>", CompanyName)
     Set editor = .GetInspector.WordEditor
     editor.Content.Paste
     .Display
 End With
Dim MyData   As DataObject
Set MyData = New DataObject
MyData.SetText ""
MyData.PutInClipboard
doc.Close
 doc.Close savechanges:=False
 wd.Quit
 Set wd = Nothing

 End Sub

Open in new window

its my excel issue,its working,thank you