none
Need to find table Margin RRS feed

  • Question

  • Hi every1,

    using word 2002, VSIO 2010 c#

    i hve a big problem

    i need to perform a check

    my table margin should not exceed that of page margin (my table margin should be within page margin limit)

    is there a way i can do that???

    plz help me :(

    thks in advance

    Mush

    :)

    Friday, January 21, 2011 7:51 AM

Answers

  • thks Cindy n Paul

    made few changes to the code

    n its wrking fine nw

    thks 4 ur help :)

    here it is

                    //checking if table is larger than page margin
                    _app.ScreenUpdating = false;

                    Single sLMargin = 0;
                    Single sRMargin = 0;
                    Single sPgWdth = 0;
                    Single sTblWdthStart = 0;
                    Single sTblWdthEnd = 0;
                    Range tRng = _app.Selection.Range;
                    WdParagraphAlignment alignment;

                    foreach (Table oTable in _app.ActiveDocument.Tables)
                    {
                        sLMargin = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.LeftMargin;
                        sRMargin = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.RightMargin;
                        sPgWdth = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.PageWidth;
                        tRng = oTable.Cell(1, 1).Range;
                        alignment = oTable.Cell(1, 1).Range.ParagraphFormat.Alignment;

                        if (oTable.Cell(1, 1).Range.ParagraphFormat.Alignment == WdParagraphAlignment.wdAlignParagraphLeft)
                        {
                            sTblWdthStart = tRng.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);
                        }
                        else
                        {
                            oTable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                            sTblWdthStart = tRng.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);
                            oTable.Cell(1, 1).Range.ParagraphFormat.Alignment = alignment;
                        }

                        do
                        {
                            tRng.Move(WdUnits.wdCell, 1);
                        }
                        while ((tRng.Cells[1].RowIndex == 1));
                       
                        tRng.MoveEnd(WdUnits.wdCharacter, -1);
                        sTblWdthEnd = tRng.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);

                        if((sTblWdthStart < sLMargin) || (sTblWdthEnd > (sPgWdth -  sRMargin)))
                        {
                             MessageBox.Show("Table " + i + " is too wide by " + (sTblWdth - sPrnWdth) + " points.", "Magin", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        }
                    }
                    _app.ScreenUpdating = true;

    hve a gr8 afternoon

    Mush

    :)

    • Marked as answer by StarMush Monday, January 24, 2011 11:25 AM
    Monday, January 24, 2011 11:25 AM

