Results 1 to 6 of 6
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Northampton, Northamptonshire, England
    Posts
    1,951
    Thanks
    2
    Thanked 1 Time in 1 Post

    Create a Word Doc from within Excel (Excel 2002)

    Hi

    I am trying to creat a word document from wihin excel but I get an error when trying to run the macro.

    Sub Test_Word()
    Dim oWd As Word.Application, oWdoc As Word.Document
    Dim op As Word.Range
    Dim ot As Word.Table
    Set oWd = CreateObject("Word.Application")

    Set oWdoc = oWd.Documents.Add
    Set op = oWdoc.Range
    Set ot = oWdoc.Tables.Add(r, 4, 5)<---- error Here

    ot.Cell(1, 1).Range.Text = "test"
    oWdoc.SaveAs ("C:MyTest.doc")
    oWdoc.Close
    oWd.Quit
    Set oWdoc = Nothing
    Set oWd = Nothing

    End Sub

    Many thanks

    Braddy
    If you are a fool at forty, you will always be a fool

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Create a Word Doc from within Excel (Excel 2002)

    Try replacing r with op in the line that errors - the first argument should be a range but you haven't declared r as anything.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Northampton, Northamptonshire, England
    Posts
    1,951
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Create a Word Doc from within Excel (Excel 2002)

    Hi Rory

    Thanks for that, it created a table in word and put test in the top left cell, is there a way to get the data from the worksheet into the table.

    Braddy

    NB this a learning curve for me.
    If you are a fool at forty, you will always be a fool

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Create a Word Doc from within Excel (Excel 2002)

    You can download a free help file with syntax and examples of Automation from Microsoft: Office XP Automation Help file.

    (Automation is the process of controlling one application from another in code. You are trying to control Word from Excel)

  5. #5
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Northampton, Northamptonshire, England
    Posts
    1,951
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Create a Word Doc from within Excel (Excel 2002)

    Hi Hans

    Thanks I will go take peak.

    Braddy
    If you are a fool at forty, you will always be a fool

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Create a Word Doc from within Excel (Excel 2002)

    The basic idea to get data from the Excel worksheet into the Word table is

    ot.Cell(1, 1).Range.Text = ActiveSheet.Range("D37").Value

    or to populate the entire table

    Dim i As Long, j As Long
    For i = 1 To 4
    For j = 1 To 5
    ot.Cell(i, j).Range.Text = ActiveSheet.Cells(i, j)
    Next j
    Next i

Posting Permissions

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