unbalanced unbalanced - 1 month ago 38
C# Question

Adding Signature Line Word-Excel

I can add a signature line to the word document but i feel not good with my approach. I've searched a lot to handle my sitautation but not found any.

Well here is my code

private void CreateNewPage()
{
object missing = System.Reflection.Missing.Value;
object fileName = @"C:\docs\mydoc.docx";
object readOnly = false;
object isVisible = true;

//Start Word and open a document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;

oDoc = oWord.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing);


// var numberOfPages = oDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, false);



object oEndOfDoc = "\\endofdoc";
object paramNextPage = Word.WdBreakType.wdSectionBreakNextPage;

oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertBreak(ref paramNextPage);
//Insert a page break
object breakPage = Word.WdBreakType.wdPageBreak;


object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;

object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToLast;
object count = 3;

oWord.Selection.GoTo(ref what, ref which, ref count, ref missing);

object sigID = "{00000000-0000-0000-0000-000000000000}";
Timer t = new Timer();
t.Elapsed += (sender, args) =>
{


SendKeys.SendWait("{TAB}");
SendKeys.SendWait("~");
t.Stop();

};

t.Interval = 2000;
t.Start();
try
{

oWord.Activate();

SignatureSet signatureSet = oWord.ActiveDocument.Signatures;
// signatureSet.ShowSignaturesPane = false;
Signature objSignature = signatureSet.AddSignatureLine(sigID);
objSignature.Setup.SuggestedSigner = "docSigner";
objSignature.Setup.SuggestedSignerEmail = "abc@xyz.com";
objSignature.Setup.ShowSignDate = true;

// dynamic shape = objSignature.SignatureLineShape;

}
catch (Exception ex){}

oWord.Documents.Save();
oWord.Quit();

try
{
Marshal.ReleaseComObject(oWord);
}
catch (Exception){}

}


Well as you see below, when i call AddSignatureLine funciton, this window opens modal (like showdialog) and until close this, the code does not flow..



and I am bypass this by using sendkey but we know that it is not a good way. However, If i cant find any other solution then I will try to do that finding this window (word's child window) using Win32 APIs.

But I am curious if is there anyway to bypass this? Because there are thousand documents and I am looking for also a faster way.

After adding this signature line, then we can able to sign document with DocuSign. But if there is no any signature line in the document, DocuSign does not sign the document digitally. Then We have to add this line.

Answer

As there seems no way of circumventing the display of the modal Signature Setup dialog, I would suggest you the following workaround which is based on a building block containing a properly configured signature line:

  1. Create a new empty .dotx template. This template will be used to save the building block entry.
  2. Create a sample document containing a signature line and add this signature line via Insert > Quick Parts > Save Selection to Quick Part Gallery to the template created in the previous step.
  3. Include the template in your project and deploy the template with your executable or add-in.
  4. At runtime, load the template as an add-in

    Application.AddIns.Add(fullPathToDotx);
    
  5. You can now insert the building block from the template

    var template = Application.Templates
        .OfType<Microsoft.Office.Interop.Word.Template>()
        .FirstOrDefault(t => t.Name == addInFileName);
    var buildingBlock = template.BuildingBlockEntries.Item("Signature Line");
    buildingBlock.Insert(range, true);
    
  6. (Unload the building block template)

If you need to modify the properties of the signature line, you could either

  • Prepare multiple signature line building blocks which are configured accordingly, or
  • Dynamically patch the building block template (e.g. by creating a copy at runtime, open and manipulating the file using the Open XML SDK and temporarily load the modified version.