Printing Embedded web browser

luvbite38

Active Member
Joined
Jun 25, 2008
Messages
368
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
I am having a weird problem with printing my summary page in a workbook. My summary page contains a combination of charts, text and embedded internet explorer (showing Google Map). It all works fine when you look at it on the screen, everything is fully functional. However, when I am printing this page, it prints everything except the embedded Google Map. It has been two days since I am trying to fix this issue L. But no luck. Print object is true and checked for the web browser. Could you guys please help me fix this issue so I can print the entire page including the currently displayed screen of the explorer.<o:p></o:p>
<o:p> </o:p>
Is it possible that to write a code to take a screenshot of the entire page (including the web browser) and overlay on top of my existing page and if someone cancels the print option or when the page is gone to printer. It automatically deletes the screenshot and it gets back to original settings i.e. my summary page??<o:p></o:p>
<o:p> </o:p>
Hope it makes sense??<o:p></o:p>
<o:p> </o:p>
Please help…<o:p></o:p>
<o:p> </o:p>
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I had a similar problem trying to print out an embeeded web control before. I came up with this API workaround however this will not Print the whole worksheet it will only print the Visible Range which means that the user will have to scroll the embeeded Web Control into view before using the code.

Place the following in a standard module :

Code:
Option Explicit

Private Type DOCINFO
    cbSize As Long
    lpszName As String
    lpszOutput As Long
End Type

Private Declare Function FindWindow Lib "user32.dll" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Declare Function CreateCompatibleDC Lib "gdi32" _
(ByVal hDC As Long) As Long
 
Private Declare Function CreateCompatibleBitmap Lib "gdi32" _
(ByVal hDC As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long) As Long
 
Private Declare Function SelectObject Lib "gdi32" _
(ByVal hDC As Long, _
ByVal hObject As Long) As Long

Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long) As Long

Private Declare Function BitBlt Lib "gdi32" _
(ByVal hDestDC As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hSrcDC As Long, _
ByVal xSrc As Long, _
ByVal ySrc As Long, _
ByVal dwRop As Long) As Long
 
Private Declare Function GetDC Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function ReleaseDC Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hDC As Long) As Long

Private Declare Function GetDeviceCaps Lib "gdi32" ( _
ByVal hDC As Long, ByVal nIndex As Long) As Long

Private Declare Function StartDoc& Lib "gdi32" _
Alias "StartDocA" (ByVal hcs As Long, lpDI As DOCINFO)

Private Declare Function StartPage& Lib "gdi32" (ByVal hcs As Long)

Private Declare Function EndPage& Lib "gdi32" (ByVal hcs As Long)

Private Declare Function EndDoc& Lib "gdi32" (ByVal hcs As Long)

Private Declare Function DeleteDC& Lib "gdi32" (ByVal hDC As Long)

Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90
Private Const PointsPerInch = 72
Private Const SRCCOPY As Long = &HCC0020


'---------------'
'Public routine.'
'---------------'
Public Sub PrintVisibleRange()

    Const ZOOM_FACTOR = 3 '<--- Change this value as needed.
    Dim MyDoc As DOCINFO
    Dim hwnd As Long
    Dim lBmp As Long
    Dim lMemoryDC As Long
    Dim lwbDC As Long
    Dim lDC As Long
    Dim wd As Single
    Dim hg As Single
    Dim bHeadings As Boolean
    
    
    'Store the workbook hwnd.
    hwnd = FindWindow("XLMAIN", Application.Caption)
    hwnd = FindWindowEx(Application.hwnd, 0&, "XLDESK", vbNullString)
    hwnd = FindWindowEx(hwnd, 0&, "EXCEL7", vbNullString)
    
    'store the row & column headers visible state.
    bHeadings = Application.ActiveWindow.DisplayHeadings
    
    'hide the row & column headers.
    If bHeadings Then Application.ActiveWindow.DisplayHeadings = False

    'Store the Visible Range height & width.
    wd = Application.ActiveWindow.VisibleRange.Width
    hg = Application.ActiveWindow.VisibleRange.Height
    
    'Store the workbook window dc.
    lwbDC = GetDC(hwnd)
    
    'create a memory dc.
    lMemoryDC = CreateCompatibleDC(lwbDC)
    
    'create a compatible bmp.
    lBmp = CreateCompatibleBitmap _
    (lwbDC, PTtoPX(wd, False), PTtoPX(hg, True))
    
    'select the bmp to the memory dc.
    DeleteObject SelectObject(lMemoryDC, lBmp)
    
    'copy the visible range onto the memory dc.
    BitBlt lMemoryDC, 0, 0, PTtoPX(wd, False), PTtoPX(hg, True), _
    lwbDC, 0, 0, SRCCOPY
    
    'Initialize the DOCINFO srtucture.
    MyDoc.lpszName = "VisibleRange_PrintOut"
    MyDoc.lpszOutput = 0
    MyDoc.cbSize = LenB(MyDoc)
    
    'Show the Win Print Dlg.
    lDC = Show_PrintDlg
    
    'Start a new print job.
    Call StartDoc(lDC, MyDoc)
    
    Call StartPage(lDC)
    
    'Print the visible range.( May need to adjust the zoom factor ! )
    BitBlt lDC, PTtoPX(0, False), PTtoPX(0, True), _
    PTtoPX(wd, False) * ZOOM_FACTOR, PTtoPX(hg, True) * ZOOM_FACTOR, _
    lMemoryDC, 0, 0, SRCCOPY
    
    Call EndPage(lDC)
    
    'End the printing.
    Call EndDoc(lDC)
    
    'CleanUp.
    Call DeleteDC(lDC)
    
    Call ReleaseDC(0, lwbDC)
    Call ReleaseDC(lMemoryDC, 0)
    
    Application.ActiveWindow.DisplayHeadings = bHeadings

