用VB打开一个已存在的word文档并向其中插入指定文本
2个回答
展开全部
Dim oWord As Word.Application
Dim oDocument As Word.Document
Dim nRet As Integer
On Error GoTo ErrOpen
Set oWord = New Word.Application
' If FileName = "" Then
' Set oDocument = oWord.Documents.Add
' Else
Set oDocument = oWord.Documents.Open("e:\test1.doc")'要打开的文件名
' End If
oWord.Visible = True '如果不想看见WORD窗口,可以设为false
'将text插入到最后
oDocument.Paragraphs.Last.Range.InsertAfter Text1.Text
OpenDocument = 1
GoTo ExitOpen
ErrOpen:
OpenDocument = 0
ExitOpen:
oDocument.Save '保存文档
oWord.Quit '退出WORD
Set oDocument = Nothing
Set oWord = Nothing
Dim oDocument As Word.Document
Dim nRet As Integer
On Error GoTo ErrOpen
Set oWord = New Word.Application
' If FileName = "" Then
' Set oDocument = oWord.Documents.Add
' Else
Set oDocument = oWord.Documents.Open("e:\test1.doc")'要打开的文件名
' End If
oWord.Visible = True '如果不想看见WORD窗口,可以设为false
'将text插入到最后
oDocument.Paragraphs.Last.Range.InsertAfter Text1.Text
OpenDocument = 1
GoTo ExitOpen
ErrOpen:
OpenDocument = 0
ExitOpen:
oDocument.Save '保存文档
oWord.Quit '退出WORD
Set oDocument = Nothing
Set oWord = Nothing
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询