用VB6操作WORD文档,关闭后再次打开系统会返回错误码462。请问这个问题应该怎么解决?
出错的函数如下,请各位帮我看一下!
Public Function funCreatWordDoc(ByRef aArchive As struArchive, _
ByVal FileAllPath As String) As Long
................
Dim wdApp As Object ' word.Application
Set wdApp = CreateObject("word.Application")
wdApp.Visible = False
wdApp.Documents.Add 'Err #5097 内存不足,请现在保存文档
'Dim DocName As String
'DocName = wdApp.ActiveDocument.Name
'Set wdApp.ActiveDocument.Name = "传真公文报"
'With ActiveDocument.PageSetup
'===== 设置纸张及边距
With wdApp.ActiveDocument.PageSetup
'If .TopMargin = 72 Then
'pass 'modify 2003.10.26
'.TopMargin = 50
'Else
.TopMargin = CentimetersToPoints(2) 'TOP 2CM
.BottomMargin = CentimetersToPoints(1.8) 'Bottom 1.8cm
.LeftMargin = CentimetersToPoints(3.5) 'left 1.5cm
.RightMargin = CentimetersToPoints(1.5) 'right 1.5cm
.PageWidth = CentimetersToPoints(21) 'A4纸大小
.PageHeight = CentimetersToPoints(29.7) 'A4纸大小
'End If
End With
'设置段落行距 ,固定值,28磅 ,左缩进3
With wdApp.Selection.ParagraphFormat
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = 28
'.CharacterUnitFirstLineIndent = 3 报文头以下左缩进3
.Alignment = wdAlignParagraphCenter
End With
'======= 设置公文头格式
'wdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter '中间对齐
wdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify '两端对齐
wdApp.Selection.Font.Size = 26
wdApp.Selection.Font.Name = "宋体"
wdApp.Selection.TypeParagraph
wdApp.Selection.TypeParagraph
'wdApp.Selection.TypeParagraph
wdApp.Selection.Font.Size = 26
wdApp.Selection.TypeText Text:=Space(10) + "铁 路 电 报"
wdApp.Selection.TypeParagraph
'wdApp.Selection.Font.Size = 17
' wdApp.ActiveDocument.Shapes.AddLine(98, 174, 500, 174).Select
'wdApp.Selection.Collapse
'wdApp.Selection.EndKey Unit:=wdStory
'=======缩短"批示"与线间的间距
wdApp.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceExactly
wdApp.Selection.ParagraphFormat.LineSpacing = 18
' === 设置正文
wdApp.Selection.TypeText Text:=vbCrLf
wdApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 0 '左缩进0个字符
wdApp.Selection.Font.Size = 17
wdApp.Selection.Font.Name = "StarringCS"
wdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify '两端对齐
wdApp.Selection.TypeText Text:="批示:" + vbCrLf + vbCrLf + vbCrLf + vbCrLf
'wdApp.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceExactly
'wdApp.Selection.ParagraphFormat.LineSpacing = 28
wdApp.Selection.TypeText Text:=vbCrLf
'wdApp.Selection.MoveUp Unit:=wdLine, Count:=3
wdApp.Selection.Font.Size = 14
wdApp.Selection.TypeText Text:=aArchive.s报头行 + vbCrLf + vbCrLf
wdApp.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceExactly
wdApp.Selection.ParagraphFormat.LineSpacing = 28
wdApp.Selection.Font.Size = 17
'wdApp.Selection.TypeText Text:=vbCrLf
wdApp.ActiveDocument.Shapes.AddLine(100, 150, 500, 150).Select '报头下第一根线
wdApp.ActiveDocument.Shapes.AddLine(100, 241, 500, 241).Select '报头下第二根线
wdApp.ActiveDocument.Shapes.AddLine(100, 278, 500, 278).Select '报头下第三根线
wdApp.Selection.EndKey Unit:=wdStory
Dim i As Long
............
'wdApp.ActiveDocument.Shapes.AddLine(100, 150, 500, 150).Select '报头下第一根线
'wdApp.ActiveDocument.Shapes.AddLine(100, 241, 500, 241).Select '报头下第二根线
'wdApp.ActiveDocument.Shapes.AddLine(100, 278, 500, 278).Select '报头下第三根线
'wdApp.ActiveDocument.Shapes.AddLine(100, 750, 500, 750).Select '画脚注线
If Dir(FileAllPath) <> "" Then
Kill FileAllPath
End If
'
wdApp.ActiveDocument.SaveAs FileAllPath
LSleep (2.5)
wdApp.ActiveDocument.Close
wdApp.Quit
Set wdApp = Nothing
'Call obj1.CloseProcessByExeFileName("WINWORD.EXE")
'Set obj1 = Nothing
funCreatWordDoc = TotalPage
Exit Function '
ErrorHandler: ' 错误处理程序 On Error GoTo ErrorHandler
Dim funName As String
funName = cModName + " funCreatWordDoc"
Dim ErrNumber, ErrDescription, ErrMsg
ErrNumber = Err.Number
ErrDescription = Err.Description
Select Case ErrNumber
Case 462
wdApp.ActiveDocument.Close
wdApp.Quit
Set wdApp = Nothing
Case 482
ErrDescription = "打印机错误 " '不会出现
End Select
Call objRunErr.WriteRunErrInfo("99", funName, Err.Number, Err.Description, gcErrFile)
'errMsg = "Err #" + CStr(ErrNumber) + Space(4) + ErrDescription + Space(6)
'MsgBox errMsg, vbExclamation, "错误"
Dim obj1 As New SwCommon.clsCloseProcess
Call obj1.CloseProcessByExeFileName("WINWORD.EXE")
Set obj1 = Nothing
End Function
问题点数:20、回复次数:3Top
1 楼DawnPine(拂晓的松)回复于 2005-06-06 15:28:57 得分 0
第一次运行能行
第二次运行就不行了,是吗?
会不会是有东西没关闭?Top
2 楼lzlh(lzl)回复于 2005-08-05 20:46:07 得分 10
Dim wdApp As Object ' word.Application
Set wdApp = CreateObject("word.Application")
改为:
Dim wdApp As new word.Application
Dim oDoc As Word.Document
Set oDoc = wdApp.Documents.Add
使用前绑定 可能要好一些吧Top
3 楼maweifeng(肥嘟嘟左卫门)回复于 2005-08-06 12:22:51 得分 10
前后绑定是一样的,下面最好是这样:
Set wdApp = Getobject(, "word.Application")
if wdApp is nothing then
Set wdApp = CreateObject("word.Application")
end if
wdApp.Visible = False
wdApp.Documents.AddTop




