Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 39 of 39

Thread: Solved: Creating tables with Word.Application issue (II)

  1. #21
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Gerry,

    This is what I see when working with the Range.Text in a word table.
    First cell text (in this case "AAA") followed by Chr(13), Chr(7), the next cell text ("111") followed by Chr(13), Chr(7), followed by Chr(13), Chr(7), hence my once again. To test:

    [VBA]
    Public Sub GenTable(iColumns As Long, iRows As Long)
    Dim mAA As String
    Dim marrBB() As String
    Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
    NumRows:=iRows, NumColumns:=iColumns)
    Tbl.Range.Font.Bold = True
    Tbl.Columns(1).Width = 20
    Tbl.Cell(1, 1).Range.Text = "AAA"
    Tbl.Cell(1, 2).Range.Text = "111"
    Tbl.Cell(2, 1).Range.Text = "BBB"
    Tbl.Cell(2, 2).Range.Text = "222"
    Tbl.Cell(3, 1).Range.Text = "CCC"
    Tbl.Cell(3, 2).Range.Text = "333"
    Tbl.Cell(4, 1).Range.Text = "DDD"
    Tbl.Cell(4, 2).Range.Text = "444"
    mAA = Tbl.Range.Text
    MsgBox Asc(Mid(mAA, 4, 1)) '<- chr 13
    MsgBox Asc(Mid(mAA, 5, 1)) '<- chr 7 -
    'end of first cell
    MsgBox Asc(Mid(mAA, 9, 1)) '<- chr 13
    MsgBox Asc(Mid(mAA, 10, 1)) '<- chr 7
    '- end of second cell
    MsgBox Asc(Mid(mAA, 11, 1)) '<- chr 13
    MsgBox Asc(Mid(mAA, 12, 1)) '<- chr 7
    '- end of row ** once again
    marrBB = Split(Tbl.Range.Text, Chr(13) & Chr(7))
    MsgBox UBound(marrBB) '<- = 12 not 8
    Set Tbl = Nothing '<- be nice, release variable
    objApp.Selection.MoveEnd Unit:=wdTable, Count:=1 '<-
    'move to end of table
    objApp.Selection.MoveDown Unit:=wdLine, Count:=1, _
    Extend:=wdMove '<- move foward 1 line
    End Sub
    [/VBA]

    This happens in VB6 AND VBA. So in conclusion I agree with you, it holds ALL text between the start and the stop of the Range.Text.

    Mr Doubtfire,

    C:\test.doc is a file I loaded for testing, if you don't have it it will not load. I agree with what xCav8r has posted, the only thing is I had a lot of problems with createobject and getobject to create an instance of Word through VB. I got a lot of ActiveX couldn't create object, etc. When I changed it to New Application I got a lot fewer phone calls . I also use doevents right after New Application so word will have time to load and get going, then it ready to recieve instructions. This is what I have deterined works for me best, does not mean it is the best or preferred, just works best for me.

    1.) - Tbl.Columns(1).Width is not characters You could use the autofit which would resize the columns to fit the data.
    2.)
    [VBA]
    Sub look()
    Dim A As Table
    Set A = Tables.Add(Range:=Selection.Range, NumRows:=5, _
    NumColumns:=5)
    A.Columns(1).Width = InchesToPoints(1.25)
    With A.Borders
    .InsideLineStyle = wdLineStyleNone
    .OutsideLineStyle = wdLineStyleNone
    End With
    End Sub

    [/VBA]
    3.) use getobject if an error occurs use create object if there is no error close activedocument - here could be a problem you have no idea if the doc needs to be saved.

    4.)
    [VBA]
    Private Sub Command1_Click()
    StartWrd "C:\test1.doc"
    AddText "How are you?"
    GenTable 2, 4
    AddText Chr(13)
    GenTable 6, 6
    AddText Chr(13)
    GenTable 8, 8
    AddText "Thank you!"
    KillWrd "C:\test.doc"
    End Sub
    [/VBA]

    HTH

  2. #22
    Thank you again for the suggestion and sample code.
    With NO luck the following code does not work on my machine
    [vba]
    A.Columns(1).Width = InchesToPoints(1.25)
    With A.Borders
    .InsideLineStyle = wdLineStyleNone
    .OutsideLineStyle = wdLineStyleNone
    End With
    [/vba]

    I have also tried
    A.Borders.Enable = False
    With A.Range.Borders
    .InsideLineStyle = wdLineStyleNone
    .OutsideLineStyle = wdLineStyleNone
    End With
    Neither of them work.
    Please advice.
    Thanks ALL! Especially those with detailed explanation.

  3. #23
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Tommy, I am not sure what you are trying to do with those msgbox instructions. What ARE you doing?

    Mr. Doubtfire - please be specific. "Does not work" is not helpful.

    Do you get an error message?
    Nothing at all happens?

    I think it is a good idea to split up instruction into separate subs, but in this case the instructions are separated out, IMHO incorrectly.

    The code has the creation of the table and insertion of text, THEN a separate sub for format of the table. IMHO format follows creation. In other words, you use the table object, RIGHT AFTER it is created, to do your format.

    [vba]Public Sub GenTable(iColumns As Long, iRows As Long)

    Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
    NumRows:=iRows, NumColumns:=iColumns)
    With Tbl.Range.Borders
    .InsideLineStyle = wdLineStyleNone
    .OutsideLineStyle = wdLineStyleNone
    End With[/vba]

  4. #24
    Sorry for NOT being clear.
    1. A.Columns(1).Width = InchesToPoints(1.25)
    This DOES not adjust to Inches from pixels.
    2. With Tbl.Range.Borders
    .InsideLineStyle = wdLineStyleNone
    .OutsideLineStyle = wdLineStyleNone
    End With
    This DOES not eliminate the "grid"/"border".
    Thanks and please advice.

  5. #25
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Does your code make the table? If you put the format instructions right after it SHOULD remove the border. It does for me. Are you sure it doesn't? Sometimes, depending on what view you are looking with, it is hard to see that the borders are gone.

    However, if you are sure it does NOT work, please post the ENTIRE code you are working with.

  6. #26
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Gerry I agree with the instructions not being broken out correctly I was just posting a sample of how to do it. So to correct that:

    [VBA]
    Public Sub GenTable(iColumns As Long, iRows As Long)
    Dim mAA As String
    Dim marrBB() As String
    Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
    NumRows:=iRows, NumColumns:=iColumns)
    With Tbl.Borders
    .OutsideLineStyle = wdLineStyleNone
    .InsideLineStyle = wdLineStyleNone
    End With
    Tbl.Range.Font.Bold = True
    Tbl.Columns(1).Width = InchesToPoints(1.25)
    Tbl.Cell(1, 1).Range.Text = "AAA"
    Tbl.Cell(1, 2).Range.Text = "111"
    Tbl.Cell(2, 1).Range.Text = "BBB"
    Tbl.Cell(2, 2).Range.Text = "222"
    Tbl.Cell(3, 1).Range.Text = "CCC"
    Tbl.Cell(3, 2).Range.Text = "333"
    Tbl.Cell(4, 1).Range.Text = "DDD"
    Tbl.Cell(4, 2).Range.Text = "444"
    Set Tbl = Nothing '<- be nice, release variable
    objApp.Selection.MoveEnd Unit:=wdTable, Count:=1
    '<- move to end of table
    objApp.Selection.MoveDown Unit:=wdLine, Count:=2, _
    Extend:=wdMove '<- move foward 1 line
    End Sub


    [/VBA]

    Also Gerry I changed Tbl.Range.Borders to Tbl.Borders because the columns did not have the borders removed, I don't understand why but, I have fun figuring it out .

  7. #27
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yo! Would people PLEASE (pretty please) use the underscore character to break their code lines?

    Putting long instruction lines causes the code window to expand and forces people to scroll left/right AND up down. This is a real pain sometimes. Further, using the underscore can make the code much easier to read.

    Tommy, for example:

    [vba]Set Tbl = WrdDoc.Tables.Add(Range:=objApp.Selection.Range, _
    NumRows:=iRows, NumColumns:=iColumns) [/vba]

    keeps the instruction line within the boundary of the code window.

    Not that I am picking on Tommy. It is an easy thing to do, and would be greatly appreciated. It will make it better for you too!

  8. #28
    I found out the "grid" lines do not show up on printing/printing view.
    Thanks!

  9. #29
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    I'll try to remember to do that. I have a 19" monitor at a res of 1600x1200 so I forget, sorry. I don't mind being picked on, I can handle it , like you said it's an easy thing to do.

  10. #30
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yeah...it is those huge monitors with high-rez that makes people forget that.....well, gosh....not all of us HAVE those.

    me, me, me it is all about me

    I have a 19" as well, but I could not have it at 1600 x 1200. My tired old eyes can not see text at that rez. And I do not mean I can not READ it...I can not SEE it.....just kidding.

    Thanks Tommy, I appreciate it.

  11. #31
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    NP I code FORTRAN in the dos screen (640x480 normally), do Acad work, and then goto VB, if it wasn't for my reading spec's I couldn't see anything. I've been told that if you change the tab spacing from 4 to 2 it is easier also but I quite frankly can't read that even with my readers. If I forget in the future remind me and I'll fix it.

  12. #32
    Could anyone tell me how to add a
    1. page number
    2. footnote

    at bottom of each page for the Word document?
    Thanks!

  13. #33
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Mr Doubtfire
    I found out the "grid" lines do not show up on printing/printing view.
    Thanks!
    In Word goto Table | Hide / Show gridlines to toggle that grid!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  14. #34
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Mr Doubtfire
    Could anyone tell me how to add a
    1. page number
    2. footnote

    at bottom of each page for the Word document?
    Thanks!
    As long as you leave the Header Footers alone you only have to set one Footer and the rest will follow:[vba]
    Sub InsertPageNumber()
    Dim oRange As Word.Range
    Set oRange = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range

    With oRange
    .Fields.Add Range:=oRange, Type:=wdFieldPage
    End With

    Set oRange = Nothing
    End Sub
    [/vba]

    I didn't add the footnote cause that's a strange request! Normally those go at the bottom of the page in there own space so why do you want them in the footer?

    HTH,
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  15. #35
    Thanks!
    For example, I would like to add "myweb.com" at the page number at the left bottom and "mywebsite.com" at the right bottom of every page of my Word document.
    That's what I want to achieve.
    Like this
    Page xx please visit mywebsite.com

    Please advice.

  16. #36
    VBAX Newbie
    Joined
    Oct 2005
    Posts
    1
    Location
    Quote Originally Posted by Mr Doubtfire
    Sorry for NOT being clear.
    1. A.Columns(1).Width = InchesToPoints(1.25)
    This DOES not adjust to Inches from pixels.
    2. With Tbl.Range.Borders
    .InsideLineStyle = wdLineStyleNone
    .OutsideLineStyle = wdLineStyleNone
    End With
    This DOES not eliminate the "grid"/"border".
    Thanks and please advice.
    This will remove the gridlines. (The same as the Hide/Show gridlines on the Tools Menu).
    [VBA] objApp.ActiveWindow.View.TableGridlines = False [/VBA]

  17. #37
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by scratch
    This will remove the gridlines. (The same as the Hide/Show gridlines on the Tools Menu).
    [VBA] objApp.ActiveWindow.View.TableGridlines = False [/VBA]
    A warning about this setting.

    This is a setting that is not retained switching to another client. Meaning if someone else has gridlines to show then he wil see the grid.

    If you want to make sure no one sees the grid make sure you format the table borders to white.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  18. #38
    Thanks again, guys for the knowledge.

  19. #39
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Glad to see it worked out for you!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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