kill file after emiled

  • Thread starter Thread starter jatman
  • Start date Start date
J

jatman

good afternoon,

back again doing tweaking on macro...

i have a template, with a macro that saves the file in c:/temporary/ as a
docx file (Office 2007)

then it emails out (as a docx attachment)

then it saves a user copy in the proper location c:/human_resources as a pdf
file so that the user cannot make changes to it anymore

closes down

i want to permanently kill/delete the file in c:/temporary/

i tried
Kill "C:\Temporary\" & "*.do*"
but that does not remove the current file that i just worked on, only the
old files are removed. i also tried a separte private sub document_close and
put the kill line there but i got an error message.

how would i remove the currently saved file?

thanx

jat
 
You must close the active file before you can delete it eg

Sub Send_As_Mail_Attachment()

' send the document as an attachment _
in an Outlook Email message
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

'Save the document in the temporary folder with the name 'filename.docx'
ActiveDocument.SaveAs "c:\temporary\Filename.docx"

'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")

'Outlook wasn't running, start it from code
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.to = "(e-mail address removed)"
.Subject = "This is the subject"
'Add the document as an attachment, you can use the _
.displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, _
Type:=olByValue, DisplayName:="Document as attachment"
.Body = "See attached document"
'.Display 'show the dialog
.send
End With

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing

'Save PDF version using Word 2007 PDF add-in
'with the name 'filename.pdf'
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"c:\human_resources\Filename.pdf", ExportFormat _
:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True,
_
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True,
_
BitmapMissingFonts:=True, UseISO19005_1:=True
'Close the active document
ActiveDocument.Close
'Remove the temporary file
Kill "c:\temporary\filename.docx"
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
good morning Graham,

first - thank you, because most of this word macro is from you. i got the
message boxes working all by myself so i am proud of that fact!

as for the kill command, it does not work. i have the code for the macro as
follows:Private Sub CommandButton1_Click()

Dim fname As String
With ActiveDocument
fname = Format(Date, "yyyy-mm-dd") & " " &
..BuiltInDocumentProperties("Title") & " Change of Status "
.SaveAs "C:\Operations\Temporary Files\" & fname & ".docx",
fileformat:=wdFormatDocumentDefault

MsgBox "You may be prompted for your email password, enter your password
and make sure you open Outlook to send the Change of Status.", vbExclamation

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

ActiveDocument.SaveAs "C:\Operations\Temporary Files\" & fname & ".docx"

Set oOutlookApp = GetObject(, "Outlook.Application")

If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "(e-mail address removed)"
.Subject = fname & " Store " &
ActiveDocument.BuiltInDocumentProperties("Company")
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue,
DisplayName:="Document as attachment"
.Body = "See attached document"
.Send
End With

Set oItem = Nothing
Set oOutlookApp = Nothing

ActiveDocument.ExportAsFixedFormat OutputFileName:="C:\Operations\Human
Resources\" & fname & ".pdf", ExportFormat _
:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=True

Application.DisplayAlerts = wdAlertsNone
ActiveDocument.Close

Kill "C:\Operations\Temporary Files\" & fname & " .docx"
End With
End Sub

i made some minor changes to the original codes that were provided from what
i learned through trial and error - also allowed me to learn a few things.
(i remvoed all the 'comments) everything works but the kill.... i also
tried the kill before the activedocument.close but that did not work either.

thank you for your help.

jat
 
The file is saved as
..SaveAs "C:\Operations\Temporary Files\" & fname & ".docx"
but you are trying to kill
Kill "C:\Operations\Temporary Files\" & fname & " .docx"
The two will never match so Kill will never find the file to remove.
Clue: try removing the space in " .docx"

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
no, that did not work either. i think i know what the problem is. the kill
command comes after the file closes, so it can never be executed. if i put
the kill command before the close, then all other files are killed, except
the active document.

in Excel (macro in template) i use the macro to make a copy of the active
document and the copy becomes the active document. the macro then does it's
thing (mails, etc...) closes the copy of the active document (the saved copy
in the temporary folder) and reverts back to the template, then the template
sends the kill command.

in excel, the code is

with activesheet
activesheet.copy
set destwb=activeworkbook

with destwb
'then the rest of the code would be executed...


in word, when i try with activedocument.copy, there is no copy that comes,
only copystylesfrom template (is that the same as making a copy.)

jat
 
It won't work if you have the macro in the document, but if you put it in
the document template or a global template it will work.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
without giving up, i have made it this far...

Sub MakeCopy()
' make me a copy of the current form
Selection.WholeStory
Selection.Copy
Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
Selection.PasteAndFormat (wdPasteDefault)
End Sub

only one problem, it will not copy the information that has been entered
into the fields/properties.

oh well, it was a good run while it lasted.

thank you for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top