none
Building a signature button RRS feed

  • Question

  • I was tasked with creating a custom control that users could simply drag onto a Word document give it some text, like Add signature here, and when clicked it displays a signature pad, then afterwards replaces the button with an image of the signature. First of all is this even possible? I have created a really basic demo using Visual studio 2017 and Word 2016, although it is very specific to that word document and I have to hard code the button location. 

    Also the framework for writing C# code for office 2016 apps is awful. All the objects make zero sense and half the time are literally objects, not special classes relating to what they are. Another issue I am facing is the framework is not stable in terms of repeating as task with two different results. I can do this, as the comment explains calling the remove method fails for another button created the same way. Can anyone give me some guidance on how to do something like this?

    When clicking the first button everything works fine. When clicking the second button I get a null reference expection on the remove 

    public partial class ThisDocument
        {
            //This needs to be set to where the signatures will be temp stored when the ink-pad app closes, and when word inserts the image.
            private const String SignatureRootPath = @"C:\Users\baile\Desktop\Signature.bmp";
    
            //This is where the EXE will be for the ink-pad app.
            private const String InkPadExePath = @"C:\SignaturePad.exe";
    
            private Word.Range ClientRange, WitnessRange;
            private Microsoft.Office.Tools.Word.Controls.Button ClientBtn;
            private Microsoft.Office.Tools.Word.Controls.Button WitnessBtn;
            
    
            private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
                //For the client signature
                var ClientSignature = this.SelectContentControlsByTag("ClientSig")[1];
                ClientRange = ClientSignature.Range;
                //For some reason delete doesn't remove the control from the document.
                ClientSignature.Cut();
                
                ClientBtn = this.Controls.AddButton(ClientRange, 100, 25, "clientSignatureButton");
    
                ClientBtn.Text = "Insert Client Signature";
    
                ClientBtn.Click += GetSignature;
                //========================================================================
    
    
                var WitnessSignature = this.SelectContentControlsByTag("WitnessSig")[1];
                WitnessRange = WitnessSignature.Range;
                //For some reason delete doesn't remove the control from the document.
                WitnessSignature.Cut();
    
                WitnessBtn = this.Controls.AddButton(WitnessRange, 100, 25, "witnessSignatureButton");
    
                WitnessBtn.Text = "Insert Witness Signature";
    
                WitnessBtn.Click += GetSignature;
    
            }
            
    
            private void GetSignature(object sender, EventArgs e) {
                var signature = System.Diagnostics.Process.Start(InkPadExePath);
    
                signature.WaitForExit();
    
                
                this.Controls.Remove(sender);
    
                //Adds the image.
                ClientRange.InlineShapes.AddPicture(SignatureRootPath);
    
                //Delete image
                DeleteSignatureImageFile(SignatureRootPath);
    
            }
    
            private void DeleteSignatureImageFile(String Path)
            {
                System.IO.File.Delete(Path);
            }
    
            private void ThisDocument_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            
    
            #region VSTO Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisDocument_Startup);
                this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
            }
    
            #endregion
        }

    Monday, April 17, 2017 2:16 PM

All replies

  • Hello,

    Do you get any error? What is the error message? 

    I could remove the button when clicking using the following code.

            private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
                Microsoft.Office.Tools.Word.Controls.Button button1 =
          this.Controls.AddButton(25, 75, 80, 30, "button1");
                button1.Text = "button1";
                button1.Click += new EventHandler(button1_Click);
    
                Microsoft.Office.Tools.Word.Controls.Button button2 =
     this.Controls.AddButton(50, 150, 80, 30, "button2");
                button2.Text = "button2";
                button2.Click += new EventHandler(button1_Click);
    
            }
            void button1_Click(object sender, EventArgs e)
            {
                this.Controls.Remove(sender);
    
                //Microsoft.Office.Tools.Word.Controls.Button clickedButton =
                //    (Microsoft.Office.Tools.Word.Controls.Button)sender;
    
                //clickedButton.Delete();
            }

    Do you want to remove all controls? Unfortunately, it doesn't support Controls.Clear(); method to delete all controls. We need to loop through the controls and delete it.

                foreach (Microsoft.Office.Tools.Word.Controls.Button btn in this.Controls.OfType<Microsoft.Office.Tools.Word.Controls.Button>().ToList())
                {
                    this.Controls.Remove(btn);
                }

    Regards,

    Celeste


    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.

    Tuesday, April 18, 2017 8:03 AM
    Moderator