A
Alex St-Pierre
Hi,
I would like to create a document from Excel. I did the following code
(works well) and I'm wondering if it's the best way to make a Word Document?
Thanks!
Alex
Sub CreateWordDocument()
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim NewStyle As Word.Style
Dim oRange As Word.Range
Dim i As Integer
i = 1
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If appWord Is Nothing Then
Set appWord = CreateObject("Word.Application")
End If
appWord.Visible = True
Set docWord = appWord.Documents.Add
docWord.Styles("Bold Text").Delete
docWord.Styles("Italic Text").Delete
On Error GoTo 0
Set NewStyle = ActiveDocument.Styles.Add("Bold Text")
With NewStyle
.Font.Bold = True
.Font.Italic = False
End With
Set NewStyle = ActiveDocument.Styles.Add("Italic Text")
With NewStyle
.Font.Bold = False
.Font.Italic = True
End With
Set oRange = ActiveDocument.Range(0, 0)
With oRange
.Style = "Bold Text"
.Text = "Bold Text" & Chr(13)
End With
appWord.Selection.EndKey unit:=wdStory
i = i + 1
Set oRange = docWord.Paragraphs(i).Range
With oRange
.Style = "Italic Text"
.Text = "Italic Text" & Chr(13)
End With
'...
Set appWord = Nothing
Set docWord = Nothing
Set NewStyle = Nothing
Set oRange = Nothing
End Sub
I would like to create a document from Excel. I did the following code
(works well) and I'm wondering if it's the best way to make a Word Document?
Thanks!
Alex
Sub CreateWordDocument()
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim NewStyle As Word.Style
Dim oRange As Word.Range
Dim i As Integer
i = 1
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If appWord Is Nothing Then
Set appWord = CreateObject("Word.Application")
End If
appWord.Visible = True
Set docWord = appWord.Documents.Add
docWord.Styles("Bold Text").Delete
docWord.Styles("Italic Text").Delete
On Error GoTo 0
Set NewStyle = ActiveDocument.Styles.Add("Bold Text")
With NewStyle
.Font.Bold = True
.Font.Italic = False
End With
Set NewStyle = ActiveDocument.Styles.Add("Italic Text")
With NewStyle
.Font.Bold = False
.Font.Italic = True
End With
Set oRange = ActiveDocument.Range(0, 0)
With oRange
.Style = "Bold Text"
.Text = "Bold Text" & Chr(13)
End With
appWord.Selection.EndKey unit:=wdStory
i = i + 1
Set oRange = docWord.Paragraphs(i).Range
With oRange
.Style = "Italic Text"
.Text = "Italic Text" & Chr(13)
End With
'...
Set appWord = Nothing
Set docWord = Nothing
Set NewStyle = Nothing
Set oRange = Nothing
End Sub