Solved

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

Posted on 2004-04-13
4
171 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

829 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