2017-01-16 46 views
0

我正在寫一個Excel宏以下算法:的Excel本身做一個文件覆蓋

步驟:在Outlook

2)下載附件

1)循環通過電子郵件(Excel文件)到一個特定的文件夾

3)打開Excel儀表盤模板

4)打開保存附件(excel文件)

5)從連接複製數據到模板

6)關閉附件

7)在模板調整數據

8)保存模板作爲新的工作簿

9)轉到步驟3並繼續下一步附件

我的問題是,在第6步中文件(附件)並沒有完全關閉 - 它仍然可以在項目瀏覽器中看到,所以每次我薩文g模板擅長作爲新的Dashboard.xlsx,它立即用附件覆蓋它。我已經搜索瞭解決方案,但是我發現的所有內容都是在附件上使用workbooks.close,這對我不起作用。

我發現完全關閉附件的唯一方法是關閉模板文件,因此看起來它們以某種方式連接。

下面是代碼:

' this code gets called by a macro that downloads the attachments 
Sub update_WB() 

Dim main_book, att_book As Workbook 
Dim lastrow, firstrow As Long 
Dim att_name as string 

Workbooks.Open "../template.xlsx" ‘<- I have shorten the path for the purpose of posting 

Set main_book = Application.ActiveWorkbook 

main_book.Worksheets("Raw Data").Activate 
main_book.Worksheets("Raw Data").Cells.Select 
Selection.ClearContents 
Selection.UnMerge 

Application.Workbooks.Open "../attachment.xlsx" ‘<- I have shorten the path for the purpose of posting 

Set att_book = ActiveWorkbook 

att_book.Worksheets(1).Range("A:BD").Select 

Selection.Copy main_book.Worksheets("Raw Data").Range("A:BD") 

att_book.Close ‘<- this is where the attachment should close, but it does not. It only disappears from windows taskbar. 

main_book.Worksheets("Raw Data").Activate 

lastrow = Worksheets("Raw Data").Cells(Worksheets("Raw Data").Rows.Count, "A").End(xlUp).Row - 1 
For firstrow = 1 To 100 
    If Worksheets("Raw Data").Cells(firstrow, 1).Text = "Date" Then Exit For 
Next firstrow 

main_book.Worksheets("Raw Data").Activate 
main_book.Worksheets("Raw Data").Cells.Select 
Selection.UnMerge 

main_book.Worksheets("Raw Data").Range("A" & firstrow & ":BG" & lastrow - 1).Name = "Raw_Data" 


‘. 
‘. 
‘. 
‘. Some data manipulation –> copy, paste, delete, etc. 
‘. 
‘. 
‘. 
‘.  

    Application.DisplayAlerts = False 
    main_book.SaveAs (「../Dashboard_" & Format(Timeserial(hour(now()),minute(now()),second(now())),"hhmmss") & ".xlsx") ‘<- I have shorten the path for the purpose of posting. This is where excel does the saving twice – first it saved the main_book and then att_book, both under the same name. 
    Application.DisplayAlerts = True 
main_book.Close '<- this is where both of the files close entirely 

End Sub 

回答

1

此時應更換以下 -

Workbooks.Open "../template.xlsx" ‘<- I have shorten the path for the purpose of posting 

Set main_book = Application.ActiveWorkbook 

隨着

Set main_book = Workbooks.Add(Template:="../template.xlsx") 

,創建一個新的WB出來的模板,而不是剛剛開放模板文件。

編輯:!

還要考慮

Application.Workbooks.Open "../attachment.xlsx" ‘<- I have shorten the path for the purpose of posting 

Set att_book = ActiveWorkbook 

- >

Set att_book = Application.Workbooks.Open "../attachment.xlsx" 

這避免了無意的請示不正確打開的書。

編輯2:代碼,運行的工作部件,並保存沒有錯誤

Sub update_WB() 

Dim main_book, att_book As Workbook 
Dim lastrow, firstrow As Long 
Dim att_name As String 

Set main_book = Workbooks.Add(Template:="template.xlsx") 'Corrected 



main_book.Worksheets("Raw Data").Activate 
main_book.Worksheets("Raw Data").Cells.Select 
Selection.ClearContents 
Selection.UnMerge 

Set att_book = Application.Workbooks.Open("attachment.xlsx") 'Corrected 



att_book.Worksheets(1).Range("A:BD").Select 

Selection.Copy main_book.Worksheets("Raw Data").Range("A:BD") 

att_book.Close 
Set att_book = Nothing 'Added pointer reset, ultimately closes the workbook 

main_book.Worksheets("Raw Data").Activate 

lastrow = Worksheets("Raw Data").Cells(Worksheets("Raw Data").Rows.Count, "A").End(xlUp).Row - 1 
For firstrow = 1 To 100 
    If Worksheets("Raw Data").Cells(firstrow, 1).Text = "Date" Then Exit For 
Next firstrow 

main_book.Worksheets("Raw Data").Activate 
main_book.Worksheets("Raw Data").Cells.Select 
Selection.UnMerge 


    'Application.DisplayAlerts = False 
    main_book.SaveAs ("Dashboard_" & Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hhmmss") & ".xlsx") ''<- I have shorten the path for the purpose of posting. This is where excel does the saving twice – first it saved the main_book and then att_book, both under the same name. 
    'Application.DisplayAlerts = True 
main_book.Close 
Set mainbook = Nothing 'Ultimately closes the template 

End Sub 
+0

感謝您的答覆,但遺憾的是它沒有工作,保存的文件是由Attachment.xlsx覆蓋。 – Wujaszkun

+0

我細化了我的答案,請注意,您還通過活動工作簿在附件中提到了活動工作簿。這種類型的分配很容易出現錯誤,因爲沒有明確指向某個打開的文件。 –

+0

仍然一樣。我嘗試了以下所有組合,但它沒有奏效。: '設置main_book = Workbooks.Open(模板:= 「../ template.xlsx 」)' '設置main_book = Workbooks.Add(模板:=「 ../ template.xlsx」)' '設置att_book = Application.Workbooks.Open「../ attachment.xlsx」' 'Set att_book = Application.Workbooks.Add「../ attachment.xlsx」' 事情是當我有附件已經保存在我的桌面,然後重新啓動應用程序,宏的錯誤部分完美工作。這實際上是我現在使用的解決方法 - 下載後重新啓動整個事情。 – Wujaszkun