2016-04-25 44 views
8

我應該從Excel導出一些大的數據範圍到Powerpoint,每張幻燈片一頁,當然我應該處理分頁符以避免「孤行」行或列。如何「強制」Excel來正確計數分頁符?

我想通過閱讀HPageBreaks.Count和VPageBreaks.Count來檢查有多少頁面可以縱向和橫向使用給定的縮放比例,然後手動定義每個中斷的位置。這個想法是在每個頁面上具有大致相同的寬度和高度。

當我一步一步調試我的代碼時,它運行良好,邏輯看起來不錯,但如果我「自由」運行它,分頁符完全關閉。添加一些MsgBox指令,我可以看到,當我讀取HPageBreaks.Count(或垂直)時,我得到錯誤的值。 (如果我手動執行代碼應該做的事,我可以檢查正確的)。

在許多論壇上搜索,我看到一些醜陋的解決方法,如強制重置PaperSize(ws.PageSetup.PaperSize = ws.PageSetup.PaperSize)。在嘗試了一些之後,看起來好一點的是在更改PageSetup之前關閉PrintCommunication,然後再打開它。這在我的大多數表單上運行良好,但在真正大的表單上(大約750行x 80列,幾乎所有帶公式的單元格),它都沒有。

下面的代碼的摘錄:

'Reset page breaks 
    .ResetAllPageBreaks 

    'Set minimum acceptable zoom factor 
    Application.PrintCommunication = False 'This is the ugly workaround 
    .PageSetup.Zoom = 60 
    Application.PrintCommunication = True 

    MsgBox "Zoom = " & .PageSetup.Zoom 'Only for debugging 

    'Calculate the number of pages in width 
    Application.PrintCommunication = False 
    NPagesWide = .VPageBreaks.Count + 1 
    Application.PrintCommunication = True 

    MsgBox "NPagesWide = " & NPagesWide 

    'Find the higher zoom factor that can fit that number of pages 
    Application.PrintCommunication = False 
    .PageSetup.Zoom = 100 
    Application.PrintCommunication = True 
    Do While .VPageBreaks.Count > NPagesWide - 1 
     Application.PrintCommunication = False 
     .PageSetup.Zoom = .PageSetup.Zoom - 5 
     Application.PrintCommunication = True 
    Loop 

    MsgBox "Zoom = " & .PageSetup.Zoom 

    'Set average width per page and initialize column pointer 
    If HasTitleColumns Then  'Defined earlier 
     PageWidth = (PrintArea.Width + TitleColumns.Width * (NPagesWide - 1))/NPagesWide 
     j = TitleColumns.Columns(TitleColumns.Columns.Count).Column + 1 
    Else 
     PageWidth = PrintArea.Width/NPagesWide 
     j = 1 
    End If 

    'Cycle vertical page breaks 
    For i = 1 To NPagesWide - 1 
     'Set width of TitleColumns 
     If HasTitleColumns Then 
      CumulWidth = TitleColumns.Width 
     Else 
      CumulWidth = 0 
     End If 
     'Cumulate columns width until the available page width 
     Do While CumulWidth + .Columns(j).Width <= PageWidth 
      CumulWidth = CumulWidth + .Columns(j).Width 
      j = j + 1 
     Loop 
     'Add the break 
     .VPageBreaks.Add .Columns(j + 1) 
    Next i 

任何想法,爲什麼發生這種情況,我怎麼能解決呢?

感謝,

+0

沒有想法?任何批評,將不勝感激! – Edgard

+0

您是否嘗試在更改縮放的行後添加「DoEvents」?通常我不建議這樣做,但是在這些情況下,在採取下一個命令之前似乎沒有完成這個動作,它會將我從一些無法解決的奧祕中解救出來。如果它有效,我可以將其作爲答案。 – ib11

回答

2

我建議一般建議當同時擊中在調試模式F8 VBA代碼工作正常的問題,但它不會不按F5來運行整個宏觀後工作。

提示1.儘可能使用ThisWorkbook.ActiveSheet而不是ActiveWorkbook.ActiveSheet來引用正確的工作表。使用ThisWorkbook.Application而不是Application.這可能是因爲您在後臺運行了另一個Addin程序,請將ActiveSheet切換爲您可能未意識到的其他內容。檢查其他宏啓用並擺脫任何你不使用。因此,在代碼中的任何重要內容之前,請嘗試使用ThisWorkbook.Activate來獲取表單的焦點。請參閱該頁上的圖表:http://analystcave.com/vba-tip-day-activeworkbook-vs-thisworkbook/

提示2.強制Excel以不同的方式等待,然後DoEvents

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'Place this line of code at the very top of module 
Sleep 1 'This will pause execution of your program for 1 ms 

​​ https://www.fmsinc.com/microsoftaccess/modules/examples/AvoidDoEvents.asp

或者:

Application.Calculate 
If Not Application.CalculationState = xlDone Then 
    DoEvents 
End If 

https://stackoverflow.com/a/11277152/1903793

提示3.如果以上仍不能用於刷新使用工作:

ThisWorkbook.Connections("ConectionName").Refresh 
ThisWorkbook.Application.CalculateUntilAsyncQueriesDone 

https://stackoverflow.com/a/26780134/1903793