Private Sub cmdWord_Click()
'Autore: Federico Luciani
Dim wordApp As Object
Dim wordDoc As Object
Dim blOpen As Boolean
On Error Resume Next
blOpen = True
Set wordApp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set wordApp = CreateObject("Word.Application")
blOpen = False
Err.Number = 0
End If
On Error GoTo gestErrori
Set wordDoc = wordApp.Documents.Add()
With wordApp
.Visible = True
.Activate
.Selection.Style = wordDoc.Styles("Titolo 1")
.Selection.TypeText "Hello world!"
End With
If Not blOpen Then
wordDoc.Close savechanges:=False
wordApp.Application.Quit
End If
esci:
Set wordDoc = Nothing
Set wordApp = Nothing
Exit Sub
gestErrori:
MsgBox Err.Number & " - " & Err.Description
GoTo esci
End Sub
|