【问题标题】:Delete activedocument Word 2013删除活动文档 Word 2013
【发布时间】:2017-12-23 16:35:37
【问题描述】:

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

我找到了 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 时,保护仍然是表单而不是只读。

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

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    此代码将执行以下操作:

    • 打开 Word 的新实例
    • 将宏启用文档的内容复制到新的单词实例中
    • 将新的实例文档保存在临时位置
    • 然后您需要添加用于发送电子邮件等的代码
    • 然后它将从临时位置删除新的实例单词 doc
    • 关闭 word 的新实例
    • 关闭宏启用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
    

    【讨论】:

    • 这并未从 TEMP 文件夹中删除文件。这与上面的代码基本相同,但添加了要另存为的行,该行已内置到发送电子邮件附件的代码中(必须在发送前保存)。所有代码在关闭文档时停止,在本例中为 FormDocument.Close 行。此后,文件不会通过Kill 删除,应用程序也不会关闭。我再次假设,因为此时包含 VBA 代码本身的文档已退出。那么又有谁知道这是否与 2013 年不同?
    • @Awill,请记住不要随着您的要求或想法的变化而演变您的问题。您的问题是删除 activedocument word 2013,已得到解答。另一个问题是保存然后发送原始文件或另存为pdf等
    • 在一定程度上足够真实,尽管它不会删除我提供的示例编码声称在早期版本的 Word 中成功完成的活动文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多