+ Reply to Thread
Results 1 to 7 of 7

Word Table Formatting problem

  1. #1
    Registered User
    Join Date
    08-29-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    6

    Word Table Formatting problem

    Hi,

    I am using the below code to format a table in a word document by selecting each column and removing the indenting from each column.

    However when I run the code for the first instance the below code works fine but from the second time table does not get formatted as desired. Indenting is not removed as per the below code.

    Also the table I m trying to format using the below code is the last table in the document.

    Please help.

    wdrange.Tables(1).Select

    wdrange.Tables(1).Columns(1).SetWidth ColumnWidth:=20, RulerStyle:=wdAdjustNone
    wdrange.Tables(1).Columns(2).SetWidth ColumnWidth:=260, RulerStyle:=wdAdjustNone
    wdrange.Tables(1).Columns(3).SetWidth ColumnWidth:=30, RulerStyle:=wdAdjustNone
    wdrange.Tables(1).Columns(4).SetWidth ColumnWidth:=60, RulerStyle:=wdAdjustNone


    wdrange.Tables(1).Columns(1).Select
    wdrange.ParagraphFormat.Space1
    With wdrange.ParagraphFormat
    .LeftIndent = InchesToPoints(0)
    .RightIndent = InchesToPoints(0)
    .SpaceBefore = 5
    .SpaceBeforeAuto = True
    .SpaceAfter = 5
    .SpaceAfterAuto = True
    .LineSpacingRule = wdLineSpaceSingle
    .Alignment = wdAlignParagraphCenter
    .WidowControl = True
    .KeepWithNext = False
    .KeepTogether = False
    .PageBreakBefore = False
    .NoLineNumber = False
    .Hyphenation = True
    .FirstLineIndent = InchesToPoints(0)
    .OutlineLevel = wdOutlineLevelBodyText
    .CharacterUnitLeftIndent = 0
    .CharacterUnitRightIndent = 0
    .CharacterUnitFirstLineIndent = 0
    .LineUnitBefore = 0
    .LineUnitAfter = 0
    .MirrorIndents = False
    .TextboxTightWrap = wdTightNone
    End With

    Thanks

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Word Table Formatting problem

    You don't need to select anything. Also, your paragraph formatting isn't restricted to the table - it applies to all of whatever you've defined as wdrange. Try:
    Please Login or Register  to view this content.
    PS: When posting code, please use the code tags, indicated by the # symbol on the reply toolbar.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    08-29-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Word Table Formatting problem

    Thanks, I will give it a try.

    Next time for sure I will use the code tags.

  4. #4
    Registered User
    Join Date
    08-29-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Word Table Formatting problem

    Hi Paul,

    I tried the changes above but for the first instance everything worked fine but when I ran the macro again the indenting part does not worked.

    Please advice.

    I am writing the data from excel sheet to word document adding table to word and pasting the cells data from excel to each cells in the word table.

    Thanks

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Word Table Formatting problem

    Your macro is coded to format whatever is the first table in the range you have defined as wdrange. Without knowing what that range refers to and whether the table you want to update is, in fact, the first table in that range, it's impossible to diagnose the problem.

  6. #6
    Registered User
    Join Date
    08-29-2012
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Word Table Formatting problem

    The wdrange is defined as #worddoc .selection# at the start of the code where wordoc is the document.

    And I use wdrange for all the data to be entered in the word document.

    I have created 3 tables in the document before this table and the problem appears only for the last table. Also all the above 3 tables have 2 column and the last one has 4 columns.

    I am using the #wdrange.tables(1)# for this last table as well.

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Word Table Formatting problem

    As I said in my last post:
    Your macro is coded to format whatever is the first table in the range you have defined as wdrange.
    So regardless of how many tables you create, only the first one with be processed.

    Assuming the tables are added progressively and you're endeavouring to format each one as it created, try changing:
    With wdRange.Tables(1)
    to:
    With wdRange.Tables(wdRange.Tables.Count)

    If that doesn't resolve the problem, you're going to have to show more of your code - specifically that showing the table creation/addition process.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1