none
How do I manipulate a checkbox in a Word doc file in vb.net

    Question

  • I have a vb.net 2008 aplication that writes to a Word 2010 file.

    I can easily change the text in the bookmarks BM1 and BM2. In the file there is also a checkbox (Kryss1) that

    I need to change between checked an unchecked. How do I do to change the checkbox from vb.net?

    Imports Microsoft.Office.Interop Imports Microsoft.Office.Interop.Word Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim oWord As Word.Application Dim oDoc As Word.Document oWord = CreateObject("Word.Application") oWord.Visible = True oDoc = oWord.Documents.Add("C:\Temp\WordTest.docx ") oDoc.Bookmarks("bm1").Range.Text = "TextBox1.Text" oDoc.Bookmarks("bm2").Range.Text = "TextBox2.Text" End Sub End Class


    Sunday, June 10, 2012 7:47 PM

Answers

  • object oMissing = System.Reflection.Missing.Value;
    object oEndOfDoc = "\\endofdoc";  //\endofdoc is a predifined bookmark.
    
    //Start Word and open template.
    Word._Application oWord;
    Word._Document oDoc;
    oWord = new Word.Application();
    oWord.Visible = true;
    object fileName = @"C:\Plant Tracker\AMP-108.doc";
    oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    .
    .
    .
    .
    //Call to checkbox creator.
    CreateChkBx("FinalChkBkMk", "Final", true, oDoc);
    CreateChkBx("InterimChkBx", "Interim", false, oDoc);           
    
    
    
    private void CreateChkBx(object oBookMk, string myCaption, bool isChecked, Word._Document myDoc)
    {
      object oTrue = true;
      object oFalse = false;
                
    
      Word.Range bookmarkRange = myDoc.Bookmarks.get_Item(ref oBookMk).Range;
      object oleClass = "Forms.CheckBox.1";
      object oRng = bookmarkRange;
      Word.OLEFormat of = myDoc.InlineShapes.AddOLEControl(ref oleClass, ref oRng).OLEFormat;
      Microsoft.Vbe.Interop.Forms.CheckBox cx = of.Object as Microsoft.Vbe.Interop.Forms.CheckBox;
      if (cx != null)
        {
          cx.Caption = myCaption;
          cx.Enabled = true;
          cx.AutoSize = true;
          if (isChecked == true)
          {
            cx.set_Value(ref oTrue);
          }
          else
          {
            cx.set_Value(ref oFalse);
          }
      }
    }

    I found this code on another thread:

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/7671f454-8614-48f4-90ac-6e21af1e316d/

    Sunday, June 10, 2012 8:37 PM

All replies

  • Have you tried using the Macro Recorder? The idea is to turn it on so that it watches you do some task with your mouse, recording your actions, translating them into code.  Then you go into the macro to see what code it generated and then try to reproduce  that code in your own program.
    Sunday, June 10, 2012 8:30 PM
  • object oMissing = System.Reflection.Missing.Value;
    object oEndOfDoc = "\\endofdoc";  //\endofdoc is a predifined bookmark.
    
    //Start Word and open template.
    Word._Application oWord;
    Word._Document oDoc;
    oWord = new Word.Application();
    oWord.Visible = true;
    object fileName = @"C:\Plant Tracker\AMP-108.doc";
    oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    .
    .
    .
    .
    //Call to checkbox creator.
    CreateChkBx("FinalChkBkMk", "Final", true, oDoc);
    CreateChkBx("InterimChkBx", "Interim", false, oDoc);           
    
    
    
    private void CreateChkBx(object oBookMk, string myCaption, bool isChecked, Word._Document myDoc)
    {
      object oTrue = true;
      object oFalse = false;
                
    
      Word.Range bookmarkRange = myDoc.Bookmarks.get_Item(ref oBookMk).Range;
      object oleClass = "Forms.CheckBox.1";
      object oRng = bookmarkRange;
      Word.OLEFormat of = myDoc.InlineShapes.AddOLEControl(ref oleClass, ref oRng).OLEFormat;
      Microsoft.Vbe.Interop.Forms.CheckBox cx = of.Object as Microsoft.Vbe.Interop.Forms.CheckBox;
      if (cx != null)
        {
          cx.Caption = myCaption;
          cx.Enabled = true;
          cx.AutoSize = true;
          if (isChecked == true)
          {
            cx.set_Value(ref oTrue);
          }
          else
          {
            cx.set_Value(ref oFalse);
          }
      }
    }

    I found this code on another thread:

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/7671f454-8614-48f4-90ac-6e21af1e316d/

    Sunday, June 10, 2012 8:37 PM
  • Or maybe this thread will help:

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/d83e98ec-283f-4c54-acda-723ce9111647

    Sunday, June 10, 2012 8:38 PM
  • Hi Skanepagen,

    Welcome to the MSDN forum.

    As most code from Jal2 is C#. If you are not familiar with C# code, you can use the C# to VB convertor.

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 12, 2012 8:41 AM