none
Adding Signature Line Word-Excel in .Net RRS feed

  • Question

  • 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..

    i.stack.imgur.com/YyA4v.png  (cant added a picture here)

    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.

    Friday, October 21, 2016 8:37 AM

Answers

  • Hey,

    Thank you for your reply. Yes it solved my problem. i was expecting another ways but for now, this way also seems good to me.

    here is my code

    private static void AddSignatureLine()         {             if (string.IsNullOrEmpty(_path))                 return;             try             {                 var templateFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);                 string addinFileName;                 var fi = newFileInfo(_path);                 templateFolderPath = Path.Combine(templateFolderPath, "Templates");                 addinFileName = Path.Combine(templateFolderPath,                     fi.Extension.Replace(".", "").ToUpper().Equals("DOCX")                         ? "SignatureTemplate.dotx"                         : "SignatureTemplate.dot");                 oWord = new Word.Application();                 //oWord.Visible = true;object tmpPath = _path;                 oDoc = oWord.Documents.Open(ref tmpPath, 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);                 oWord.AddIns.Add(addinFileName); 

    var template = oWord.Templates                     .OfType<Word.Template>()                     .FirstOrDefault(ta => ta.Name == "SignatureTemplate.dotx");                 var buildingBlock = template.BuildingBlockEntries.Item("TestSignature");                 var range = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                 buildingBlock.Insert(range, true);                 oWord.Documents.Save();                 oWord.Quit();             }             catch (Exception ex)             {                 throw ex;             }             finally             {                 ReleaseObject(oDoc);                 ReleaseObject(oWord);             }         }  

        private static void ReleaseObject(object obj)       {           try           {               System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);               obj = null;           }           catch (Exception ex)           {               obj = null;               throw ex;           }           finally           {               GC.Collect();           }       }     

    I've also solved excel issue with similar way. (excel does not have quick parts)

                                     



    • Edited by tsfsf Tuesday, October 25, 2016 7:53 AM
    • Marked as answer by tsfsf Tuesday, October 25, 2016 7:54 AM
    Tuesday, October 25, 2016 7:33 AM

All replies

  • Hi tsfsf,

    it looks like you got your answer and your issue has been resolved.

    you had posted a same issue on Stack overflow forum. where you mark the post as an Answer.

    Below is the link of that thread marked by you on Stackoverflow.

    Adding Signature Line Word-Excel

    so I suggest you to update the status of this thread so that community members can know that your issue has been resolved.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 24, 2016 8:13 AM
    Moderator
  • Hey,

    Thank you for your reply. Yes it solved my problem. i was expecting another ways but for now, this way also seems good to me.

    here is my code

    private static void AddSignatureLine()         {             if (string.IsNullOrEmpty(_path))                 return;             try             {                 var templateFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);                 string addinFileName;                 var fi = newFileInfo(_path);                 templateFolderPath = Path.Combine(templateFolderPath, "Templates");                 addinFileName = Path.Combine(templateFolderPath,                     fi.Extension.Replace(".", "").ToUpper().Equals("DOCX")                         ? "SignatureTemplate.dotx"                         : "SignatureTemplate.dot");                 oWord = new Word.Application();                 //oWord.Visible = true;object tmpPath = _path;                 oDoc = oWord.Documents.Open(ref tmpPath, 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);                 oWord.AddIns.Add(addinFileName); 

    var template = oWord.Templates                     .OfType<Word.Template>()                     .FirstOrDefault(ta => ta.Name == "SignatureTemplate.dotx");                 var buildingBlock = template.BuildingBlockEntries.Item("TestSignature");                 var range = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;                 buildingBlock.Insert(range, true);                 oWord.Documents.Save();                 oWord.Quit();             }             catch (Exception ex)             {                 throw ex;             }             finally             {                 ReleaseObject(oDoc);                 ReleaseObject(oWord);             }         }  

        private static void ReleaseObject(object obj)       {           try           {               System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);               obj = null;           }           catch (Exception ex)           {               obj = null;               throw ex;           }           finally           {               GC.Collect();           }       }     

    I've also solved excel issue with similar way. (excel does not have quick parts)

                                     



    • Edited by tsfsf Tuesday, October 25, 2016 7:53 AM
    • Marked as answer by tsfsf Tuesday, October 25, 2016 7:54 AM
    Tuesday, October 25, 2016 7:33 AM