怎样在VB中控制Word !恳请指点!

yunhaikunpeng 2004-08-31 04:12:21
怎样在VB中控制Word 是我在实现查询之后的一个很大的问题

我必须实现这个功能
同时具备生成word文档
继而打印的功能


小弟希望有大哥出手相助
提供解说以及介绍相应网址,和书籍。
~~~~~~~~注意很希望介绍相应网址,和书籍。~~~~~~~~~~~~~~~~~~~
...全文
106 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
RUKYO 2004-08-31
'接上:

Public Sub DeleteToEnd()
Attributes DeleteToEnd.VB_Description = "删除从当前位置到结尾的所有内容"
mysel.EndKey Unit:=wdStory, Extend:=wdExtend
mysel.Delete Unit:=wdCharacter, Count:=1
End Sub

Public Sub MoveEnd()
Attributes MoveEnd.VB_Description = "光标移动到文档结尾"
'光标移动到文档结尾
mysel.EndKey Unit:=wdStory
End Sub

Public Sub GotoLine(LineTime As Integer)
mysel.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=LineTime, Name:=""
End Sub

Public Sub OpenDoc(view As Boolean)
Attributes OpenDoc.VB_Description = "打开Word文件,View确定是否显示Word界面"
On Error Resume Next

'********************************************************************************
'     打开Word文件,并给全局变量mysel赋值
'********************************************************************************

If Len(C_TemplateDoc) = 0 Then
mywdapp.Documents.Add
Else
mywdapp.Documents.Open (C_TemplateDoc)
End If

If Err.Number <> 0 Then
C_ErrMsg = 4
RaiseEvent HaveError
Exit Sub
End If

mywdapp.Visible = view
mywdapp.Activate
Set mysel = mywdapp.Application.Selection
'mysel.Select

End Sub

Public Sub OpenWord()
On Error Resume Next

'********************************************************************************
'     打开Word程序,并给全局变量mywdapp赋值
'********************************************************************************

Set mywdapp = CreateObject("word.application")
If Err.Number <> 0 Then
C_ErrMsg = 1
RaiseEvent HaveError
Exit Sub
End If
End Sub

Public Sub ViewDoc()
Attributes ViewDoc.VB_Description = "显示Word程序界面"
mywdapp.Visible = True
End Sub

Public Sub AddNewPage()
Attributes AddNewPage.VB_Description = "插入分页符"
mysel.InsertBreak Type:=wdPageBreak
End Sub

Public Sub WordCut()
Attributes WordCut.VB_Description = "剪切模板所有内容到剪切板"
'保存模板页面内容
mysel.WholeStory
mysel.Cut
mysel.HomeKey Unit:=wdStory
End Sub

Public Sub WordCopy()
Attributes WordCopy.VB_Description = "拷贝模板所有内容到剪切板"
mysel.WholeStory
mysel.Copy
mysel.HomeKey Unit:=wdStory
End Sub

Public Sub WordDel()
mysel.WholeStory
mysel.Delete
mysel.HomeKey Unit:=wdStory
End Sub

Public Sub WordPaste()
Attributes WordPaste.VB_Description = "拷贝剪切板内容到当前位置"
'插入模块内容
mysel.Paste
End Sub

Public Sub CloseDoc()
Attributes CloseDoc.VB_Description = "关闭Word文件模板"
'********************************************************************************
'     关闭Word文件模本
'********************************************************************************
On Error Resume Next


mywdapp.ActiveDocument.Close False

If Err.Number <> 0 Then
C_ErrMsg = 3
Exit Sub
End If

End Sub

Public Sub QuitWord()
'********************************************************************************
'     关闭Word程序
'********************************************************************************
On Error Resume Next

mywdapp.Quit

If Err.Number <> 0 Then
C_ErrMsg = 3
Exit Sub
End If
End Sub

Public Sub SavetoDoc()
Attributes SavetoDoc.VB_Description = "保存当前文档为FileName指定文件"
On Error Resume Next

'并另存为文件FileName

If Len(C_newDoc) = 0 Then
C_ErrMsg = 2
RaiseEvent HaveError
Exit Sub
End If

mywdapp.ActiveDocument.SaveAs (C_newDoc)

If Err.Number <> 0 Then
C_ErrMsg = 3
RaiseEvent HaveError
Exit Sub
End If

End Sub


Public Property Get TemplateDoc() As String
Attributes TemplateDoc.VB_Description = "模板文件名."
TemplateDoc = C_TemplateDoc
End Property

Public Property Let TemplateDoc(ByVal vNewValue As String)
C_TemplateDoc = vNewValue
End Property

Public Property Get newdoc() As String
Attributes newdoc.VB_Description = "执行CloseDoc方法时,将模板文件另存为此文件名指定的新文件.如果不指定,在执行CloseDoc方法时,将产生一个错误"
newdoc = C_newDoc
End Property

Public Property Let newdoc(ByVal vNewValue As String)
C_newDoc = vNewValue
End Property

Public Property Get PicFile() As String
Attributes PicFile.VB_Description = "图像文件名"
PicFile = C_PicFile
End Property

Public Property Let PicFile(ByVal vNewValue As String)
C_PicFile = vNewValue
End Property

Public Property Get ErrMsg() As Integer
Attributes ErrMsg.VB_Description = "错误信息.ErrMsg代码: 1-word没有安装 2-缺少参数 3-没权限写文件 4-文件不存在"
ErrMsg = C_ErrMsg
End Property



回复
RUKYO 2004-08-31
'是这里某位前辈的类模块代码,借花献佛:


