如何隐藏ExportAsFixedFormat的发布进度栏

我正在尝试使用interop.excel将Excel文件转换为PDF,同时在网站上执行ExportAsFixedFormat “发布”进度条。 有什么办法可以隐藏这个吗? 我发现这个问题的大小超过300KB的Excel文件。

代码如下:

 //(tried using Application instead of ApplicationClass) Microsoft.Office.Interop.Excel.ApplicationClass excelApplication = new Microsoft.Office.Interop.Excel.ApplicationClass(); excelApplication.ScreenUpdating = false; excelApplication.DisplayAlerts = false; excelApplication.Visible = false; if (excelWorkbook == null) { excelApplication.Quit(); excelApplication = null; excelWorkbook = null; return false; } var exportSuccessful = true; try { excelApplication.AlertBeforeOverwriting = false; excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath); } catch (System.Exception ex) { exportSuccessful = false; } 

我找不到任何解决scheme。 我的项目是一个C#Web应用程序。

我花了几天的时间才弄清楚,但最后这里是一个workarround,它使用一些WinAPI函数来观察Windows事件。 挂钩处于活动状态时,每个新窗口都会与PDF保存对话框类相同。 如果是这样的话,窗口就会隐藏起来。

感谢解决scheme的想法去一些中国家伙: http : //www.itjie.wang/officebase/516998.html

使用要求:
由于WinAPI的使用,操作系统(=操作系统)必须是Windows。

警告:
如果“SetWinEventHook”由于某些错误而没有再次停止,最好重新启动系统,否则可能会遇到一些严重的Windows问题。

注意:
默认情况下,PDF保存对话框不会定期出现。 这取决于保存PDF文件所需的时间。 如果需要更长的时间,保存popup窗口会显示出来。 如果需要更短的保存popup窗口不会显示。 无论如何,你不必担心保存对话框是否会出现,代码已经为你做了这个。

指令:
在您的Excel工作簿中,如果您还没有模块,请创build一个新名称(名称无关紧要)并粘贴以下代码:

 ' WINDOWS API FUNCTIONS: Private Declare Function SetWinEventHook Lib "user32" (ByVal eventMin As Long, ByVal eventMax As Long, ByVal hmodWinEventProc As Long, ByVal pfnWinEventProc As Long, ByVal idProcess As Long, ByVal idThread As Long, ByVal dwFlags As Long) As Long Private Declare Function UnhookWinEvent Lib "user32" (ByVal hWinEventHook As Long) As Long Private Declare Function apiGetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassname As String, ByVal nMaxCount As Long) As Long Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long ' CONSTANT VARIABLES: Private Const SW_HIDE = 0 Private Const DLG_CLSID = "CMsoProgressBarWindow" Private Const EVENT_SYSTEM_FOREGROUND = &H3& Private Const WINEVENT_OUTOFCONTEXT = 0 ' GLOBAL VARIABLES: Dim long_WinEventHook As Long Public Function StartEventHook() As Long long_WinEventHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, 0&, AddressOf WinEventFunc, 0, 0, WINEVENT_OUTOFCONTEXT) StartEventHook = long_WinEventHook End Function Public Sub StopEventHook() Dim b_unhooked As Boolean If long_WinEventHook = 0 Then MsgBox "WinEventHook couldn't be stopped! " & _ "Variable 'long_WinEventHook' is empty! " & _ "Better restart Windows now!" Exit Sub End If b_unhooked = UnhookWinEvent(long_WinEventHook) If b_unhooked = True Then Else MsgBox "WinEventHook couldn't be stopped! " & _ "Variable 'b_unhooked' is false! " & _ "Better restart Windows now!" End If End Sub ' CALLBACK FUNC OF "SetWinEventHook" (DEFINE ACTIONS TO RUN ON THE EVENTS): ' http://stackoverflow.com/questions/20486944/detecting-in-vba-when-the-window-containing-an-excel-instance-becomes-active Public Function WinEventFunc(ByVal HookHandle As Long, ByVal LEvent As Long, ByVal hWnd As Long, ByVal idObject As Long, ByVal idChild As Long, ByVal idEventThread As Long, ByVal dwmsEventTime As Long) As Long 'This function is a callback passed to the win32 api 'We CANNOT throw an error or break. Bad things will happen On Error Resume Next Dim l_handle As Long Dim s_buffer As String Dim b_visible As Boolean Dim i_bufferLength As Integer s_buffer = String$(32, 0) i_bufferLength = apiGetClassName(hWnd, s_buffer, Len(s_buffer)) If Left(s_buffer, i_bufferLength) = DLG_CLSID Then b_visible = apiShowWindow(hWnd, SW_HIDE) WinEventFunc = hWnd End If End Function 

在你的VBA代码中,当你想把你的excel工作簿保存为PDF时,你可以这样调用上面的macros:

 ' ADD WINDOWS EVENT HOOK BEFORE SAVING: Application.Run XL_WB.Name & "!StartEventHook" ' SAVE EXCEL AS PDF: ' https://msdn.microsoft.com/de-de/library/microsoft.office.tools.excel.worksheetbase.exportasfixedformat.aspx XL_WB.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:="C:\PDF.pdf", _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False ' REMOVE WINDOWS EVENT HOOK AFTER SAVING: Application.Run XL_WB.Name & "!StopEventHook" 

在上面的VBA代码示例中,“XL_WB”是一个variables。 你必须根据你的需要来调整它。 例如使用“ActiveSheet”代替。

从下面的其他网站用户也要求帮助解决这个问题: