Automatic Submit and Close - InfoPath Dev
in

InfoPath Dev

Automatic Submit and Close

Last post 03-27-2013 02:03 AM by HHearle. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 03-11-2013 06:58 AM

    • bblanke
    • Top 500 Contributor
      Male
    • Joined on 11-12-2009
    • Milwaukee, WI
    • Posts 32

    Automatic Submit and Close

    I have a form that takes in some input parameters, passed from an prior process. These are the only fields that need to be populated. I would like once the input parameters are set in the form to Submit and Close the form with no user input.

    I found a way to get around the inability to have an execute command initiated from Form Loading event, by having the form loading event edit a field and based on the change of that field it call the submit process.

    The form submits fine, but errors when I try to close the form.

    using Microsoft.Office.InfoPath;
    using System;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.XPath;
    using mshtml;
    
    namespace JSR_Issue_Tracking
    {
        public partial class FormCode
        {
            public void InternalStartup()
            {
                EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
                ((ButtonEvent)EventManager.ControlEvents["Submit"]).Clicked += new ClickedEventHandler(Submit_Clicked);
                EventManager.XmlEvents["/my:myFields/my:xmlFilename"].Changed += new XmlChangedEventHandler(xmlFilename_Changed);
            }
    
            public void FormEvents_Loading(object sender, LoadingEventArgs e)
            {
                XPathNavigator xn = this.MainDataSource.CreateNavigator();
    
                if (String.IsNullOrEmpty(xn.SelectSingleNode("/my:myFields/my:jsrID", NamespaceManager).Value))
                {
                    try
                    {
                        string _jsrID = e.InputParameters["jsrid"];
                        string _jobName = e.InputParameters["jobname"];
                        string _titleCode = e.InputParameters["titlecode"];
                        string _psr = e.InputParameters["psr"];
    
                        xn.SelectSingleNode("/my:myFields/my:jsrID", NamespaceManager).SetValue(_jsrID);
    
                        xn.SelectSingleNode("/my:myFields/my:jobName", NamespaceManager).SetValue(_jobName);
    
                        xn.SelectSingleNode("/my:myFields/my:titleCode", NamespaceManager).SetValue(_titleCode);
    
                        xn.SelectSingleNode("/my:myFields/my:PSR", NamespaceManager).SetValue(_psr);
    
                        Set_XML_Filename();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("An error occured while trying to import data into the form.\nPlease manually fill in the top row and click submit.",
                            "Error on Import", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
            }
    
            private void Set_XML_Filename()
            {
                XPathNavigator xn = this.MainDataSource.CreateNavigator();
                string xname = xn.SelectSingleNode("/my:myFields/my:xmlFilename", NamespaceManager).Value;
    
                if (String.IsNullOrEmpty(xname))
                {
                    xname = Convert.ToString(long.Parse(System.DateTime.Now.ToString("yyyyMMddHHmmssfff")), 16).ToUpper();
                    xn.SelectSingleNode("/my:myFields/my:xmlFilename", NamespaceManager).SetValue(xname);
                }
            }
    
            public void Submit_Clicked(object sender, ClickedEventArgs e)
            {
                Submit_Form();
            }
    
            private void Submit_Form()
            {
                FileSubmitConnection submit = (FileSubmitConnection)(DataConnections["SharePoint Library Submit"]);
                try
                {
                    submit.Execute();
    
                    MessageBox.Show("Form Submitted.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
    
                    Application.ActiveWindow.Close(true);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
    
            public void xmlFilename_Changed(object sender, XmlEventArgs e)
            {
                Submit_Form();
            }
        }
    }
    
    Brandon Blanke
    Technical Analyst / Quad Graphics
  • 03-26-2013 08:04 AM In reply to

    Re: Automatic Submit and Close

    Why not just use a standard button?  You can create the two rules, one to submit, and one to close the form.

     Or if you still want that insanely long code above, use that as the code and then have the rule to close it.

  • 03-26-2013 08:28 AM In reply to

    • bblanke
    • Top 500 Contributor
      Male
    • Joined on 11-12-2009
    • Milwaukee, WI
    • Posts 32

    Re: Automatic Submit and Close

    I ended up going with just a button the use must click to submit and close the form. I was hoping to get it so the submitting user would not need to do anything.
    Brandon Blanke
    Technical Analyst / Quad Graphics
  • 03-27-2013 02:03 AM In reply to

    Re: Automatic Submit and Close

    Or you could use SendKeys... you seem quite proficient with code judging from the above. Try

    {

    var WshShell = new ActiveXObject("WScript.Shell");  

    WshShell.SendKeys("%fx")

    }

    Again, a user would have to click something to do it but it would probably be better since you could incorporate it into an existing button in the form as the rules always run before the code.  Trouble is, when I tried to do this I had error message "Automation Server cannot create object", but due to intranet policies at work I can't upgrade the script editor to fix this problem.  In theory it should work and would probably suit your needs better.

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