none
Word Automation Cannot Display Tables Borders RRS feed

  • Question

  • This is my first post. I hope this is the right forum!

    I am using word automation to create a document and add a table. This works ok, but I cannot seem to be able to change the table borders despite having spent several hours researching on the internet. My code is as follows (note it is a bit messy because I have been tring lots of different things):

    typelib Word "Word.Document"
    
    // Use the #ifdef to check if word is on the pc running this script - doesn't work if it isn't
    #ifdef Word
    
    class MainClass {
      Word::Application app;
      void Main(CaseClass c) {
        SystemClass::ClearConsole(1);
        if (app.Create())
        {
          app.SetVisible(true);
          Word::Documents docs = app.Documents();
          Word::Document doc = docs.Add();
          Word::Table table;
          range = selection.Range();
          table = selection.Tables().Add(range, 3, 4);
          table.Cell(1,1).Range().SetText("ss");
          table.Cell(1,2).Range().SetText("This");
    
    
          //Word::Border aborder;
          int j;
          String str;
          bool b;
          table.Borders().InsideLineStyle() = Word::wdLineStyleDouble;
          table.Borders().OutsideLineStyle() = Word::wdLineStyleDouble;
          foreach( Word::Border aborder in table.Borders()){
            str = aborder.LineStyle();
            aborder.Visible() = true;
            b = aborder.Visible();
            aborder.LineStyle() = Word::wdLineStyleSingle;
            str = aborder.LineStyle();
            aborder.LineWidth() = Word::wdLineWidth050pt;
            aborder.Color() = Word::wdColorAutomatic;
            j++;
          }
          table.Borders().Enable();
          table.Select();
          selection.Borders().Item(Word::wdBorderTop).LineStyle() = Word::wdLineStyleDouble;
        

    The word do is created ok, as is the table. Text is added to the table, but no matter what I try, the table borders do not change. It's as if the border properties are read only and are not being set.

    Any ideas anyone?

    Wednesday, September 19, 2012 9:10 PM

Answers

  • Many thanks to Tom and Cindy for your quick replies.

    I am using a C++ like language which has made use of the Word COM interface.

    I have tried to change my code as you suggested, but it didn't work. I have found that I have to use the method

    aborder.SetLineStyle(Word.WdLineStyle.wdLineStyleSingle);
    

    instead of

    aborder.LineStyle = Word.WdLineStyle.wdLineStyleSingle;

    It works!. I don't know why. I don't know enough about Word and COM. Can you explain.

    I was having the same problem with inLineShapes when trying to set the height. I have to use the

    ishape.SetHeight(50);

    instead of ishape.Height() = 50;

    Any explanations anyone?

    Thanks

    Thursday, September 20, 2012 3:42 PM
  • Hi hiha

    <<Can you explain.>>

    This has to do with the programming language you're using, whatever it is. It apparently makes some kind of compatible "interface" - similar to how .NET creates IAs - for the COM type libraries. So what you're having to use is a function of that programming language. You'd need to ask in a forum that supports that language about how it's "interpreting" the Word type library.

    C# turns some of the Word properties into get and set methods when the properties accept/expect parameters. As far as C# is concerned, a property shouldn't work with parameters. So TlbImp.exe makes adjustments accordingly and presents the developer with get_ and set_ methods.

    It appears that the language you're using doesn't like to assign values to properties using equals (=). Instead, it turns the properties into methods and changes the names to Set (and possibly Get to read the values?) But that's just a guess on my part.

    Glad you were able to solve your problem :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 20, 2012 4:06 PM
    Moderator

All replies

  • Hi hihaveaniceday,

    Thanks for posting in the MSDN Forum.

    l not too sure about which kind of C++ you used there. I suppose you use CLR C++ due to your code seems more like it. I will provide your a code snippet to create a new document and a table with borders:

    #pragma once
    
    
    #define Word Microsoft::Office::Interop::Word
    
    namespace WordTableBoards {
    
     using namespace System;
     using namespace System::ComponentModel;
     using namespace System::Collections;
     using namespace System::Windows::Forms;
     using namespace System::Data;
     using namespace System::Drawing;
    
     /// <summary>
     /// Summary for Form1
     /// </summary>
     public ref class Form1 : public System::Windows::Forms::Form
     {
     public:
      Form1(void)
      {
       InitializeComponent();
       //
       //TODO: Add the constructor code here
       //
      }
    
     protected:
      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      ~Form1()
      {
       if (components)
       {
        delete components;
       }
      }
     private: System::Windows::Forms::Button^  button1;
     protected: 
    
     private:
      /// <summary>
      /// Required designer variable.
      /// </summary>
      System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      void InitializeComponent(void)
      {
       this->button1 = (gcnew System::Windows::Forms::Button());
       this->SuspendLayout();
       // 
       // button1
       // 
       this->button1->Location = System::Drawing::Point(278, 13);
       this->button1->Name = L"button1";
       this->button1->Size = System::Drawing::Size(75, 23);
       this->button1->TabIndex = 0;
       this->button1->Text = L"button1";
       this->button1->UseVisualStyleBackColor = true;
       this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
       // 
       // Form1
       // 
       this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
       this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
       this->ClientSize = System::Drawing::Size(672, 67);
       this->Controls->Add(this->button1);
       this->Name = L"Form1";
       this->Text = L"Form1";
       this->ResumeLayout(false);
    
      }
    #pragma endregion
     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
         Word::Application^ wdApp = gcnew Word::Application();
         wdApp->Visible = true;
         Object^ missing = Type::Missing;
         Word::Document^ wdDoc = wdApp->Documents->Add(missing,missing,missing,missing);
         Word::Selection^ wdSelection = wdApp->Selection;
         Word::Table^ wdTable = wdSelection->Tables->Add(wdSelection->Range,3,4,missing,missing);
         wdTable->Borders[Word::WdBorderType::wdBorderHorizontal]->LineStyle = Word::WdLineStyle::wdLineStyleSingle;
         wdTable->Borders[Word::WdBorderType::wdBorderVertical]->LineStyle = Word::WdLineStyle::wdLineStyleSingle;
         wdTable->Borders[Word::WdBorderType::wdBorderTop]->LineStyle = Word::WdLineStyle::wdLineStyleSingle;
         wdTable->Borders[Word::WdBorderType::wdBorderRight]->LineStyle = Word::WdLineStyle::wdLineStyleSingle;
         wdTable->Borders[Word::WdBorderType::wdBorderLeft]->LineStyle = Word::WdLineStyle::wdLineStyleSingle;
         wdTable->Borders[Word::WdBorderType::wdBorderBottom]->LineStyle = Word::WdLineStyle::wdLineStyleSingle;
        }
     };
    }

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 20, 2012 6:57 AM
    Moderator
  • Hi hiha

    I don't recognize the programming language, is it Java?

    I have no trouble "interpreting" it as C#. I've made some small changes that should get you up and running. The most pertinent, I think, is that you have to set Enable = -1 (true). On my system, I was also getting diagonal borders through all the cells, so I added a couple of lines to suppress that.

    {
        Word.Documents docs = wdApp.Documents;
        Word.Document doc = docs.Add(ref missing, ref missing, ref missing, ref missing);
        Word.Range range = wdApp.Selection.Range;
        Word.Table table = wdApp.Selection.Tables.Add(range, 3, 4, ref missing, ref missing);
        table.Cell(1, 1).Range.Text = "ss";
        table.Cell(1, 2).Range.Text = "This";
        //Word.Border aborder;
        int j = 0;
        String str;
        bool b;
        table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleDouble;
        table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDouble;
        foreach (Word.Border aborder in table.Borders)
        {
            System.Diagnostics.Debug.Print(aborder.LineStyle.ToString());
            aborder.Visible = true;
            System.Diagnostics.Debug.Print(aborder.Visible.ToString());
            aborder.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
            System.Diagnostics.Debug.Print(aborder.LineStyle.ToString());
            aborder.LineWidth = Word.WdLineWidth.wdLineWidth050pt;
            aborder.Color = Word.WdColor.wdColorAutomatic;
            j++;
        }
        table.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalDown].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
        table.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderDiagonalUp].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
        table.Borders.Enable = -1;
        table.Select();
        wdApp.Selection.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
    }
    //end of code


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 20, 2012 9:02 AM
    Moderator
  • Hi Cindy,

    It isn't Java, we will not use "::" in Java. As far as I know that only C++ will use it.

    T.X.


    志异征诛,三让两家王朝
    功同开辟,一桮万古江南

    Thursday, September 20, 2012 11:28 AM
  • Thanks, TX. First time I've ever found C++ to be so "readable", then. I usually struggle a lot more <g>

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 20, 2012 11:40 AM
    Moderator
  • Many thanks to Tom and Cindy for your quick replies.

    I am using a C++ like language which has made use of the Word COM interface.

    I have tried to change my code as you suggested, but it didn't work. I have found that I have to use the method

    aborder.SetLineStyle(Word.WdLineStyle.wdLineStyleSingle);
    

    instead of

    aborder.LineStyle = Word.WdLineStyle.wdLineStyleSingle;

    It works!. I don't know why. I don't know enough about Word and COM. Can you explain.

    I was having the same problem with inLineShapes when trying to set the height. I have to use the

    ishape.SetHeight(50);

    instead of ishape.Height() = 50;

    Any explanations anyone?

    Thanks

    Thursday, September 20, 2012 3:42 PM
  • Hi hiha

    <<Can you explain.>>

    This has to do with the programming language you're using, whatever it is. It apparently makes some kind of compatible "interface" - similar to how .NET creates IAs - for the COM type libraries. So what you're having to use is a function of that programming language. You'd need to ask in a forum that supports that language about how it's "interpreting" the Word type library.

    C# turns some of the Word properties into get and set methods when the properties accept/expect parameters. As far as C# is concerned, a property shouldn't work with parameters. So TlbImp.exe makes adjustments accordingly and presents the developer with get_ and set_ methods.

    It appears that the language you're using doesn't like to assign values to properties using equals (=). Instead, it turns the properties into methods and changes the names to Set (and possibly Get to read the values?) But that's just a guess on my part.

    Glad you were able to solve your problem :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 20, 2012 4:06 PM
    Moderator