Button to copy and paste a field on an InfoPath form to the clipboard - InfoPath Dev
in

InfoPath Dev

Button to copy and paste a field on an InfoPath form to the clipboard

Last post 04-27-2009 03:41 PM by msupinski. 20 replies.
Page 1 of 2 (21 items) 1 2 Next >
Sort Posts: Previous Next
  • 11-20-2008 12:35 AM

    Button to copy and paste a field on an InfoPath form to the clipboard

    Hi,

    I'm absolutely new to InfoPath, but I already reached that a from (it content as well) is stored in a form library at our WSS 3.0. Now I would like to have not experienced users (more not experienced than me :-) ) to open the data from the list and watch it with InfoPath and to have a button (I already have this button on the form) and when clicking on it the data (content) of a specific field should be copied to the clipboard.

    Now I do have added this button and set it to custom code, but how can I now copy the content of a specific form field to the clipboard...

    Can you give me a code that will do this?

    Many thanks...

     Joerg

  • 11-20-2008 04:12 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    In C# for InfoPath 2007, the following would do the trick: 

    XPathNavigator nodeToCopy = MainDataSource.CreateNavigator().SelectSingleNode("my:myFields/my:field1", NamespaceManager);
    if (!string.IsNullOrEmpty(nodeToCopy.Value))
    {
         Clipboard.SetText(nodeToCopy.Value, TextDataFormat.Text);
    }
    else
    {
         MessageBox.Show("There is no text to copy!");
    }

     Just replace "my:myFields/my:field1" with the actual XPath of the field you want to copy.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-20-2008 06:51 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    Dear Jimmy,

    many thanks to you for helping me out, I now have this code behind my button, where SmartKey is the name of the Field (it is XHTML formated Richtext):

    function CTRL9_5::OnClick(eventObj)
    {

    XPathNavigator nodeToCopy = MainDataSource.CreateNavigator().SelectSingleNode("my:myFields/my:SmartKey", NamespaceManager);
    if (!string.IsNullOrEmpty(nodeToCopy.Value))
      {
       Clipboard.SetText(nodeToCopy.Value, TextDataFormat.Text);
      } 
    else
      {
       MessageBox.Show("There is no text to copy!");
     }
    }

    And it is automatically stored in script.js - this was already there as I double clicked the button to add my custom code and it said where to put it to... 

    But I'm getting an error with this:

    Form template: https://my.sharepoint.com/test/SmartKeyLibrary/Forms/template.xsn
    InfoPath cannot open the selected form because of an error in the form's code.
    The following error occurred:

    Expected ';'
    File:script.js
    Line:24
     XPathNavigator nodeToCopy = MainDataSource.CreateNavigator().SelectSingleNode("my:myFields/my:SmartKey", NamespaceManager);

    What I'm doing wrong?

    Many thanks again for your help!

    Joerg

  • 11-20-2008 06:57 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    The problem is that the code I wrote above is C# code and your form is set up to use JScript code.  Which version of InfoPath are you using?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-20-2008 06:59 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    InfoPath 2003 (11.8165.8221) SP3 do I have to change the setting anywhere that it uses C# instead of JScript? If so - where can I set this up?

    When I open my form I found a section where I can change the setting, but it gives me only JScript or VBScript...

  • 11-20-2008 07:09 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    IP2003 does not have built-in support for C#.  I can try to figure out a way to do this in JScript tomorrow, if nobody else responds by then.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-20-2008 07:16 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    Dear Jimmy,

    again many thanks for your help...

    When I open my form I found a section where I can change the setting, but it gives me only JScript or VBScript...

    Would be great to get a solution... :-)

    Thanks!

    Joerg

  • 11-24-2008 03:15 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    Hi Jimmy,

    until now no one replied :-(

    Are you able to make this work with JScript or VBScript?

    Many thanks in advance...

  • 11-26-2008 03:05 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    As no one replies anymore I startet to do my own tests...

    First of all I simply would like to look if I can read a value given in a field called SmartKey

    Sub copyButton_OnClick(eventObj)

    Dim Hello
    Dim SmartValue

    set SmartValue = XDocument.DOM.selectSingleNode("//my:myfields/my:Smartkey").text

    Hello = MsgBox(SmartValue, 0, "Testfenster")

    End Sub

    But it throws out this error:

    The following error occurred:

    Object required: 'XDocument.DOM.selectSingleNode(...)'
    File:script.vbs
    Line:23

    Line 23 is this:

    set SmartValue = XDocument.DOM.selectSingleNode("//my:myfields/my:Smartkey").text

    What I have is InfoPath 2003 set to the form to use VBScript. What have I done wrong, do I have to include something or do I need to configure/setup InfoPath in a special way?
    The data I would like to copy is located at the actual opened and displayed form in info path...

    And then what comes next? How to copy the SmartValue content to the clipboard?

    Hope you can help out...

    Thanks

    Joerg

  • 11-26-2008 08:46 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    I'd guess the "//" before my:myfields should be "/". The object required error means your code to the right of the = sign in your set statement is not returning a value. Generally, this is caused by incorrect XPath.....

    Hilary Stoupa

  • 11-27-2008 12:00 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    Hilary Stoupa:

    I'd guess the "//" before my:myfields should be "/".

     

    Dear Hilary,

    many thanks for your reply - but sorry - no change - same error...

    But I found this in the MSE Help:

    Note  In some cases, calling the ExecuteAction method from the OnClick event for a button in a view may cause an error. This is because the selected context changes to the button when the button is clicked. In this case, it is better to use a button (or link) on a custom task pane, toolbar, or menu to call the ExecuteAction method.

    so I moved my code onto a custom toolbar with a button and this code:

    Sub CopySmartKey_OnClick()

       Dim objXMLNodes
       Dim objXMLNode

       set objXMLNode = XDocument.DOM.selectSingleNode("/my:myfields/my:SmartKeyValue")
       XDocument.View.SelectNodes(objXMLNode)
       XDocument.View.ExecuteAction("Copy")

    End Sub

     

    And now I'm getting this error:

    The following error occurred:

    The specified pxnStartNode value for the View.SelectNodes call is not valid.
    File:script.vbs
    Line:25

    Line:25 is XDocument.View.SelectNodes(objXMLNode)

    What I do not understand is that in the InfoPath 2003 Help the code is written like this:

    function SelectEmployee()
    {
       var objXMLNodes;
       var objXMLNode;

       objXMLNode = XDocument.DOM.selectSingleNode("/employees/employee");
       XDocument.View.SelectNodes(objXMLNode);

       objXMLNodes = XDocument.View.GetSelectedNodes();
       if (objXMLNodes.Count > 0)
       {
          XDocument.UI.Alert(objXMLNodes(0).nodeName + "\n\n" + objXMLNodes(0).text);
       }
    }

     

    But when I put this code into the Microsoft Script Editor the code throws out errors... so I modified it to the above mentioned code I'm trying to use!
    Why is microsoft so confusing a NewBe with a lot of code in the help that doesn't work!?
    I also tried the code setting the InfoPath 2003 form to JScript and VBScript but does not make any difference.

    Any hints?

    Cheers Joerg

  • 11-27-2008 12:18 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    In all likelihood the error you're getting is the result of invalid XPath in line 24.  In an earlier post you had a node name my:SmartKey, and in this post you have my:SmartKeyValue.  Which one is it?  And in either case, unless you changed it from the default, the 'F' in my:myFields should be capitalized.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-27-2008 12:47 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    Jimmy:

    In all likelihood the error you're getting is the result of invalid XPath in line 24.  In an earlier post you had a node name my:SmartKey, and in this post you have my:SmartKeyValue.  Which one is it?  And in either case, unless you changed it from the default, the 'F' in my:myFields should be capitalized.

    Hi Jimmy,

    many thanks for your hint!

    Well I produced a new form to have a new environment to test with and I called the Field SmartKeyValue in this form so this is OK, but I had a lowered F and capitalized it, it seems now that I'm closer to my desired outcome as I do now have this error:


    The following error occurred:

    Either the combination of values for the View.SelectNodes call is an invalid selection, or the values are not defined for this view, defined for this view more than once, or not selectable in the view.
    File:script.vbs
    Line:25

    Line:25 looks like this: XDocument.View.SelectNodes(objXMLNode)

    Many thanks for helping me and your patience!

    Cheers

    Joerg

  • 11-27-2008 01:40 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    Please try changing line 25 to

       XDocument.View.SelectText(objXMLNode)

    that should do the trick.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-27-2008 02:07 AM In reply to

    Re: Button to copy and paste a field on an InfoPath form to the clipboard

    Hi Jimmy,

    this is my code now and it works very fine now...


    Sub CopySmartKey_OnClick()

       Dim objXMLNode

       set objXMLNode = XDocument.DOM.selectSingleNode("/my:myFields/my:SmartKeyValue")
       XDocument.View.SelectText(objXMLNode)
       XDocument.View.ExecuteAction("Copy")
      
    End Sub

    Now thanks again for all your help...

    Cheers

    Joerg

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