How do I print and switch views programmatically. - InfoPath Dev
in

InfoPath Dev

How do I print and switch views programmatically.

Last post 10-11-2011 08:59 AM by Doreen. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 10-05-2011 11:05 AM

    • Doreen
    • Top 50 Contributor
      Female
    • Joined on 08-11-2010
    • Federal Way, WA
    • Posts 291

    How do I print and switch views programmatically.

    I have a form with 3 views (so far). The Default view is "View 1" and is intended for entering data.  I have a button that will allow user to switch to a print view 2 that prints a read only blank form so it can be printed then filled in by hand.  These two functions work great. 

    The problem is, after the printing executes, I want to form to automatically switch back to the defautl View 1. Currently it stays on the print view 2.  I have put the following code in all the clicked event handler for the button:

    public void CTRL447_5_Clicked(object sender, ClickedEventArgs e)

    {

     this.Print();

    this.ViewInfos.SwitchView("View 1");

    }

    I get this error:

    System.Runtime.InteropServices.COMException
    InfoPath cannot execute any view-related object model calls. The view is not ready.
       at Microsoft.Office.Interop.InfoPath.SemiTrust.View.SwitchView(String bstrName)
       at Microsoft.Office.Interop.InfoPath.SemiTrust.ViewObjectWrapper.SwitchView(String bstrName)
       at Microsoft.Office.InfoPath.Internal.ViewInfoCollectionHost.SwitchView(String name)
       at Template1.FormCode.CTRL447_5_Clicked(Object sender, ClickedEventArgs e)
       at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
       at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

    Basically I want to switch view, print the view, then switch back to default view (where I started).

    I have read the other posts with regards to my question and none of them address how to do this - basically print another view from one view and go back to that view after it prints maybe that is why no one has responded to this post.


     

    Filed under: ,
  • 10-10-2011 04:26 PM In reply to

    • Doreen
    • Top 50 Contributor
      Female
    • Joined on 08-11-2010
    • Federal Way, WA
    • Posts 291

    Switching views and printing programmatically, results in different behavior with similar code...

    I have almost solved my problem.  I have several more views, but only concerned with 3 views for this problem; 1) default view, 2) print view (blank form), 3) print view (completed form)

    I have two buttons that operate similarly: on click, they switch to another view then print it via code. 

    The problem: one button (#2 below) works like I want it to (start on default view, switch to another view, print, return to default view)

    The other button (#1 below) does the same steps except it does not return to the default view. It doesn't matter which order I click the buttons, they consistently behave different from each other (Button #2 is desired behavior, #1 is not).  As far as my eye can see the code is identical with exception of the views I'm printing.  Can someone tell me why they behave differently?

     Here is the code:

     

    //Button #1 (does not return to default view after print)

    public void CTRL447_5_Clicked(object sender, ClickedEventArgs e) //"Print blank form" button

            {

                // Write your code here.

                try            

                {

                    //switch to another view

                    //this.ViewInfos.SwitchView("View 1B - Print Main (Blank)");

                    this.ViewInfos.Initial = this.ViewInfos["View 1B - Print Main (Blank)"];

                }

                catch (COMException ex)

                {

                    string msg = "Message: " + ex.Message + " Source: " + ex.Source;

                }

                //"Print blank form" button

                this.Print();  

            }

    //Button #2 (does return to default view after print)

    public void CTRL450_5_Clicked(object sender, ClickedEventArgs e) //"Print" button

            {

                // Write your code here.

                try

                {

                    //switch to printer-friendly view 

                    this.ViewInfos.Initial = this.ViewInfos["View 1A - Print Main (Completed)"];

                }

               catch (COMException ex3)

                {

                    string msg2 = "Message: " + ex3.Message + " Source: " + ex3.Source;

                }

                //"Print" button

                this.Print();

            }

     

  • 10-11-2011 08:59 AM In reply to

    • Doreen
    • Top 50 Contributor
      Female
    • Joined on 08-11-2010
    • Federal Way, WA
    • Posts 291

    Re: Switching views and printing programmatically, results in different behavior with similar code...

    I solved this problem.  I had forgotten that I set a rule on button #1 to switch to another view.  Once that rule was removed, it works just like button #2!

     Incidentally I searched all over the net to find out how to print other mulitiple views from one view, and remain on the original view.  The above code seems to do the job.  Try/Catch may seem like overkill but it stopped COMException errors from happening when I was debugging.

    I read this thread to help me understand what I needed to do: http://www.infopathdev.com/forums/p/533/2197.aspx#2197

Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.