我在用vb调用word时,生成了文本,但是再次生成(程序没有退出)生成的是空白文档,我修改参数(程序依然没退出),生成文本时出现下面的错误:“实时错误‘462’,远程服务器不在或不能使用!”。请问大家这是什么原因,该怎么解决呀!
还有就是我程序退出重新运行时必须关闭word文件,否则。生成的文本不会新打开一个文本,而是在原来打开的文本里接着输出。我的代码如下,大家帮我看看,谢谢啦先!
Private Sub Command1_Click()
Dim WordApp As New Word.Application
Set newdoc = WordApp.Documents.Add
WordApp.Caption = "文件名称"
WordApp.Visible = True
Set WordApp = Word.Application……
……
Set WordApp = NothingEnd sub
还有就是我程序退出重新运行时必须关闭word文件,否则。生成的文本不会新打开一个文本,而是在原来打开的文本里接着输出。我的代码如下,大家帮我看看,谢谢啦先!
Private Sub Command1_Click()
Dim WordApp As New Word.Application
Set newdoc = WordApp.Documents.Add
WordApp.Caption = "文件名称"
WordApp.Visible = True
Set WordApp = Word.Application……
……
Set WordApp = NothingEnd sub
Set WordApp = Word.Application 重新赋值了
Private Sub Command1_Click()Set newdoc = WordApp.Documents.Add
WordApp.Caption = "ÎļþÃû³Æ"
WordApp.Visible = True'¡¡
'¡¡
Set newdoc = Nothing
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
WordApp.quit
Set WordApp = Nothing
End Sub
所有 Word 对象必须按照从小到大的次序依次关闭/释放。
比如你的代码中用到了这些 Excel 对象:Application、WorkBook、WorkSheet、Cell,那么按照从小到大的次序释放应是:Cell、WorkSheet、workBook、Application。
理由:假如你尚未释放 WorkSheet 对象,先对 WorkBook 对象进行关闭/释放,由于它下面的 WorkSheet 对象还在被使用,所以不会真正被释放。其他的的对象关系也类似。
我用下面的代码当第一次点击确定时生成word的文本,当第二次点击时就出现错误提示:实时错误‘462’,远程服务器不在或不能使用!”。我也觉得是没有释放和关闭对象,这个该怎么操作,请大家给点指示,万分感谢!
Private Sub Command1_Click()
Dim WordApp As New Word.Application
Set newdoc = WordApp.Documents.Add
WordApp.Visible = True
Set WordApp = Word.Application
With WordApp.Selection
.Font.Name = "Times New Roman"
.Font.Italic = True
.Font.Size = 10.5
.TypeText vbCrLf
.Font.Italic = False
.Font.Name = "宋体"
.TypeText "很简单,首次调用后 Word 对象(比如 WordApp、newdoc)没有正确释放"
End With
Set newdoc = Nothing
Set WordApp = Nothing
End Sub
newdoc.SaveAs ...
Set newdoc = Nothing
WordApp.Quit
Set WordApp = Nothing
Private Sub Command1_Click()
Dim WordApp As New Word.Application
Set newdoc = WordApp.Documents.Add
WordApp.Visible = True
Set WordApp = Word.Application
With WordApp.Selection
.Font.Name = "Times New Roman"
.Font.Italic = True
.Font.Size = 10.5
.TypeText vbCrLf
.Font.Italic = False
.Font.Name = "宋体"
.TypeText "很简单,首次调用后 Word 对象(比如 WordApp、newdoc)没有正确释放"
End With
newdoc.SaveAs "c:\Doc1.doc"
Set newdoc = Nothing
WordApp.Quit
Set WordApp = Nothing
End Sub
出现的依然还是原来的这个错误,不知是什么原因,恳请大家帮帮忙,谢谢啦!
newdoc 声明在 Command1_Click() 内
Dim WordApp As New Word.Application
Set newdoc = WordApp.Documents.Add
WordApp.Visible = True With WordApp.Selection
.Font.Name = "Times New Roman"
.Font.Italic = True
.Font.Size = 10.5
Set myField = Selection.Fields.Add(Range:=WordApp.Selection.Range, Type:=wdFieldEmpty, Text:="EQ \r(2,x+y)",PreserveFormatting:=False)
.TypeText vbCrLf
.Font.Italic = False
.Font.Name = "宋体"
.TypeText "很简单,首次调用后 Word 对象(比如 WordApp、newdoc)没有正确释放"
newdoc.SaveAs "c:\Doc1.doc"
Set newdoc = Nothing
End With WordApp.Quit
Set WordApp = Nothing
End Sub 其中的myField这个对象如何释放呀!
再次感谢楼上朋友的帮忙,我会加分以示酬谢!
sub result1()
Set WordApp = Word.Application
With WordApp.Selection
……
……
end with
Set WordApp = Nothing
End subsub result2()
Set WordApp = Word.Application
With WordApp.Selection
……
……
end with
Set WordApp = Nothing
End subPrivate Sub Command1_Click()
Dim WordApp As New Word.Application
Set newdoc = WordApp.Documents.Add
WordApp.Visible = True
With WordApp.Selection
.Font.Name = "Times New Roman"
.Font.Italic = True
.Font.Size = 10.5
.TypeText vbCrLf
.Font.Italic = False
.Font.Name = "宋体"
.TypeText "很简单,首次调用后 Word 对象(比如 WordApp、newdoc)没有正确释放"
End With Call result1
Call result2Set newdoc = Nothing
WordApp.Quit
Set WordApp = Nothing
End Sub 这时还是出现那个错误,也就是说对象还是没有释放!
请问达人,这个该怎么办呀!!
对象通过参数传递才能保证是同一个实例,这里不适合用全局/模块变量。又:整个模块代码的第一行要保证是
Option Explicit
sub result1()
Set WordApp = Word.Application
With WordApp.Selection
……
……
end with
Set WordApp = Nothing
End sub sub result2()
Set WordApp = Word.Application
With WordApp.Selection
……
……
end with
Set WordApp = Nothing
End sub Private Sub Command1_Click()
Dim WordApp As New Word.Application
Set newdoc = WordApp.Documents.Add
WordApp.Visible = True
With WordApp.Selection
.Font.Name = "Times New Roman"
.Font.Italic = True
.Font.Size = 10.5
.TypeText vbCrLf
.Font.Italic = False
.Font.Name = "宋体"
.TypeText "很简单,首次调用后 Word 对象(比如 WordApp、newdoc)没有正确释放"
End With Call result1
Call result2 Set newdoc = Nothing
WordApp.Quit
Set WordApp = Nothing
End Sub
就拿这个来说,该怎么调用和传递才能最后释放呀!
Option Explicit '<-代码最前面一定要加Sub result1(ByVal WordApp As Word.Application)
With WordApp.Selection
……
……
End With
End SubSub result2(ByVal WordApp As Word.Application)
With WordApp.Selection
……
……
End With
End SubPrivate Sub Command1_Click()
Dim WordApp As Word.Application '<-避免使用 As New 定义
Dim newDoc As Word.Document '<-要显示定义局部变量
Set WordApp = New Word.Application
Set newDoc = WordApp.Documents.Add
WordApp.Visible = True
With WordApp.Selection
.Font.Name = "Times New Roman"
.Font.Italic = True
.Font.Size = 10.5
.TypeText vbCrLf
.Font.Italic = False
.Font.Name = "宋体"
.TypeText "很简单,首次调用后 Word 对象(比如 WordApp、newdoc)没有正确释放"
End With Call result1(WordApp)
Call result2(WordApp) newDoc.SaveAs ... '文档一定要保存、关闭,否则Word无法关闭
newDoc.Close
Set newDoc = Nothing
WordApp.Quit
Set WordApp = Nothing
End Sub