Switch view before exporting and submitting - InfoPath Dev
in

InfoPath Dev

Switch view before exporting and submitting

Last post 04-17-2008 10:31 AM by Greg Collins. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 04-16-2008 07:40 AM

    • jmvdh
    • Not Ranked
    • Joined on 04-16-2008
    • Posts 5

    Switch view before exporting and submitting

    I have an InfoPath 2003 form that has two views ("data entry" and "print view").  When I submit the form, I use code to switch to the "print view" before I export the form and submit it.

    Here's my code:

    thisXDocument.View.SwitchView("Print View")
    thisXDocument.View.Export("c:\MHT\temp.mht", "MHT")
    thisXDocument.DataAdapters.Item("Main submit").Submit()
    thisApplication.Quit(True)

    Unfortunately I get this error:

    InfoPath cannot execute any view-related object model calls. The view is not ready.

    If I comment out the first line to switch the view it works.  Any suggestions?

  • 04-16-2008 10:43 AM In reply to

    Re: Switch view before exporting and submitting

    InfoPath can be really picky about doing things at the same time you are switching views. When I say at the same time, I mean in one total transaction (all the rules and code the run as the result of a button click or whatever).

    As an example... you are trying to switch views and then export. Likely, if it works, your export will be the original edit view instead of the expected print view. Why? Because a view switch isn't actually complete until control returns to the form (i.e. after all of the rules and code involved have completed).

    What you can do is to set some kind of a flag: fPerformSubmit = true, and then switch views. Then in the OnSwitchView event (or maybe it only worked in the OnContextChange event--I don't recall which) you can perform your other functions if fPerformSubmit is true. When done, set it back to false.

  • 04-16-2008 12:42 PM In reply to

    • jmvdh
    • Not Ranked
    • Joined on 04-16-2008
    • Posts 5

    Re: Switch view before exporting and submitting

    OnSwitchView gave me the same error as before, but OnContextChange almost worked. The swiching view and exporting works fine but now it won't close the form.  I get an error: "Invalid context for OM call."

    I tried all the following close commands and they all generate that error.

    thisApplication.Quit(True)
    thisApplication.ActiveWindow.Close(True)
    thisApplication.XDocuments.Close(thisXDocument)
    thisXDocument.View.Window.Close(True)

    Any more suggestions?

  • 04-16-2008 01:38 PM In reply to

    Re: Switch view before exporting and submitting

    Argh! I missed that last statement. Unfortunately, the ONLY place you can perform a Window.Close() is in an OnClick and OnSubmitRequest handler. Although the help does not specificly suggest it, I think all the other Close() methods have the same restriction.

    However, since you are using the main submit, I think you might actually get the OnSubmitRequest event fired when you perform the submit. If that is the case, then you should be able to close it in that event handler. Convoluted--but hopefully it works!

  • 04-16-2008 02:01 PM In reply to

    • jmvdh
    • Not Ranked
    • Joined on 04-16-2008
    • Posts 5

    Re: Switch view before exporting and submitting

    I'm calling my code from OnSubmitRequest, so I don't think I can use it again.  Here is my current code.  How do I reuse OnSubmitRequest?

    <InfoPathEventHandler(EventType:=InfoPathEventType.OnSubmitRequest)> _
    Public Sub OnSubmitRequest(ByVal e As DocReturnEvent)
        StartSubmit()
    End Sub

    <InfoPathEventHandler(EventType:=InfoPathEventType.OnContextChange)> _
    Public Sub OnContextChange(ByVal e As DocContextChangeEvent)
        If mblnSubmit Then
            FinishSubmit()
        End If
    End Sub

    Private Sub StartSubmit()
        mblnSubmit = True
        thisXDocument.View.SwitchView("Print Version View 1")
    End Sub

    Private Sub FinishSubmit()
        mblnSubmit = False
        thisXDocument.View.Export("c:\MHT\temp.mht", "MHT")
        thisXDocument.DataAdapters.Item("Main submit").Submit()
        CloseInfoPath()
    End Sub

    Private Sub CloseInfoPath()
        On Error Resume Next
        thisApplication.Quit(True)
        thisApplication.ActiveWindow.Close(True)
        thisApplication.XDocuments.Close(thisXDocument)
        thisXDocument.View.Window.Close(True)
    End Sub

  • 04-17-2008 02:43 AM In reply to

    • Albert
    • Top 500 Contributor
    • Joined on 04-03-2008
    • The Netherlands
    • Posts 32

    Re: Switch view before exporting and submitting

    Hi,

    When calling the Export method from the CurrentView object in InfoPath 2007 the printview instead of the CurrentView is exported. This is undocumented (and unnatural imho) behaviour.

    I don't know about InfoPath 2003, but you could try to specify a printview and not call thisXDocument.View.SwitchView("Print View")

    Might be worth a try :-)

    Albert

  • 04-17-2008 05:57 AM In reply to

    • jmvdh
    • Not Ranked
    • Joined on 04-16-2008
    • Posts 5

    Re: Switch view before exporting and submitting

    I tried that, and it does print the print view when you print or print preview the form, but not when you export it.  I guess that is a 2007 feature only. My clients are all on 2003 still, so 2007 isn't an option. If anyone has any other ideas, please let me know... Thanks.

  • 04-17-2008 06:59 AM In reply to

    • jmvdh
    • Not Ranked
    • Joined on 04-16-2008
    • Posts 5

    Re: Switch view before exporting and submitting

    Would it maybe be possible to temporarily open the print view in a dialog box or task pane or something in the background, and then export it from there?  Afterwards I could use code to close it and continue with the submit and close. I'm not sure how to do this...

  • 04-17-2008 10:31 AM In reply to

    Re: Switch view before exporting and submitting

    I'm out of ideas here. Unfortunately there are still a good number of things that InfoPath could improve upon to make this a more fully functional enviornment to develop in, and we are only on the 3rd release--so they are still getting good feedback from customers like yourself on things that are needed.

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