writing excel data into word document



I want to create report in Word using data from excel. I have problems withwriting several paragraphs, one after the other, starting after bookmark created in word templates. In my code it is writing in reverse order and I would like to change it. In addition I would like to add paragraph styles after inserting each paragraph. How I should change my code ?

Thanks for help / suggestions.

Sub CreateWordReport()

Dim applWord As Object

TodaysDate = Format(Date, "dd mmm, yyyy")

'Create a new instance of the Word application, if an existing Word object is not available.
'Set the Application object as follows:
On Error Resume Next
Set applWord = GetObject(, "Word.Application")
'if an instance of an existing Word object is not available, an error will occur (Err.Number = 0 means no error):
If Err.Number <> 0 Then
Set applWord = CreateObject("Word.Application")
End If
'disable error handling:
On Error GoTo 0

'make the Word window visible:
applWord.Visible = True

'add a new word document:
applWord.Documents.add Template:="G:\Template\RTS Flash - Template.dotx", _
NewTemplate:=False, DocumentType:=0

'to save document in the default folder:
NewFileName = "c:\temp\Flash Report " & TodaysDate & ".docx"
applWord.ActiveDocument.SaveAs Filename:=NewFileName

With ActiveDocument
.Bookmarks("MajorIssuesStart").Range.InsertAfter "Inserted Text"
.Bookmarks("MajorIssuesStart").Range.InsertAfter Chr(13)
.Bookmarks("MajorIssuesStart").Range.InsertAfter "Second Inserted Text"
.Bookmarks("MajorIssuesStart").Range.InsertAfter Chr(13)
.Bookmarks("MajorIssuesStart").Range.InsertAfter "Third Inserted Text"
End With

'clear the object variables:
Set applWord = Nothing

End Sub


Couple of suggestions:

This code 1st tries to 'hijack' a running instance of Word (very bad
practice) instead of using its own automated instance from the start. I
recommend you just go with CreateObject()!

What are you using Word for that you can't just use a blank worksheet
in Excel for your report? (I've never found anything Word can do that
can't be duplicated in Excel!!!) You can use a template sheet and just
populate it with your data laid out in the report format.


Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question