T
tonypluta
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
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