我認爲你遇到的問題與ActiveX的安全障礙是因爲你嵌入到網頁excel。通過使用Excel的發佈方法將工作表發佈爲HTML,應該可以打印工作表。我使用它來創建一個電子郵件的HTML。
Dim rngeTableRange As Range
Dim lsTableFileName As String
lsTableFileName = <filepath and name for HTML output as string>
Set rngeTableRange = ActiveSheet.UsedRange
ActiveWorkbook.PublishObjects.Add(xlSourceRange, lsTableFileName, _
rngeTableRange.Parent.Name, rngeTableRange.Address, xlHtmlStatic).Publish True
如果您希望打印輸出顯示爲電子表格,您可以在發佈工作表之前添加邊框。下面的VBA代碼就會把邊框每個細胞周圍:
With ActiveSheet.UsedRange.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With ActiveSheet.UsedRange.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With ActiveSheet.UsedRange.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With ActiveSheet.UsedRange.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With ActiveSheet.UsedRange.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With ActiveSheet.UsedRange.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
End With
的結果應該是不到風度導致安全警告spreadheet的HTML圖像,我會承擔將打印。
不,謝天謝地,想象一下如果你的打印機可能會吐出的廣告。 – 2012-08-28 09:10:56
我希望用戶提示打印窗口,我的意思是按確定,然後將其打印到打印機。不是全部自動。我只想要打印excel文件。不是整個頁面。 PDF文件可以在隱藏的iframe中打開,並可以通過「window.print()」代碼發送到打印機。但不幸的是,它與Excel文件不一樣。 –
不認爲我得到這個問題,但由於瀏覽器本身不支持excel文件,它必須要求客戶端機器如何處理它,因此您可以打開\ save對話框。 – 2012-08-28 09:28:40