2017-07-18 94 views
0

我有一个形式,是向用户发送完成并提交电子,这真的只是将自动发送的电子邮件与文档作为附件。为了将文档附加到电子邮件中,它首先会自动保存到用户桌面。我想要做的是找到一种方法来删除提交后的文档。我发现了一些例子在线,但没有一个为我工作,坦白说,我不看,如果你有后跟该线路的线路ActiveDocument.Close False删除的文件路径将如何工作,文档关闭和宏不再运行就我所能想象的那样。删除Word中的ActiveDocument 2013

我找到无人盯防的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,我不知道,如果上述可能在早期版本的工作的例子,只是不是我的,这可能吗?

感谢您的任何帮助。

UPDATE 目前,我正在尝试一种解决方法,其中文档将节省只读文件,用户发送的附件后,这样,他们可以节省他们的记录,我想。虽然保护我在工作簿设置我让它只读不适用,取而代之的则是恢复到形式仅原始文件的保护,我无法找出原因。

`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文档。

回答

0

此代码将执行以下操作:

  • 打开新的Word实例
  • 宏复制内容使文档到Word
  • 的新实例保存新的实例文档的临时位置
  • 您需要添加电子邮件等的代码
  • 然后它会从临时位置删除新的实例word文档
  • 关闭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 
+0

这并没有删除Temp文件夹中的文件。这与上面添加的另一行保存的代码基本相同,已将其添加到代码中以发送电子邮件附件(它必须在发送前保存)。所有代码在关闭文档时停止,在这种情况下,在'FormDocument.Close'行。此后文件不会通过'Kill'​​删除,应用程序也不会关闭。我再次假设,因为包含VBA代码的文档本身已经退出。那么,有没有人知道这是否与2013年不同? – Awill

+0

@Awill请尝试我更新的anser –

+0

@Awill,只要记住不要因为您的要求或想法更改而发展您的问题。你的问题是**删除已被回答的活动文档单词2013 **。另一个问题是保存然后发送原始文件或保存为pdf等012等等 –