Export 2 Infopath fields to a text file or XML file using VB - InfoPath Dev
in

InfoPath Dev

Export 2 Infopath fields to a text file or XML file using VB

Last post 07-09-2015 10:30 AM by rmsphoto. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 06-30-2015 08:34 AM

    • rmsphoto
    • Not Ranked
    • Joined on 04-15-2010
    • Indianapolis
    • Posts 25

    Export 2 Infopath fields to a text file or XML file using VB

    I am looking to automatically export a couple fields to a text or XML file on a local hard drive whenever a form is submitted. I have done a lot of searching online, including this forum, for ideas, and due to my lack of coding knowledge, I am not having much luck. I currently have VB code attached to a button to generate folder on the local drive upon submit, now I want to also write some field data into one of those folders. Does anyone have a snippet of VB code I can attach to a button that will export an infopath field to a text or xml file? Thanks for any help!
  • 07-01-2015 04:55 PM In reply to

    Re: Export 2 Infopath fields to a text file or XML file using VB

     Okay, so you are already creating a folder? You just need to add a file with some field values?

    https://msdn.microsoft.com/en-us/library/wt3h7b61.aspx may also help you, but here is a simple code sample. Your XPath won't be my:myFields/my:field1 or my:myFields/my:field2 - right click the nodes you want data from in the data source task pane in InfoPath and select Copy XPath to get the correct XPath. Your file path is likely not D:\Documents\Temp\MyTest.txt either - so you'll want to change that. :)


    Dim path As String = "D:\Documents\Temp\MyTest.txt"

    ' Get some values from fields

    Dim s1 As String = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field1", NamespaceManager).Value

    Dim s2 As String = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field2", NamespaceManager).Value

    Dim lines() As String = {s1, s2}

    System.IO.File.WriteAllLines(path, lines)

     

    Hilary Stoupa

  • 07-02-2015 07:24 AM In reply to

    • rmsphoto
    • Not Ranked
    • Joined on 04-15-2010
    • Indianapolis
    • Posts 25

    Re: Export 2 Infopath fields to a text file or XML file using VB

    Thank you so much! I will give this a go this afternoon.
  • 07-08-2015 12:03 PM In reply to

    • rmsphoto
    • Not Ranked
    • Joined on 04-15-2010
    • Indianapolis
    • Posts 25

    Re: Export 2 Infopath fields to a text file or XML file using VB

    This solution worked perfectly! Thanks! However, after I instituted it, the other developer indicated that his app using this information (Autodesk Vault) does not work well with a text file... - He would like me to provide the same information in an Excel file, instead... Is it possible, using VB in Infopath to write a new row to an existing excel (that already contains legacy data) file for each infopth form submission? Thanks for any help!
  • 07-09-2015 10:30 AM In reply to

    • rmsphoto
    • Not Ranked
    • Joined on 04-15-2010
    • Indianapolis
    • Posts 25

    Re: Export 2 Infopath fields to a text file or XML file using VB

    Nevermind on the Excel. We decided that csv would be fine, so I am reading the existing csv, then rewriting it with the new data appended, and mark a field in the form to note that the data has been written to the csv file. Dim varNewDirectoryName As String = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:ProjFilename", NamespaceManager).Value Dim path As String = "Q:\PROJECT_CURRENT\PROJECT_CURRENT Folder Index.csv" Dim fileReader As String fileReader = My.Computer.FileSystem.ReadAllText(path) Dim s1 As String = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:ProjectNumber", NamespaceManager).Value Dim lines() As String = {fileReader & s1 & ", " & varNewDirectoryName} System.IO.File.WriteAllLines(path, lines) Dim nav As XPathNavigator = MainDataSource.CreateNavigator() nav.SelectSingleNode("/my:myFields/my:ExcelInfoWritten", NamespaceManager).SetValue("1")
Page 1 of 1 (5 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.