Link to home
Start Free TrialLog in
Avatar of davejhansen
davejhansen

asked on

Exporting Excel data into Word using vba - runtime error - application-defined or object defined

I am new to VBA and am trying to debug my code, which is attempting to move thousands of rows of data into a large Word file.  I am struggling with the code to gather my data from excel and paste it into word.  I do not want the final product in table form, but rather as a list with titles and subtitles.  Any help would be much appreciated.  Below is the code:

Sub XltoWd()
Dim wordapp As Object
Dim data As Object

Set wordapp = CreateObject("Word.Application")

With wordapp
    .Visible = True
    .Documents.Add
    With .Selection
    .Font.Size = 14
    .Font.Bold = True
    .ParagraphFormat.Alignment = 1
    .TypeText Text:="Third-Party Report Excerpts"
    .TypeParagraph
    .TypeParagraph
    End With
 .ActiveDocument.SaveAs Filename:="C:\Documents and Settings\dhansen\My Documents\Last Try.doc"
End With

ActiveDocument.Range("a1").Select
Set data = Sheets("sheet1").Range("a1")

Records = ActiveDocument.CountA(Sheets("Sheet1").Range("A:A"))
For i = 1 To Records
'       Assign current data to variables
        Title = data.Offset(i - 1, 0).Value
        Date = data.Offset(i - 1, 1).Value
        Analyst = data.Offset(i - 1, 2).Value
        Doc = data.Offset(i - 1, 3).Value
        Issue = data.Offset(i - 1, 4).Value
        Notes = data.Offset(i - 1, 5).Value
        Quote = data.Offset(i - 1, 6).Value

            With wordapp.Selection
                .Font.Size = 12
                .Font.Bold = False
                .ParagraphFormat.Alignment = 0
                .TypeText Text:="==="
                .TypeParagraph
                .TypeParagraph
                .TypeText Text:="Title:" & vbTab & Title
                .TypeParagraph
                .TypeText Text:="Date:" & vbTab & Format(Date, "YYYY-MM-DD")
                .TypeParagraph
                .TypeText Text:="Analyst:" & vbTab & Analyst
                .TypeParagraph
                .TypeText Text:="Document No.:" & vbTab & Document
                .TypeParagraph
                .TypeText Text:="Category:" & vbTab & Issue
                .TypeParagraph
                .TypeText Text:="Summary:" & vbTab & Notes
                .TypeParagraph
                .TypeText Text:="Excerpt:" & vbTab & Quote
                .TypeParagraph
                .TypeParagraph
            End With
    Next i

wordapp.Quit
Set wordapp = Nothing
End Sub
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi Dave,

I have set Option Explicit in the declarations section. This ensures that all variables are declared.
I've used early binding, which makes development easier because it allows Auto Quick Info & Auto List Members to work.
I've added an underscore to the Date variable because Date is a reserved word in VBA. I've done it to Document as well, thought it's probably not necessary in the Excel context.
Selection has been changed to Range for added the data to the Word document.
The Save has been moved to the end.

Option Explicit
Sub XltoWd()
    Dim wordapp As Word.Application
    Dim Records As Integer
    Dim i As Integer
    Dim data As Excel.Range
    Dim Title As String
    Dim Date_ As String
    Dim Analyst As String
    Dim Doc As String
    Dim Issue As String
    Dim Notes As String
    Dim Quote As String
    Dim Document_ As String
    Dim wdRange As Word.Range
    'Set wordapp = CreateObject("Word.Application")
    Set wordapp = New Word.Application
    With wordapp
        .Visible = True
        .Documents.Add
        With .Selection
        .Font.Size = 14
        .Font.Bold = True
        .ParagraphFormat.Alignment = 1
        .InsertAfter Text:="Third-Party Report Excerpts"
        .InsertParagraphAfter
        .InsertParagraphAfter
        End With
    End With
   
    'ActiveSheet.Range("a1").Select
    Set data = Sheets("sheet1").Range("a1")
   
    Records = WorksheetFunction.CountA(Sheets("Sheet1").Range("A:A"))
    For i = 1 To Records
    ' assign current data to variables
        Title = data.Offset(i - 1, 0).Value
        Date_ = data.Offset(i - 1, 1).Value
        Analyst = data.Offset(i - 1, 2).Value
        Doc = data.Offset(i - 1, 3).Value
        Issue = data.Offset(i - 1, 4).Value
        Notes = data.Offset(i - 1, 5).Value
        Quote = data.Offset(i - 1, 6).Value
        Set wdRange = wordapp.ActiveDocument.Content
        wdRange.Collapse wdCollapseEnd
        With wdRange
            .Font.Size = 12
            .Font.Bold = False
            .ParagraphFormat.Alignment = 0
            .InsertAfter Text:="==="
            .InsertParagraphAfter
            .InsertParagraphAfter
            .InsertAfter Text:="Title:" & vbTab & Title
            .InsertParagraphAfter
            .InsertAfter Text:="Date:" & vbTab & Format(Date_, "YYYY-MM-DD")
            .InsertParagraphAfter
            .InsertAfter Text:="Analyst:" & vbTab & Analyst
            .InsertParagraphAfter
            .InsertAfter Text:="Document No.:" & vbTab & Document_
            .InsertParagraphAfter
            .InsertAfter Text:="Category:" & vbTab & Issue
            .InsertParagraphAfter
            .InsertAfter Text:="Summary:" & vbTab & Notes
            .InsertParagraphAfter
            .InsertAfter Text:="Excerpt:" & vbTab & Quote
            .InsertParagraphAfter
            .InsertParagraphAfter
        End With
    Next i
    wordapp.ActiveDocument.SaveAs Filename:="C:\Documents and Settings\dhansen\My Documents\Last Try.doc"

    wordapp.ActiveDocument.Close
    wordapp.Quit
    Set wordapp = Nothing
End Sub

Graham
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial