Adding Signature Line Word-Excel

0 votes

Although I can add a signature line to the word document, I don't feel confident in my strategy. I looked everywhere for solutions to my situation but came up empty-handed.

       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 bypassed this by using send key but we know that it is not a good way. However, If I can't find any other solution then I will try to do that by finding this window (word's child window) using Win32 APIs.

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

Oct 14, 2022 in Others by Kithuzzz
• 38,000 points
794 views

1 answer to this question.

0 votes

Since there doesn't appear to be a way to stop the modal Signature Setup window from appearing, I would recommend the following workaround, which is based on a building block with a properly defined 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 manipulate the file using the Open XML SDK and temporarily load the modified version.
answered Oct 14, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Merge and export Excel/Word/PDF to PDF

Use GroupDocs.Merger for .NET API to merge Word, Excel, ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
1,472 views
0 votes
1 answer

How can I find and replace text in Word using Excel VBA?

Try this code Option Explicit Const wdReplaceAll = 2 Sub ...READ MORE

answered Oct 15, 2022 in Others by narikkadan
• 63,600 points
4,958 views
0 votes
1 answer
0 votes
1 answer

Convert word document to excel

n essence, I had to change the ...READ MORE

answered Oct 23, 2022 in Others by narikkadan
• 63,600 points
810 views
0 votes
1 answer

Generate a flat list of all excel cell formulas

Hello, you'll have to follow certain steps ...READ MORE

answered Feb 18, 2022 in Others by gaurav
• 23,260 points
554 views
0 votes
1 answer

Convert a number to a letter in C# for use in Microsoft Excel [duplicate]

If you are familiar with using formulas ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
836 views
0 votes
1 answer

Deleting duplicate rows in Excel using Epplus

You need to re-think this… the while ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
1,471 views
0 votes
1 answer

Export DataTable to Excel File

Add Interop References. First we need to ...READ MORE

answered Jun 9, 2022 in JQuery by gaurav
• 23,260 points
774 views
0 votes
1 answer

Removing duplicates from Excel rows by adding values of some columns

Copy the first three columns to another ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,600 points
901 views
0 votes
1 answer

Convert Excel and Word files to PDF Using ruby

 You can combine some: For excel files - ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,600 points
1,331 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP