Solved

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

Posted on 2004-04-13
4
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

726 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