All replies

  • Hi StarMush

    The following macro will scale the width of the first table in the document to the page margins:

    Sub Demo()
    With ActiveDocument.Tables(1)
      .PreferredWidthType = wdPreferredWidthPercent
      .PreferredWidth = 100
    End With
    End Sub

    All column widths are adjusted proportionately.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Friday, January 21, 2011 8:45 AM
  • hi Macropod, thks 4 replying

    but the prob is that i need to detect if table border goes beyond margin limit (need to know this part)

    once detected, will write it in a report( this part ok)

    is there a way to do that???

    plz help me on that one

    thks in advance dear

    Mush

    :)

     

    Friday, January 21, 2011 10:33 AM
  • Hi StarMush,

    To test every table in a document, try something along the lines of:

    Sub TableTester()
    Application.ScreenUpdating = False
    Dim sLMargin As Single, sRMargin As Single, sGutter As Single
    Dim sPgWdth As Single, sPrnWdth As Single, sTblWdth As Single
    Dim oTable As Table, tRng As Range, i As Integer
    With ActiveDocument
      For Each oTable In .Tables
        i = i + 1
        With .Sections(oTable.Range.Information(wdActiveEndSectionNumber)).PageSetup
          sLMargin = .LeftMargin
          sRMargin = .RightMargin
          sGutter = .Gutter
          sPgWdth = .PageWidth
        End With
        sPrnWdth = sPgWdth - sLMargin - sRMargin - sGutter
        Set tRng = oTable.Cell(1, 1).Range
        sTblWdth = tRng.Information(wdHorizontalPositionRelativeToPage)
        Do While tRng.Cells(1).RowIndex = 1
          tRng.Move Unit:=wdCell, Count:=1
        Loop
        tRng.MoveEnd wdCharacter, -1
        sTblWdth = tRng.Information(wdHorizontalPositionRelativeToPage) - sTblWdth
        If sTblWdth - sPrnWdth > 0 Then
          MsgBox "Table " & i & " is too wide by " & sTblWdth - sPrnWdth & " points."
        End If
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Proposed as answer by macropodMVP Monday, January 24, 2011 11:37 PM
    Friday, January 21, 2011 12:57 PM
  • hi, Macropod

    thks 4 the reply and the example

    needed it in c# so tried to convert it to c#

    but mayb i've gone wrng smewhere because its nt wrking :(

    can u plz check it 4 me dear

    here it is:

                    /*********************************************************************************/
                    //checking if table is larger than page margin
                    _app.ScreenUpdating = false;

                    Single sLMargin = 0;
                    Single sRMargin = 0;
                    Single sGutter = 0;
                    Single sPgWdth = 0;
                    Single sPrnWdth =0;
                    Single sTblWdth = 0;
                    Range tRng = _app.Selection.Range;
                    int i = 0;

                    foreach (Table oTable in _app.ActiveDocument.Tables)
                    {
                        i += 1;

                        sLMargin = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.LeftMargin;
                        sRMargin = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.RightMargin;
                        sGutter = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.Gutter;
                        sPgWdth = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.PageWidth;

                        sPrnWdth = sPgWdth - sLMargin - sRMargin - sGutter;
                        tRng = oTable.Cell(1, 1).Range;
                        sTblWdth = tRng.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);

                        do
                        {
                            tRng.Move(WdUnits.wdCell, 1);
                        }
                        while((tRng.Cells[1].RowIndex == 1));
                       
                        tRng.MoveEnd(WdUnits.wdCharacter, -1);
                        sTblWdth = tRng.get_Information(WdInformation.wdHorizontalPositionRelativeToPage) - sTblWdth;
                       
                        if((sTblWdth - sPrnWdth) > 0)
                        {
                            MessageBox.Show("Table " + i + " is too wide by " + (sTblWdth - sPrnWdth) + " points.", "Magin", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        }
                    }
                    _app.ScreenUpdating = true;

                    /*********************************************************************************/

    Mayb i've gone wrong with the Do While n after

    plz help me dear

    thks in advance 4 ur precious time

    Mush

    :)

    ps: one small question: the With you used in your VB code, Is there any equivalent of it in c#??? thks

    • Edited by StarMush Monday, January 24, 2011 7:35 AM missed this part: _app.ScreenUpdating = true;
    Monday, January 24, 2011 6:14 AM
  • Hi Mush

    1. Just saying "it isn't working" doesn't help others track down problems you may be having. Please explain HOW it doesn't work

    2. Paul's example had do while: the check to continue is before the loop repeats. Your code has the check after the loop. If you suspect the problem is in the loop, then you need to move the check to the front of the loop. If C# doesn't have a looping option to check in the "do", then put the check in the first line of the loop and, if the check is positive, "break" out of the loop before it can repeat. That would make your code better correspond to Paul's.


    Cindy Meister, VSTO/Word MVP
    Monday, January 24, 2011 7:17 AM
    Moderator
  • hi Cindy,

    been testing z code with different tables

    wat i hve noticed is that the code works fine n detects that the table is larger than margin iff the alignment of 1st row of the table (which it it using in the do while) is set to Left.

    Earlier wen i tested i tried with table with alignmnet center, thats why i said it wasnt working (sry)

    but when i tested wiz different tables and different alignments, then notice that when alignment of 1st row is center, right or even justified it would not detect that table is largin than page margin.

    the only case it will detect is when alignment is set to Left

    does sme1 hve an idea abt what i can do in this situation???

    plz help me

    thks in advance

    Mush

    :)

    by the way the do while is fine, thats how it is in C#, searched a bit on that

    Monday, January 24, 2011 7:34 AM
  • Thanks Cindy,

    I hadn't had time to check the code and, besides, I don't do C#.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Monday, January 24, 2011 8:47 AM
  • thks Cindy n Paul

    made few changes to the code

    n its wrking fine nw

    thks 4 ur help :)

    here it is

                    //checking if table is larger than page margin
                    _app.ScreenUpdating = false;

                    Single sLMargin = 0;
                    Single sRMargin = 0;
                    Single sPgWdth = 0;
                    Single sTblWdthStart = 0;
                    Single sTblWdthEnd = 0;
                    Range tRng = _app.Selection.Range;
                    WdParagraphAlignment alignment;

                    foreach (Table oTable in _app.ActiveDocument.Tables)
                    {
                        sLMargin = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.LeftMargin;
                        sRMargin = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.RightMargin;
                        sPgWdth = _app.ActiveDocument.Sections[oTable.Range.get_Information(WdInformation.wdActiveEndSectionNumber)].PageSetup.PageWidth;
                        tRng = oTable.Cell(1, 1).Range;
                        alignment = oTable.Cell(1, 1).Range.ParagraphFormat.Alignment;

                        if (oTable.Cell(1, 1).Range.ParagraphFormat.Alignment == WdParagraphAlignment.wdAlignParagraphLeft)
                        {
                            sTblWdthStart = tRng.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);
                        }
                        else
                        {
                            oTable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                            sTblWdthStart = tRng.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);
                            oTable.Cell(1, 1).Range.ParagraphFormat.Alignment = alignment;
                        }

                        do
                        {
                            tRng.Move(WdUnits.wdCell, 1);
                        }
                        while ((tRng.Cells[1].RowIndex == 1));
                       
                        tRng.MoveEnd(WdUnits.wdCharacter, -1);
                        sTblWdthEnd = tRng.get_Information(WdInformation.wdHorizontalPositionRelativeToPage);

                        if((sTblWdthStart < sLMargin) || (sTblWdthEnd > (sPgWdth -  sRMargin)))
                        {
                             MessageBox.Show("Table " + i + " is too wide by " + (sTblWdth - sPrnWdth) + " points.", "Magin", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        }
                    }
                    _app.ScreenUpdating = true;

    hve a gr8 afternoon

    Mush

    :)

    • Marked as answer by StarMush Monday, January 24, 2011 11:25 AM
    Monday, January 24, 2011 11:25 AM