'类模块SetWord.Cls
'VERSION 1.0 CLASS
Private Sub Class_Initialize()
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
End Sub

Attributes Me.VB_Name = "SetWord"
Attributes Me.VB_GlobalNameSpace = False
Attributes Me.VB_Creatable = True
Attributes Me.VB_PredeclaredId = False
Attributes Me.VB_Exposed = False


Private mywdapp As Word.Application
Private mysel As Object

'属性值的模块变量
Private C_TemplateDoc As String
Private C_newDoc As String
Private C_PicFile As String
Private C_ErrMsg As Integer

Public Event HaveError()
Attributes HaveError.VB_Description = "出错时激发此事件.出错代码为ErrMsg属性"
'***************************************************************
'ErrMsg代码:1-word没有安装 2 - 缺少参数 3 - 没权限写文件
' 4 - 文件不存在
'
'***************************************************************

Public Function ReplacePic(FindStr As String, Optional Time As Integer = 0) As Integer
Attributes ReplacePic.VB_Description = "查找FindStr,并替换为PicFile所指向的图片文件,替换次数由time参数确定,为0时,替换所有"

'********************************************************************************
'    从Word.Range对象mysel中查找所有FindStr,并替换为PicFile图像
' 替换次数由time参数确定,为0时,替换所有
'********************************************************************************

If Len(C_PicFile) = 0 Then
C_ErrMsg = 2
Exit Function
End If

Dim i As Integer
Dim findtxt As Boolean

mysel.Find.ClearFormatting
mysel.Find.Replacement.ClearFormatting
With mysel.Find
.Text = FindStr
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
mysel.HomeKey Unit:=wdStory
findtxt = mysel.Find.Execute(Replace:=True)
If Not findtxt Then
ReplacePic = 0
Exit Function
End If
i = 1
Do While findtxt
mysel.InlineShapes.AddPicture FileName:=C_PicFile
If i = Time Then Exit Do
i = i + 1
mysel.HomeKey Unit:=wdStory
findtxt = mysel.Find.Execute(Replace:=True)
Loop
ReplacePic = i
End Function

Public Function FindThis(FindStr As String) As Boolean
Attributes FindThis.VB_Description = "查找FindStr,如果模板中有FindStr则返回True"
If Len(FindStr) = 0 Then
C_ErrMsg = 2
Exit Function
End If
mysel.Find.ClearFormatting
mysel.Find.Replacement.ClearFormatting
With mysel.Find
.Text = FindStr
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
mysel.HomeKey Unit:=wdStory
FindThis = mysel.Find.Execute
End Function

Public Function ReplaceChar(FindStr As String, RepStr As String, Optional Time As Integer = 0) As Integer
Attributes ReplaceChar.VB_Description = "查找FindStr,并替换为RepStr,替换次数由time参数确定,为0时,替换所有"
'********************************************************************************
'     从Word.Range对象mysel中查找FindStr,并替换为RepStr
' 替换次数由time参数确定,为0时,替换所有
'********************************************************************************
Dim findtxt As Boolean

If Len(FindStr) = 0 Then
C_ErrMsg = 2
RaiseEvent HaveError
Exit Function
End If

mysel.Find.ClearFormatting
mysel.Find.Replacement.ClearFormatting
With mysel.Find
.Text = FindStr
.Replacement.Text = RepStr
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With


If Time > 0 Then
For i = 1 To Time
mysel.HomeKey Unit:=wdStory
findtxt = mysel.Find.Execute(Replace:=wdReplaceOne)
If Not findtxt Then Exit For
Next
If i = 1 And Not findtxt Then
ReplaceChar = 0
Else
ReplaceChar = i
End If
Else
mysel.Find.Execute Replace:=wdReplaceAll
End If
End Function



Public Function GetPic(PicData() As Byte, FileName As String) As Boolean
Attributes GetPic.VB_Description = "把图像数据PicData,存为PicFile指定的文件"
'********************************************************************************
'     把图像数据PicData,存为PicFile指定的文件
'********************************************************************************
On Error Resume Next

If Len(FileName) = 0 Then
C_ErrMsg = 2
RaiseEvent HaveError
Exit Function
End If

Open FileName For Binary As #1

If Err.Number <> 0 Then
C_ErrMsg = 3
Exit Function
End If

'二进制文件用Get,Put存放,读取数据
Put #1, , PicData
Close #1

C_PicFile = FileName
GetPic = True

End Function


回复
RUKYO 2004-08-31
'工程->引用Microsoft Word x.0 Object Library
Private Sub Command1_Click()
Set wrdApp = New Word.Application '写word文档
With wrdApp
'Show Word
.Visible = True
'Create New Document
.Documents.Add
'Add text to the document
.ActiveDocument.Content.Text = "Hello"
.ActiveDocument.Content.Text = "This is a test example"
End With
End Sub

Private Sub Command2_Click()
Dim sFileName, sContent, sPartContent As String '打开word文档
Dim wrdApp As Object
Dim k As String
CommonDialog1.ShowOpen
If Err <> 0 Then Exit Sub
sFileName = CommonDialog1.FileName
If sFileName = "" Then Exit Sub
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
wrdApp.Documents.Open (sFileName)
sContent = wrdApp.ActiveDocument.Content
End Sub
回复
haohaohappy 2004-08-31
http://search.csdn.net/Expert/topic/2308/2308060.xml?temp=.7929804
回复
haohaohappy 2004-08-31
http://search.csdn.net/Expert/topic/2461/2461212.xml?temp=.8323328
回复
相关推荐
发帖

1188

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2004-08-31 04:12
社区公告
暂无公告