删除activedocument Word 2013

时间:2017-07-18 17:03:53

标签: vba word-vba

我有一个表单,发送给用户完成并以电子方式提交,这实际上只是发送一个自动电子邮件,其中文档作为附件。为了将文档附加到电子邮件中,它首先会自动保存到用户桌面。我想要做的是找到一种在提交后删除该文档的方法。我在网上找到了一些例子,但没有一个对我有用,坦率地说,如果你有一行ActiveDocument.Close False后跟行删除文件路径,文档关闭和宏,我就看不出它会如何工作已经不再像我想象的那样跑了。

我发现另一位用户的问题有1个未标记的答案:Delete doc when macro finishes

 Sub DeleteCurrentDoc()

    Dim Doc1 As Document
    Dim deletepath As String


    'get path of this document to delete it later
    deletepath = ActiveDocument.FullName

    'Close the document we are going to delete (Word should remain Open
    ActiveDocument.Close False

    'Delete it
    Kill (deletepath)

    'Tidy up and close Word (Optional Line, delete if necessary)
    Application.Quit

    End Sub

和word.tips.net上的另一个: https://wordribbon.tips.net/T011642_Deleting_the_Open_Document_File

Sub DeleteThisFile()
    Dim MyFile As String

    MyFile = ActiveDocument.Path & "\" & ActiveDocument.Name
    If MsgBox(MyFile & " will be deleted permanently", _
      vbYesNo, "Delete this File?") = vbYes Then
        ActiveDocument.Close (wdDoNotSaveChanges)
        Kill MyFile
    End If
End Sub

我试着查看一个代码来打开一个新文档并每次都添加一个宏,但我觉得它正在进入一些不受欢迎的领域。我真的想找到一种方法从用户的电脑上删除文件,因为他们只能根据请求访问单个表单。

我正在使用Word 2013,我不确定上面的例子是否可以在早期版本中使用而不是我的,这可能吗?

谢谢你的帮助。

更新 我正在尝试一种解决方法,在发送附件后,文档将保存为用户的只读文件,这样他们就可以保存他们的记录了。虽然我在工作簿中设置的保护只是为了使它只是应用,而是恢复到仅保护原始文件的形式,我找不到原因。

`ActiveDocument.SaveAs2 FileName:="C:\Users\" & curUser & "\Desktop\My User Request_" & _
Format(Date, "mm.dd.yy") & ".docx", fileformat:=wdFormatDocumentDefault
Selection.HomeKey wdStory
With ActiveDocument
    .Protect 3, Password:="password"
    .Save
End With`

我在这里做的是使用不同的文件名重新保存它并作为非宏启用的工作簿,然后删除我最初保存到桌面所需的临时文件以发送附件,只留下它们他们填写的表格的副本,他们不能再对其进行任何更改。

我尝试了几种方法,我添加了保护,然后执行了保存,每次打开docx时,保护仍然只是形式而不是只读。

理想情况下,我真的想知道如何成功删除activedocument,以便将pdf副本保存到用户桌面,然后删除Word文档。

1 个答案:

答案 0 :(得分:0)

此代码将执行以下操作:

  • 打开Word的新实例
  • 将宏启用文档的内容复制到新的单词
  • 将新实例文档保存在临时位置
  • 您需要添加电子邮件等代码
  • 然后它会从临时位置删除新的实例word doc
  • 关闭新单词
  • 关闭宏启用word doc

请参阅以下代码:

    Option Explicit

    Sub SaveAndDeleteBeforeClosing()

        'Tested and working well with Office 2010

        Dim FormDocument As Document
        'Activedocument can be changed to reference a specific document name.
        'Set FormDocument = Documents("Some Doc Name")
        Set FormDocument = ActiveDocument

        'Opening new instance of Word
        Dim WordProg As Word.Application
        Set WordProg = CreateObject("Word.Application")
        WordProg.Application.Visible = False

        'Adding a new document toi the new instance of Word
        WordProg.Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
        Dim WordDoc As Document
        Set WordDoc = WordProg.Documents(WordProg.Documents.Count)

        'Copy contents of Macro Document to new document
        FormDocument.Content.Copy
        WordDoc.Range.Paste

        'Use this to as a sanity check to see if everything is copied over correctly
        'otherwsie you will need to play around with the paste options
        WordProg.Visible = True

        'Saving New instance word doc to temp location

        Dim FileNameString As String
        'Enter your desired file name here
        FileNameString = "Some Doc Name"
        Dim FilePathString As String
        'Temp file for deleting
        FilePathString = "C:\Temp\" & FileNameString & ".docx"

        WordDoc.SaveAs2 FileName:=FilePathString, FileFormat:= _
            wdFormatDocumentDefault, LockComments:=False, Password:="", AddToRecentFiles _
            :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
            :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
            SaveAsAOCELetter:=False, CompatibilityMode:=14

        'Closing new instance word document
        WordDoc.Close

        'Some code to send the file as a email
        '
        '
        '
        '
        '
        'delete the file from the C:\Temp\ folder
        Kill (FilePathString)

        'Quitting the temp word application
        WordProg.Application.Quit
        'Quitting the word application inwhich the macro is stored
        Application.Quit False

    End Sub
相关问题