Consulting

Results 1 to 5 of 5

Thread: Make Word table from XL

  1. #1
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location

    Make Word table from XL

    Can't seem to get the right syntax together to add a variable size table to a Word doc from XL. I don't want to use a Word reference (ie. use late binding) to achieve this. Something like the following I suspect but this doesn't work. Any assistance always appreciated. Dave
    [VBA]
    'open blank word doc file
    Dim oWDBasic As Object
    Set oWDBasic = CreateObject("Word.Application")
    oWDBasic.documents.Open Filename:="C:\tabletest.doc"
    'add table to doc
    '****this doesn't work
    With oWDBasic.ActiveDocument
    .Tables.Table(1).Add NumRows:=4, NumColumns:=3
    End With
    [/VBA]

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Open a existing document and add table
    [vba]Sub AddTable()
    'open blank word doc file
    Dim oWDBasic As Object
    Dim wrdDoc As Object
    Set oWDBasic = CreateObject("Word.Application")
    Set wrdDoc = oWDBasic.Documents.Open(Filename:="C:\tabletest.doc")
    oWDBasic.Visible = True

    'add table to doc
    With wrdDoc
    .Tables.Add oWDBasic.Selection.Range, numrows:=4, Numcolumns:=3
    End With
    Set wrdDoc = Nothing
    Set oWDBasic = Nothing
    End Sub
    [/vba]

    or Create new document and add table
    [vba]
    Sub AddTable2()
    'Create new doc and table
    Dim oWDBasic As Object
    Dim wrdDoc As Object
    Set oWDBasic = CreateObject("Word.Application")
    Set wrdDoc = oWDBasic.Documents.Add
    oWDBasic.Visible = True
    'add table to doc
    With wrdDoc
    .Tables.Add oWDBasic.Selection.Range, numrows:=3, Numcolumns:=4
    End With
    Set wrdDoc = Nothing
    Set oWDBasic = Nothing
    End Sub
    [/vba]

    I believe the Range was what you were missing.

    Cheers!

    David


  3. #3
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Thank you Tinbendr for your time and assistance. I have just been learning abit more about XL and Word tables. I'll post my learning to date in the event that it might be useful for others. Again, thanks for your help. Have a nice day! Dave
    [vba]
    Sub XLWordTable()
    Dim oWDBasic As Object, Rng As Range
    Dim wrdDoc As Object, Ocell As Variant
    'adds text & table to Word doc from XL
    'adds range and chart to table
    'no Word reference required

    On Error GoTo ErFix
    'open existing word doc file ie. "D:\tabletest.doc"
    Set oWDBasic = CreateObject("Word.Application")
    Set wrdDoc = oWDBasic.Documents.Open(Filename:="D:\tabletest.doc")

    'clear doc
    With wrdDoc
    .Range(0, .Characters.Count).Delete
    End With

    'add text to top of doc and spaces before table
    oWDBasic.ActiveDocument.Select
    With oWDBasic.Selection
    .typetext Text:=" *** Table Test *** " & vbCrLf
    .typeparagraph
    .typeparagraph
    End With

    'add table
    With wrdDoc
    'use line below to specify location. This eg. is for doc start
    '.Tables.Add oWDBasic.ActiveDocument.Range(Start:=0, End:=0), _
    'numrows:=4, Numcolumns:=3
    ' insert table at current selection point
    .Tables.Add oWDBasic.Selection.Range, numrows:=4, Numcolumns:=3
    End With

    'adds row to existing table
    oWDBasic.ActiveDocument.Tables(1).Rows.Add
    'adds column to existing table
    oWDBasic.ActiveDocument.Tables(1).Columns.Add

    'autoformat table
    'wdTableFormat3DEffects1 (0)
    'wdTableFormat3DEffects2 (1)
    'wdTableFormat3DEffects3 (2)
    'etc
    'wdTableFormatWeb1 (40)
    'wdTableFormatWeb2 (41)
    'wdTableFormatWeb3 (42)
    'table autoformat #'s 0 to 42
    oWDBasic.ActiveDocument.Tables(1).AutoFormat Format:=13, _
    ApplyBorders:=True, ApplyFont:=True, ApplyColor:=True

    'loop through table cells
    For Each Ocell In oWDBasic.ActiveDocument.Tables(1).Range.Cells
    iCount = iCount + 1
    Ocell.Range.InsertAfter "Cell " & iCount
    Next Ocell

    'set sheet "Rng" to insert as picture into table
    'use line below for named range on sheet ie. Rng
    'With Sheets("Sheet1").Range("Rng")
    'vba set range
    With Sheets("Sheet1")
    Set Rng = .Range(.Cells(1, "A"), .Cells(7, "B"))
    End With

    With Rng
    .CopyPicture
    End With

    'paste picture of range to table cell (2,2)
    With oWDBasic.ActiveDocument.Tables(1).cell(2, 2).Range
    .Paste
    End With

    'copy chart as picture
    '***the chart MUST exist
    With Sheets("sheet1").ChartObjects(1)
    .CopyPicture
    End With

    'paste chart to table cell (2,3)
    With oWDBasic.ActiveDocument.Tables(1).cell(2, 3).Range
    .Paste
    End With

    Application.CutCopyMode = False

    '***this doesn't work
    'wdAutoFitContent, wdAutoFitFixed, or wdAutoFitWindow
    'oWDBasic.ActiveDocument.Tables(1).AutoFitbehavior = 0

    'autofit table cell contents
    With oWDBasic.ActiveDocument.Tables(1)
    .Columns.AutoFit
    End With

    'make text string with space following table
    Dim Txtstr As String
    Txtstr = vbCrLf & "Test finished at: " & Now()

    'add text string after table
    With oWDBasic.ActiveDocument
    .content.InsertAfter Txtstr
    End With

    'close and save doc
    oWDBasic.ActiveDocument.Close savechanges:=True

    Set wrdDoc = Nothing
    oWDBasic.Quit
    Set oWDBasic = Nothing
    MsgBox "Finished"
    Exit Sub

    ErFix:
    On Error GoTo 0
    MsgBox "error"
    Set wrdDoc = Nothing
    oWDBasic.Quit
    Set oWDBasic = Nothing
    End Sub
    [/vba]

  4. #4
    VBAX Regular
    Joined
    Mar 2005
    Location
    Detroit, Michigan
    Posts
    8
    Location

    Excel pictures and charts to Word

    Very helpfull,

    I already have logic to replace unique codes in Word, with text that has been entered in Excel

    Here is my next problem
    I have multiple pictures that need to be inserted at specific locations in various 2 column, 1 row tables in word.

    Hoping to manually add unique text in word tables column-1 (Picture01, Picture02, etc).
    Then go through my Excel data on Sheet1 and identify the pictures names corresponding to the Picture01, etc location. The pictures will be in a specific directory on my hard drive.

    Thanks, hope someone is still watching this thread

  5. #5
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Henry... I don't understand what you want to accomplish. 1 row/2 column Word tables... Text in column1 and picture in column2? Manually add? Match XL picture name with Word picture text? Maybe abit more info. Dave

Posting Permissions

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