回复 2楼 风吹过b 再次请教!!
你好!!你提供的方法可以实现!非常感谢。我想实现:如有发现某个其他word文档已经打开,将其进程关闭,只打开我指定word文档进行相关操作。
另外又出现新的问题:利用taskkill 关闭winword.exe后,删除word母版文件后,在“11中 Set doc1 = Documents.Open(outfilepath)这句”出现“远程服务器不存在,462”错误,哪里出问题了呢?能否一看??先谢谢!!
下面是代码:
Dim wordobj As Word.Application
Dim txfilepath As String
Dim outfilepath As String
Dim outputname As String
Dim L As Long
Dim H As Long
Dim H1 As Long
Private Sub Command1_Click()
11
Set wordobj = New Word.Application
txfilepath = "E:\图形Word\5f\m1.docx"
outfilepath = "E:\图形Word\ff\m15.docx"
FileCopy txfilepath, outfilepath
L = Text1.Text
H = Text2.Text
H1 = Text4.Text
outputname = "E:\程序开发\图形Word\ff\图.pdf"
Set doc1 = Documents.Open(outfilepath)
doc1.Activate
If Documents.Count < 1 Then
MsgBox "无文件打开"
End If
wordobj.Visible = False
On Error GoTo ErrHandler
ErrHandler:
If Err <> 0 Then
Err.Clear
'##############当发现已经打开了某个其他word文档后修改文本框会报错,需要删除word母版文件###############
If Dir("C:\Users\knight\AppData\Roaming\Microsoft\Templates\Normal.dot", vbDirectory) <> "" Then
MsgBox "文件存在"
Kill "C:\Users\knight\AppData\Roaming\Microsoft\Templates\Normal.dot"
Else
MsgBox "文件不存在"
End If
'
If Dir("C:\Users\knight\AppData\Roaming\Microsoft\Templates\Normal.dotm", vbDirectory) <> "" Then
MsgBox "文件存在"
Shell "taskkill /F /IM " & "winword.exe"
Kill "C:\Users\knight\AppData\Roaming\Microsoft\Templates\Normal.dotm"
GoTo 11
Else
MsgBox "文件不存在"
GoTo 11
End If
End If
'########################################################
'##################打开指定word文档对指定文本框进行修改#############
wordobj.ActiveDocument.Shapes("Text Box 22").Select
wordobj.Selection.ShapeRange.TextFrame.TextRange.Select
wordobj.Selection.Collapse
wordobj.Selection.MoveRight Unit:=wdCharacter, Count:=15
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeText Text:=H1
wordobj.Selection.ShapeRange.TextFrame.TextRange.Select
wordobj.Selection.Collapse
wordobj.Selection.ShapeRange.Select
wordobj.Selection.Collapse
wordobj.Selection.EscapeKey
wordobj.Selection.EscapeKey
''''''
wordobj.ActiveDocument.Shapes("Text Box 24").Select
wordobj.Selection.ShapeRange.TextFrame.TextRange.Select
wordobj.Selection.Collapse
wordobj.Selection.MoveRight Unit:=wdCharacter, Count:=15
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeText Text:=H
wordobj.Selection.ShapeRange.TextFrame.TextRange.Select
wordobj.Selection.Collapse
wordobj.Selection.ShapeRange.Select
wordobj.Selection.Collapse
wordobj.Selection.EscapeKey
wordobj.Selection.EscapeKey
''''''
wordobj.ActiveDocument.Shapes("Text Box 23").Select
wordobj.Selection.ShapeRange.TextFrame.TextRange.Select
wordobj.Selection.Collapse
wordobj.Selection.MoveRight Unit:=wdCharacter, Count:=15
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeBackspace
wordobj.Selection.TypeText Text:=L
wordobj.Selection.ShapeRange.TextFrame.TextRange.Select
wordobj.Selection.Collapse
wordobj.Selection.ShapeRange.Select
wordobj.Selection.Collapse
wordobj.Selection.EscapeKey
wordobj.Selection.EscapeKey
'#################另存为PDF格式###############
wordobj.ActiveDocument.ExportAsFixedFormat OutputFileName:= _
outputname, ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
True, UseISO19005_1:=False
wordobj.ActiveWindow.Close
wordobj.Application.Quit
Set wordobj = Nothing
'
MsgBox "打印完成"
End Sub