Vba 删除activedocument Word 2013

Vba 删除activedocument Word 2013,vba,ms-word,Vba,Ms Word,我有一个表单,发送给用户完成并以电子方式提交,它实际上只是发送一封自动电子邮件,并将文档作为附件。为了在电子邮件中附加文档,它首先会自动保存到用户桌面。我想做的是找到一种在提交后删除此文档的方法。我在网上找到了一些示例,但没有一个对我有效。坦白地说,如果有一行ActiveDocument.Close False后跟删除文件路径的行,文档将关闭,宏将不再按照我的想象运行,我看不出它会如何工作 我发现另一个用户的问题有1个未标记的答案: 另一个在word.tips.net上: 我试图查看一段代码来

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

我发现另一个用户的问题有1个未标记的答案:

另一个在word.tips.net上:

我试图查看一段代码来打开一个新文档,每次都向其中添加一个宏,但我觉得这会进入一些不需要的领域。我真的很想找到一种方法,从用户的pc删除该文件,但是,因为他们只能访问一个单一的形式的要求

我使用的是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文档。

此代码将执行以下操作:

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

    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

这不会从临时文件夹中删除该文件。这与上面的代码基本相同,添加了要另存为的行,该行已内置于发送电子邮件附件的代码中(必须在发送之前保存)。所有代码在关闭文档时停止,在本例中,在第
FormDocument.Close行。此后,文件不会通过
Kill
删除,应用程序也不会关闭。我再次假设,因为包含VBA代码本身的文档此时已退出。那么,有人知道这与2013年是否有所不同吗?@Awill,记住不要随着需求或想法的变化而不断演变你的问题。您的问题是删除activedocument word 2013,该问题已得到回答。另一个问题是保存并发送原始文件,或者在一定程度上另存为pdf等,尽管它不会删除活动文档,我提供的示例编码声称在早期版本的Word中成功地删除了该文档。
`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`
    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