2017-06-27 137 views
0

我有一個打開,編輯和保存文件的宏。但這需要一些時間,因此會出現進度條窗口。我不知道如何擺脫它們。我試過了:Excel vba隱藏文件打開和文件保存窗口

Application.ScreenUpdating = False 
    Application.DisplayStatusBar = False 
    Application.DisplayAlerts = False 

但它不起作用。有什麼建議?

編輯:這裏是我的代碼:

我嘗試了建議答案here,但它不工作(這一切只是減慢和輸出文件夾爲空)。也許我做錯了?

Sub iterateThroughFolder() 
    '**SETTINGS** 
    Application.ScreenUpdating = False 
    Application.DisplayStatusBar = False 
    Application.StatusBar = "Starting..." 

    '**VARIABLES** 
    Dim folderPath As String 
    folderPath = "Y:\vba\test_reserves\test_data\" 

    '**INVISIBLE APPLICATION** <---- the part from other answer 
    ' Dim app As New Excel.Application 
    Dim app As Object 
    Set app = CreateObject("Excel.Application") 
    app.Visible = False 

    '**LOOPING THROUGH THE FOLDER** 
    fileTitle = Dir(folderPath & "*.xl??") 
    Do While fileTitle <> "" 
     'SETTINGS 
     Application.DisplayAlerts = False 
     Application.StatusBar = fileTitle + "pending: " 

     'OPENING FILES 
     Dim resultWorkbook As Workbook 
     Dim dataWorkbook As Workbook 

     'OLD VARIANT: 
     'Set resultWorkbook = 
      'Workbooks.Open("Y:\vba\test_reserves\files\rating_template.xls") 
     'Set dataWorkbook = Workbooks.Open(folderPath & fileTitle) 

     'NEW VARIANT: 
     'Set resultWorkbook = app.Workbooks.Add("Y:\vba\test_reserves\files\rating_template.xls") 
     ' Set dataWorkbook = app.Workbooks.Add(folderPath & fileTitle) 

     'NEXT NEW VARIANT (from this question's answer): 
     Set resultWorkbook =app.Application.Workbooks.Open 
     ("Y:\vba\test_reserves\files\rating_template.xls ") 
     Set dataWorkbook = app.Application.Workbooks.Open(folderPath & 
     fileTitle) 

     'REFRESHING CONNECTIONS (?) 
     Dim cn As WorkbookConnection 
     For Each cn In resultWorkbook.Connections 
       cn.Refresh 
     Next 

     'GETTING THE NAME AND PUTTING IT IN "A1" cell of "B1" list 
     Application.StatusBar = Application.StatusBar + "Getting the state name" 
     Dim stateName As String 
     stateName = dataWorkbook.Worksheets("ðàçäåë 1").Cells(5, 4).Value 
     resultWorkbook.Worksheets("B1").Cells(1, 1).Value = stateName 


     'SAVING AND CLOSING 
     Application.StatusBar = Application.StatusBar + "Saving and closing new rating file" 
     resultWorkbook.SaveAs Filename:="Y:\vba\test_reserves\output\" + stateName 
     resultWorkbook.Close 
     dataWorkbook.Close 
     Application.DisplayAlerts = True 

     'NEXT FILE 
     Application.StatusBar = Application.StatusBar + "Getting next data file" 
     fileTitle = Dir() 
    Loop 
    '**CLEANING THE APP** <--- from another answer, added it, so it doesn't 
    'work now 
    app.Quit 
End Sub 
+0

您是否嘗試過這個問題https://stackoverflow.com/questions/579797/open-excel-file-for-reading-with-vba-without-display – Gowire

+0

'所以進度條窗口appear.'什麼進度條窗口? –

+0

@Siddharth Rout,保存進度,打開進度(文件足夠大,可以顯示)。 – Ans

回答

1

這裏有一些代碼你可以嘗試......它啓動一個隱藏的Excel應用程序並打開一個工作簿。您可以使用WB對象處理此工作簿。

但是...如果操作需要時間(小時玻璃或旋轉光標),您仍然會等待鼠標圖標。

Dim ExcelApp As Object 
Dim WB As Workbook 

Set ExcelApp = CreateObject("Excel.Application") 
Set WB = ExcelApp.Application.Workbooks.Open("myfile.xlsx") 

WB.DoYourStuffWithTheWorkbook 

WB.Save 
WB.Close 
+0

它不工作;(絆倒開放它,只是認爲,並認爲... – Ans

+0

我編輯我的問題,包括您的代碼。 – Ans