Results 1 to 3 of 3

Thread: Helpp using Words macros (uugggghhhh)

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Helpp using Words macros (uugggghhhh)

    Hi,

    I consider myself an experienced programmer in vba.. unfortunately that is in Access VBA which seems to be a hell of a lot easier than Word or Excel VBA.

    Currently I am exporting data into a template built by code and shoved into Word. Initially I tried Words VBA (using record macro) but I was advised to use the proper objects (besides it killed the computer). Any how, I moved to html instead - easier. But it didn't like being transported back into word so I am having to try to do the templates in word objects.

    The help file is practically useless as it keeps refering back to the selection object, which is used in the current doc and not via the code :/

    I have the following code, which is in a loop.
    VB Code:
    1. Set objWTable = Nothing
    2.                         Set objWTable = objWDoc.Tables.Add(objWRange, 5, 2)  'range,rows,cols
    3.                         objWTable.Cell(1, 1).Width = objWord.MillimetersToPoints(190) 'row,col (starts at 1)
    4.                         objWTable.Cell(1, 2).Width = objWord.MillimetersToPoints(10)  'row,col
    5.                         objWTable.Cell(1, 1).Height = objWord.MillimetersToPoints(20) 'row,col (starts at 1)
    6.                         objWTable.Cell(1, 2).Height = objWord.MillimetersToPoints(20)   'row,col
    7.                         objWTable.Cell(2, 1).Width = objWord.MillimetersToPoints(80) 'row,col (starts at 1)
    8.                         objWTable.Cell(2, 2).Width = objWord.MillimetersToPoints(120)  'row,col
    9.                         objWTable.Cell(2, 1).Height = objWord.MillimetersToPoints(82) 'row,col (starts at 1)
    10.                         objWTable.Cell(2, 2).Height = objWord.MillimetersToPoints(82)   'row,col
    11.                         objWTable.Cell(3, 1).Width = objWord.MillimetersToPoints(80) 'row,col (starts at 1)
    12.                         objWTable.Cell(3, 2).Width = objWord.MillimetersToPoints(120)  'row,col
    13.                         objWTable.Cell(3, 1).Height = objWord.MillimetersToPoints(82) 'row,col (starts at 1)
    14.                         objWTable.Cell(3, 2).Height = objWord.MillimetersToPoints(82)   'row,col
    15.                         objWTable.Cell(4, 1).Width = objWord.MillimetersToPoints(80)    'row,col (starts at 1)
    16.                         objWTable.Cell(4, 2).Width = objWord.MillimetersToPoints(120)   'row,col
    17.                         objWTable.Cell(4, 1).Height = objWord.MillimetersToPoints(82)       'row,col (starts at 1)
    18.                         objWTable.Cell(4, 2).Height = objWord.MillimetersToPoints(82)       'row,col
    19.                         objWTable.Cell(5, 1).Width = objWord.MillimetersToPoints(100)       'row,col (starts at 1)
    20.                         objWTable.Cell(5, 2).Width = objWord.MillimetersToPoints(100)       'row,col
    21.                         objWTable.Cell(5, 1).Height = objWord.MillimetersToPoints(20)       'row,col (starts at 1)
    22.                         objWTable.Cell(5, 2).Height = objWord.MillimetersToPoints(20)       'row,col
    23.                        
    24.                         For lngX = 1 To 5
    25.                             For lngY = 1 To 2
    26.                                 objWTable.Cell(lngX, lngY).VerticalAlignment = 1 'center
    27.                                 objWTable.Cell(lngX, lngY).Range.ParagraphFormat.Alignment = 1 'wdAlignParagraphCenter
    28.                             Next
    29.                         Next
    30.                         objWTable.Cell(1, 1).Range.ParagraphFormat.Alignment = 0
    31.                         objWTable.Cell(5, 1).Range.ParagraphFormat.Alignment = 0
    32.                         objWTable.Cell(5, 2).Range.ParagraphFormat.Alignment = 2
    33.                        
    34.                         objWTable.Rows.Alignment = 1 'wdAlignRowCenter
    This creates my table and reorganises it to the correct widths etc.
    First time its called, works fine.
    However the second time its called it creates a new table BUT changes the sizes of the first table.

    I was under the impression that:
    VB Code:
    1. Set objWTable = objWDoc.Tables.Add(objWRange, 5, 2)  'range,rows,cols
    Returned a table object, and I thoughtit returned the object just created - is this not the case?


    Also I have used the same creation method but for a new table in a cell. This created a table of one row and how ever many columns, even if you specify more than one row. Is this a bug?

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    I'm not sure what you are trying to do. The problem may be the value you are setting for objWRange. If you run this code in a loop without resetting objWRange, here's what Word is doing:

    Word adds a new table. objWTable becomes the new table and objWRange becomes the range of the table. The second time through, objWRange is the range of the first table. Word adds a new table to this range. Because objWRange is a whole table, Word uses the start of objWRange: Cell 1, 1. Word puts a new table in Cell 1, 1. Word is then tries to set the sizes of the new table. You are asking it to expand the table to 200 X 286 inside a cell that is 190 X 20. Obviously that is not possible. So, Word expands Cell 1, 1 as far right and and as far down as it can to accomidate the size of the new table. It makes the new table as close as it can fit within the expandable limits of Cell 1, 1. So the size of Cell 1, 1 in the orignal table is changing because Word is trying to fit a larger object into the cell.

    I'm not sure what is going on with your second problem. Word will nest a table with multiple rows in another table.

  3. #3

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    VB Code:
    1. Set objWRange = objWDoc.Range(0, 0)
    2.                         objWRange.MoveEnd wdStory
    3.                         objWRange.Collapse 0 'wdCollapseEnd
    4.                         If Not blnPassedOnce Then
    5.                             blnPassedOnce = True
    6.                         Else
    7.                             objWRange.InsertBreak 7 'wdPageBreak
    8.                         End If

    Hi Workhorse, this is the code before which resets the objWRange to the start then the end of the whole document.

    I can't seem to put in text data where I want it. What would be the best way to move around in the document? Like I have done above but with characters or words or sentences instead of story ?

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

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



Click Here to Expand Forum to Full Width