CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: PrintPreview of Excel in VB.Net

  1. #1
    Join Date
    Nov 2007
    Posts
    11

    Exclamation PrintPreview of Excel in VB.Net

    Hi Guys ,Need your help

    I have a Windows Application that uses web browser to show different documents(.xls,.doc,.txt)
    Now i need a functionality so shat the user can printpreview of Excel document.

    The code which i have written in NavigateComplete2 function of web browser is as follows :

    Code:
        Dim ob As Object = e.pDisp
        Dim oDocument As Object
        Dim oApplication As Object
        Dim oName As Object
        oDocument = ob.GetType().InvokeMember("Document", System.Reflection.BindingFlags.GetProperty, Nothing, ob, Nothing)
        oApplication = ob.GetType().InvokeMember("Application", System.Reflection.BindingFlags.GetProperty, Nothing, oDocument, Nothing)
        oName = ob.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, Nothing, oApplication, Nothing)
                
        Dim exce As Excel.Application = oApplication
        Dim wBook As Excel.Workbook = oDocument
        wBook.PrintPreview(True)

    but i am gettin below error :

    System.Runtime.InteropServices.ComException(0x800A03EC):Exception from HRESULT:0x800A03EC

    I have also tried ,

    Code:
        Dim excel As Excel.Application
        Dim wBook As Excel.Workbook
    
        wBook = webbrowser1.Document
        wBook.PrintPreview()
    but this also gives me the same error

    I am really looking for help urgenly , from last 2 days i am working to get the printPreview functionality.

    Any Help will be highly appreciated
    Last edited by HanneSThEGreaT; May 22nd, 2008 at 10:29 AM.

  2. #2
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: PrintPreview of Excel in VB.Net

    Quote Originally Posted by Naresh23
    ...
    I have also tried ,

    Dim excel As Excel.Application ' Do not use Excel as a local variable
    Dim wBook As Excel.Workbook ' Is a type declaration, but still not a "physical" excel workbook

    wBook = webbrowser1.Document
    wBook.PrintPreview()
    ...
    try:
    Code:
    		Dim MyExcel As Excel.Application = New Excel.Application
    		Dim wBook As Excel.Workbook = MyExcel.Workbooks.Add
    
    		Myexcel.Visible = True ' This line is to make Excel visible
    	   'Otherwise you will se nothing, even if excel had done it
    
    		'wBook = WebBrowser1.Document 'No idea what is this
    		wBook.PrintPreview()

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,268

    Re: PrintPreview of Excel in VB.Net

    Naresh23, please use [CODE] [/CODE] Tags when posting code, you've been here for some time, so you should know CODE tags makes everyone's lives easier.

    Back to the question.
    If I'm not mistaken, but I remember getting the very same error in one of my programs. The reason ( I guess ) was because the Excel Application has been closed by the user during execution of the program. So, in other words, make sure that your Excel Application object ( MyExcel ) is not Nothing, then do what you want.
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  4. #4
    Join Date
    Nov 2007
    Posts
    11

    Resolved Re: PrintPreview of Excel in VB.Net

    Thanks for the quick reply , and i will make sure that all the code are placed in the tag.

    The Reason why i dont want to create new instance of Excel Application is that i belive the Web browser already creates an instance of required application.

    if i create a new Excel application , the opening of new Excel application will appear to client which i dont want & also the issue with that is if the client clicks "Page Break Preview" the focus will shift to the new Excel Application.

    I just want the print preview of Excel file opened in my WebBrowser Control.
    Also i checked the Excel Application object and it is not null

    Code:
    oDocument = ob.GetType().InvokeMember("Document", System.Reflection.BindingFlags.GetProperty, Nothing, ob, Nothing)
                oApplication = ob.GetType().InvokeMember("Application", System.Reflection.BindingFlags.GetProperty, Nothing, oDocument, Nothing)
                oName = ob.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, Nothing, oApplication, Nothing)
                'MessageBox.Show("File opened by: " & oName.ToString())
                'MessageBox.Show("Application opened by: " & oApplication.ToString())
                Dim exce As Excel.Application = oApplication
                If exce Is Nothing Then
                    MessageBox.Show("exce is null")
                End If
               MessageBox.Show(exce.ActiveWorkbook.Sheets.Count)) 
               exce.ActiveWorkbook.Activate()
               exce.ActiveWorkbook.Sheets.PrintPreview()
    I am getting the count of Sheets also but not printpreview
    May be i think it is not getting the parent window ,but i dont know whether i right or not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)