none
[C#] Add the Label control to the MS Word document RRS feed

  • Question

  • Hi! How can I programatically add the label control to the MS Word document?

    Thanks for help! 

    Tuesday, June 30, 2015 2:37 PM

Answers

  •             Word.Document wdDoc = wdApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                Word.Range rng = wdDoc.Content;
                rng.Collapse(ref oCollapseEnd);
                object oleLabelClass = "Forms.Label.1";
                object oRng = rng;
                Word.InlineShape ilsLabel = wdDoc.InlineShapes.AddOLEControl(ref oleLabelClass, ref oRng);
    


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 30, 2015 4:13 PM
    Moderator
  • Hi Ya

    Nick has shown you how the object model is designed to work with its "native" VB. VB languages can work with late-binding: inferring and "forcing" the data type at run-time.

    This is a problem for C#, however, as it expects/wants strict typing. While recent versions of C# can infer the data type at compile time, I don't believe this is possible going through the Object property of an ActiveX control as the type only becomes available at run-time. Following is a code snippet that builds on the code I provided in my previous reply that demonstrates firstly, what happens if you try the "VB" way and secondly, how you have to do it - using PInvoke.

                Word.InlineShape ilsLabel = wdDoc.InlineShapes.AddOLEControl(ref oleLabelClass, ref oRng);
                var caption = ilsLabel.OLEFormat.Object;
                //uncomment the following line to test VB-type binding
                //caption = "test";
                //the following two lines illustrate "PInvoke" - 
                //late-binding for C#
                //Comment them out to test the VB approach
                object[] args = {"test"};
                caption.GetType().InvokeMember("Caption", BindingFlags.SetProperty, null, caption, args);
    

    You'll find a number of PInvoke examples for Office, in this forum and in the VSTO forum. You need this approach for all properties of the LABEL control (as opposed to the InlineShape object in which the ActiveX control is embedded).


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 1, 2015 4:16 PM
    Moderator

All replies

  • Hi Ya

    We need more information. Could you please specify which control you mean? If you're unsure how to do that, which menu commands are used to insert it working as an end-user?

    Also, if you answer this question for yourself before anyone can help you, please so indicate so as to not waste our time, as happened with your previous question.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 30, 2015 3:35 PM
    Moderator
  • It is located on the Developer panel > Controls > Label. I am using MS Word 2010.
    Tuesday, June 30, 2015 3:53 PM
  •             Word.Document wdDoc = wdApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                Word.Range rng = wdDoc.Content;
                rng.Collapse(ref oCollapseEnd);
                object oleLabelClass = "Forms.Label.1";
                object oRng = rng;
                Word.InlineShape ilsLabel = wdDoc.InlineShapes.AddOLEControl(ref oleLabelClass, ref oRng);
    


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 30, 2015 4:13 PM
    Moderator
  • Thanks, it works. But how can I change properties of this label? I want to change font, enabled, height, autosize, wordwrap and caption.
    • Edited by YaRyaguzov Tuesday, June 30, 2015 5:57 PM
    Tuesday, June 30, 2015 5:12 PM
  • You can edit the properties by getting the OLE object and setting the values as you would on any winforms label.

    Quick VBA test

    Dim shp As InlineShape
    Set shp = ActiveDocument.InlineShapes.AddOLEControl(ClassType:="Forms.Label.1")
    shp.OLEFormat.Object.Caption = "Hello World"

    You don't get intellisense on the Object as it is late bound, but if you know the label properties you can change them quite easily.

    I think you will need to set things like the position on the shape wrapper though

    Wednesday, July 1, 2015 11:17 AM
  • Hi Ya

    Nick has shown you how the object model is designed to work with its "native" VB. VB languages can work with late-binding: inferring and "forcing" the data type at run-time.

    This is a problem for C#, however, as it expects/wants strict typing. While recent versions of C# can infer the data type at compile time, I don't believe this is possible going through the Object property of an ActiveX control as the type only becomes available at run-time. Following is a code snippet that builds on the code I provided in my previous reply that demonstrates firstly, what happens if you try the "VB" way and secondly, how you have to do it - using PInvoke.

                Word.InlineShape ilsLabel = wdDoc.InlineShapes.AddOLEControl(ref oleLabelClass, ref oRng);
                var caption = ilsLabel.OLEFormat.Object;
                //uncomment the following line to test VB-type binding
                //caption = "test";
                //the following two lines illustrate "PInvoke" - 
                //late-binding for C#
                //Comment them out to test the VB approach
                object[] args = {"test"};
                caption.GetType().InvokeMember("Caption", BindingFlags.SetProperty, null, caption, args);
    

    You'll find a number of PInvoke examples for Office, in this forum and in the VSTO forum. You need this approach for all properties of the LABEL control (as opposed to the InlineShape object in which the ActiveX control is embedded).


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 1, 2015 4:16 PM
    Moderator
  • ActiveX controls are represented as either Shape objects or InlineShape objects in Microsoft Word. To modify the properties for an ActiveX control, you use the Object property of the OLEFormat object for the specified shape or inline shape. Firstly you can add refence "Microsoft.Vbe.Interop.Forms", then try like below codes:

    Word.Document wdDoc = wdApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
    Word.Range rng = wdDoc.Content;
    
    rng.Collapse(ref oCollapseEnd);
    
    object oleLabelClass = "Forms.Label.1";
    
    object oRng = rng;
    
    Word.InlineShape ilsLabel = wdDoc.InlineShapes.AddOLEControl(ref oleLabelClass, ref oRng);
    
    Microsoft.Vbe.Interop.Forms.Label cx = ilsLabel.OLEFormat.Object as Microsoft.Vbe.Interop.Forms.Label;
    
    if (cx != null)
    
    {
    
       cx.Caption = "Testing a label";
    
    }
    
    

    Thursday, July 2, 2015 7:43 AM