Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-04-13
4
Medium Priority
?
177 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

609 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