End Sub


'------------------'
'Private routines .'
'------------------'
Private Function Show_PrintDlg() As Long

    Dim dlg As Object
    
    Const cdlPDPrintSetup = 64
    Const cdlPDReturnDC As Long = &H100
    
    'Show the print dlg to retrieve the printer dc.
    Set dlg = CreateObject("MSComDlg.CommonDialog")
    
    With dlg
        .CancelError = False
        .Flags = cdlPDPrintSetup + cdlPDReturnDC
        .ShowPrinter
        Show_PrintDlg = .hDC
    End With
    
    Set dlg = Nothing

End Function


Private Function ScreenDPI(bVert As Boolean) As Long
 
   Static lDPI(1), lDC
   
   If lDPI(0) = 0 Then
        lDC = GetDC(0)
        lDPI(0) = GetDeviceCaps(lDC, LOGPIXELSX)
        lDPI(1) = GetDeviceCaps(lDC, LOGPIXELSY)
        lDC = ReleaseDC(0, lDC)
    End If
    ScreenDPI = lDPI(Abs(bVert))
 
End Function
 
Private Function PTtoPX _
(Points As Single, bVert As Boolean) As Long
 
    PTtoPX = Points * ScreenDPI(bVert) / PointsPerInch
 
End Function

To use the above code , just run the following Test Macro :

Code:
Sub Test()
    Call PrintVisibleRange
End Sub
 
Last edited:
Upvote 0
Hey mate,

thank you soooooo much for sparing some time to response.

I placed the first code in a standard module and and placed the second in another.

when I ran the test.code: it gave me following error:

Run time error 429

active x component cannot create object.

Help me to fix this pls....

Thanks

A
 
Upvote 0
That error indicates the "MSComDlg.CommonDialog" Control is either not existant in the current machine or not registered.

The reason I had to use this control was to be able to retrieve the Printer Device Context as there is no Printer Object in VBA.

In order to avoid the need to use/register external controls, try using the GetDefaultPrinter and CreateDC APIs as follows and see what happens - ( I tested it on Win7 Office 2007 and worked fine )

In a Standard module :

Code:
Option Explicit

Private Type DOCINFO
    cbSize As Long
    lpszName As String
    lpszOutput As Long
End Type

Private Declare Function FindWindow Lib "user32.dll" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Declare Function CreateCompatibleDC Lib "gdi32" _
(ByVal hDc As Long) As Long
 
Private Declare Function CreateCompatibleBitmap Lib "gdi32" _
(ByVal hDc As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long) As Long
 
Private Declare Function SelectObject Lib "gdi32" _
(ByVal hDc As Long, _
ByVal hObject As Long) As Long

Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long) As Long

Private Declare Function BitBlt Lib "gdi32" _
(ByVal hDestDC As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hSrcDC As Long, _
ByVal xSrc As Long, _
ByVal ySrc As Long, _
ByVal dwRop As Long) As Long
 
Private Declare Function CreateDC Lib "gdi32" _
Alias "CreateDCA" _
(ByVal lpDriverName As String, _
ByVal lpDeviceName As String, _
ByVal lpOutput As String, _
ByVal lpInitData As Any) As Long
 
Private Declare Function GetDC Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function ReleaseDC Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hDc As Long) As Long

Private Declare Function GetDeviceCaps Lib "gdi32" ( _
ByVal hDc As Long, ByVal nIndex As Long) As Long

Private Declare Function DeleteDC Lib "gdi32" (ByVal hDc As Long) As Long

Private Declare Function StartDoc Lib "gdi32" _
Alias "StartDocA" (ByVal hcs As Long, lpDI As DOCINFO) As Long

Private Declare Function EndDoc Lib "gdi32" (ByVal hcs As Long) As Long

Private Declare Function GetDefaultPrinter Lib "winspool.drv" _
Alias "GetDefaultPrinterA" _
(ByVal sPrinterName As String, _
lPrinterNameBufferSize As Long) As Long

Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90
Private Const PointsPerInch = 72
Private Const SRCCOPY As Long = &HCC0020


