|
|
Sub test1()
Dim wordDoc As Object
Dim wordApp1 As Object
Dim mydoc As String
Dim myapp As String
'On Error GoTo errorhandler
mydoc = "C:\Users\along22\Desktop\New folder\invite1.doc"
myapp = "word.application"
If Not docexists(mydoc) Then
MsgBox 请先创建
Exit Sub
End If
If Not isrunning(myapp) Then
' MsgBox "软件未运行,将创建"
Set wordApp1 = CreateObject("word.application")
Set wordDoc = wordApp1.documents.Open(mydoc)
Else
MsgBox "软件已运行,将打开文件"
Set wordDoc = GetObject(mydoc)
End If
With wordDoc.Paragraphs(1).Range
.ParagraphFormat.Alignment = wdalignparagraphcenter
End With
wordDoc.Application.Quit savechanges:=True
Set wordoc = Nothing
Set wordApp1 = Nothing
Exit Sub
errorhandler:
MsgBox Err.Description, vbCritical, "错误为:" & Err.Number
End Sub
Function docexists(ByVal mydoc As String) As Boolean
On Error Resume Next
If Dir(mydoc) <> "" Then
docexists = True
Else
docexists = False
End If
End Function
Function isrunning(ByVal myapp As String) As Boolean
Dim appref As Object
On Error Resume Next
Set appref = GetObject(, myapp)
If Err.Number = 429 Then
isrunning = False
Else
isrunning = True
End If
Set appref = Nothing
End Function
|
|