Hi
I want to insert the filename from the file (Attachment1) attached inside the form into the textbox (AttachmentComment).
Below shows the source code inside my InfoPath form.
I got an error related to DOM. Below shows the error:
Error 1 An object reference is required for the nonstatic field, method, or property 'Microsoft.Office.Interop.InfoPath.SemiTrust._XDocument2.DOM.get' C:\Users\spdev\Documents\InfoPath Projects\TQ_submission_form_v3_12\FormCode.cs 64 19 TQ_submission_form_v3_1
Error 2 The name 'thisXDocument' does not exist in the current context C:\Users\spdev\Documents\InfoPath Projects\TQ_submission_form_v3_12\FormCode.cs 94 17 TQ_submission_form_v3_1
________________________________________________________________________________________________
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Linq;
using Microsoft.Office.Interop.InfoPath.SemiTrust;
================================================================================
[InfoPathEventHandler(MatchPath = "/my:myFields/my:Attachment/my:Attachment1",
EventType = InfoPathEventType.OnAfterChange)]
public void attachedDoc_OnAfterChange(DataDOMEvent e)
{
if (e.IsUndoRedo)
{
return;
}
RetrieveFilename();
}
public void RetrieveFilename()
{
try
{
IXMLDOMNode opnXN =
Microsoft.Office.Interop.InfoPath.SemiTrust.XDocument.DOM.
selectSingleNode("/my:myFields/my:Attachment/my:Attachment1");
byte[] attachmentNodeBytes =
Convert.FromBase64String(opnXN.nodeTypedValue
.ToString());
int fnLength = attachmentNodeBytes[20] * 2;
byte[] fnBytes = new byte[fnLength];
for (int i = 0; i < fnBytes.Length; i++)
{
fnBytes[i] = attachmentNodeBytes[24 + i];
}
char[] charFileName =
System.Text.UnicodeEncoding.Unicode.GetChars(fnBytes);
string fileName = new string(charFileName);
fileName = fileName.Substring(0, fileName.Length - 1);
thisXDocument.DOM.
selectSingleNode("/my:myFields/my:Attachment/my:AttachmentComment").text
= fileName;
}
catch (Exception e)// TODO: Write better error handler
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}