'---------------'
'Public routine.'
'---------------'
Public Sub PrintVisibleRange()

    Const ZOOM_FACTOR = 3 '<--- Change this value as needed.
    Dim MyDoc As DOCINFO
    Dim hwnd As Long
    Dim lBmp As Long
    Dim lMemoryDC As Long
    Dim lwbDC As Long
    Dim lDC As Long
    Dim wd As Single
    Dim hg As Single
    Dim bHeadings As Boolean
    
    
    'Store the workbook hwnd.
    hwnd = FindWindow("XLMAIN", Application.Caption)
    hwnd = FindWindowEx(Application.hwnd, 0&, "XLDESK", vbNullString)
    hwnd = FindWindowEx(hwnd, 0&, "EXCEL7", vbNullString)
    
    'Store the Visible Range height & width.
    wd = Application.ActiveWindow.VisibleRange.Width
    hg = Application.ActiveWindow.VisibleRange.Height
    
    'store the row & column headers visible state.
    bHeadings = Application.ActiveWindow.DisplayHeadings
    
    'hide the row & column headers.
    If bHeadings Then Application.ActiveWindow.DisplayHeadings = False
    
    'Store the workbook window dc.
    lwbDC = GetDC(hwnd)
    
    'create a memory dc.
    lMemoryDC = CreateCompatibleDC(lwbDC)
    
    'create a compatible bmp.
    lBmp = CreateCompatibleBitmap _
    (lwbDC, PTtoPX(wd, False), PTtoPX(hg, True))
    
    'select the bmp to the memory dc.
    DeleteObject SelectObject(lMemoryDC, lBmp)
    
    'copy the visible range onto the memory dc.
    BitBlt lMemoryDC, 0, 0, PTtoPX(wd, False), PTtoPX(hg, True), _
    lwbDC, 0, 0, SRCCOPY
    
    'restore the row & column headers.
    Application.ActiveWindow.DisplayHeadings = bHeadings

    'retrieve the default printer dc.
    lDC = GetPrinterDC

    If lDC <> 0 Then
    
        'Initialize the DOCINFO srtucture.
        MyDoc.lpszName = "VisibleRange_PrintOut"
        MyDoc.lpszOutput = 0
        MyDoc.cbSize = Len(MyDoc)

        'Start a new print job.
        Call StartDoc(lDC, MyDoc)
        
        'Print the visible range.( May need to adjust the zoom factor ! )
        BitBlt lDC, PTtoPX(0, False), PTtoPX(0, True), _
        PTtoPX(wd, False) * ZOOM_FACTOR, PTtoPX(hg, True) * ZOOM_FACTOR, _
        lMemoryDC, 0, 0, SRCCOPY
        
        'End the printing.
        Call EndDoc(lDC)
        
        'CleanUp.
        Call DeleteDC(lDC)
        Call ReleaseDC(0, lwbDC)
        Call ReleaseDC(lMemoryDC, 0)

    End If
   

End Sub


'------------------'
'Private routines .'
'------------------'

Private Function GetPrinterDC() As Long

    Dim sBuffer As String
    Dim sPrinterName As String
    Dim hPrinter As Long
    
    sBuffer = Space(128)
    If GetDefaultPrinter(sBuffer, 128) Then
        sPrinterName = Left(sBuffer, 128 - 1)
        GetPrinterDC = CreateDC("WINSPOOL", sPrinterName, vbNullString, 0&)
    End If

End Function

Private Function ScreenDPI(bVert As Boolean) As Long
 
   Static lDPI(1), lDC
   
   If lDPI(0) = 0 Then
        lDC = GetDC(0)
        lDPI(0) = GetDeviceCaps(lDC, LOGPIXELSX)
        lDPI(1) = GetDeviceCaps(lDC, LOGPIXELSY)
        lDC = ReleaseDC(0, lDC)
    End If
    ScreenDPI = lDPI(Abs(bVert))
 
End Function
 
Private Function PTtoPX _
(Points As Single, bVert As Boolean) As Long
 
    PTtoPX = Points * ScreenDPI(bVert) / PointsPerInch
 
End Function
Usage code :

Code:
Sub TEST()
    Call PrintVisibleRange
End Sub
Note: Remember this will only print the Visible Range so you will need to scroll the WebBrowser Control (or any other problematic worksheet embeeded object) into view before running the TEST macro.
 
Upvote 0
Thanks mate,

I ran the macro but this time I got no error what so ever but I didnt get any printer dialougue either. So when I went to print view option. The object was shown as blank.

I am extremely sorry for being pain.... but could you kindly indicate, if I am missing something.

I am using Excel 2010 and XP.

Thanks once again.

Kind Regards,

A
 
Upvote 0
Thanks mate,

I ran the macro but this time I got no error what so ever but I didnt get any printer dialougue either. So when I went to print view option. The object was shown as blank.

I am extremely sorry for being pain.... but could you kindly indicate, if I am missing something.

I am using Excel 2010 and XP.

Thanks once again.

Kind Regards,

A

You are not supposed to get a printer dialogue. The TEST macro should just send the visible range to the default printer straight away.
 
Upvote 0
thanks mate......

I have prob: ran the macro for 100 times and I now realised that I have 100 pages sitting on my printer :P......


you're a super super star mate.....


Thanks a Million, Trillion, Zillion
 
Upvote 0
thanks mate......

I have prob: ran the macro for 100 times and I now realised that I have 100 pages sitting on my printer :P......


you're a super super star mate.....


Thanks a Million, Trillion, Zillion

You are welcome and glad it worked for you.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top