Solved

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

Posted on 2004-04-13
4
169 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:davejhansen
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 10820818
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
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 11441076
PAQed - no points refunded (of 500)

Netminder
Site Admin
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now