删除Excel文件

时间:2015-08-14 14:46:16

标签: excel excel-vba vba

这让我疯了。我想删除文件夹中的xlsm文件。很确定我使用的语法正确。 debug.print表明我有正确的路径和名称。那么为什么它不起作用; _;

我想也许是因为if statement不会被触发,所以我在循环之后添加了另一个kill,但确实如此。也就是说,最后debug.print没有被触发。循环后出现代码停止。怎么样?

不再详细介绍,此代码实际上是从另一个工作簿导入的。那有关系吗?我不这么认为。

我发布此内容后,尝试硬编码路径。如果这样可行,我将真的难倒XD

Private Sub Workbook_Open()
       Dim bookPath As String
       For Each wkb In Application.Workbooks
            If Not wkb.Name = ThisWorkbook.Name And Not wkb.Name = "PERSONAL.XLSB" Then
                Debug.Print wkb.Name
                n = wkb.Name
                bookPath = wkb.Path & "\" & n
                Debug.Print bookPath
                wkb.Close Savechanges:=False
                Kill bookPath
            End If
       Next wkb
                Kill bookPath
                Debug.Print bookPath
    End Sub

编辑:硬编码有效。为什么?????? ; ___;

编辑2: 为了讲述整个故事,我正在使用一本书来创建一本新书,将上面的代码转储到新书中,保存/关闭新书然后重新打开它以便触发我刚刚转入的workbook_open事件一切正常,直到新书打开,触发事件,开始循环并关闭开始整个过程​​的原始书。也就是说,代码在

处死掉
wkb.close Savechanges:=false

疯了。请帮助XD(忽略msgboxes。将它们用作调试工具)。

Private Sub Workbook_Open()    
'this goes inside the "thisworkbook" module of the first book
Dim codeMod As CodeModule
Dim code As String
Dim wb2 As Workbook
Dim wb3 As Workbook
Dim book2 As String
Dim bPath As String
Dim countDown As Long

countDown = 1

start = Timer

Do While Timer < start + countDown
DoEvents
Loop

'Application.ScreenUpdating = False

'wb2 is the new book that will close all books then delete them
Set wb2 = Workbooks.Add
bPath = ThisWorkbook.path & "\"
MsgBox ("opening new workbook")

Set codeMod = wb2.VBProject.VBComponents("ThisWorkbook").CodeModule

If codeMod.CountOfLines > 0 Then
    codeMod.DeleteLines 1, codeMod.CountOfLines
End If

code = _
"Private Sub Workbook_Open()" & vbNewLine & _
"   Dim bookPath As String" & vbNewLine & _
"   For Each wkb In Application.Workbooks" & vbNewLine & _
"        If Not wkb.Name = ThisWorkbook.Name And Not wkb.Name = ""PERSONAL.XLSB"" Then" & vbNewLine & _
"            debug.print wkb.name" & vbNewLine & _
"            n = wkb.Name" & vbNewLine & _
"            bookPath = wkb.path & ""\"" & n" & vbNewLine & _
"            'debug.print bookPath" & vbNewLine & _
"            MsgBox (""closed testing"")" & vbNewLine & _
"            ThisWorkbook.Activate" & vbNewLine & _
"            wkb.Close Savechanges:=False" & vbNewLine & _
"            ThisWorkbook.Activate" & vbNewLine & _
"            MsgBox (""closed testing"")" & vbNewLine & _
"            Kill bookPath" & vbNewLine & _
"            debug.print bookPath" & vbNewLine & _
"        End If" & vbNewLine & _
"   Next wkb" & vbNewLine & _
"            'Kill bookPath" & vbNewLine & _
"            debug.print n & bookPath" & vbNewLine & _
"End Sub"

With codeMod
    .InsertLines .CountOfLines + 1, code
End With

'code dumped. now save wb2 as killbook
wb2.SaveAs Filename:=bPath & "killbook" & ".XLSM", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

'need to close it so it will trigget workbook_open event
wb2.Close

MsgBox ("closed new workbook")

'set book2 to wb2/killbook
book2 = bPath & "killbook" & ".XLSM"
'wb3 is actually opening wb2/killbook to trigger the workbook_open event
Set wb3 = Workbooks.Open(book2)

'Application.ScreenUpdating = True
End Sub

编辑3:如果我只是打开新书(killbook)就行了......很奇怪:/

0 个答案:

没有答案