注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
利用Windows控制台删除无法删除的
始料未及,谁在控制电脑重新启动
看我谋取特权 用漏洞提升计算机控
也玩入侵 通过“鼠洞”控制你的电
看我谋取特权 用漏洞提升电脑控制
文件上传八大法-远程控制必看
qmail控制文件详解
连接SQL SERVER的公共类(VB.NET)
一个自动生成用ADO调用SQL SERVE
VB访问SQLServer的探讨
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> VB >> 文章正文
vb控制word的类模块,查找、替换Word文档内容
作者:Equn  来源:CSDN  点击:  更新:2006-12-19
简介:
在VB6.0中,操作word,使用它强大的查找、替换、删除、复制、翦切功能。还可以把特定字符替换成图片。有了它你就可以使用数据库中的内容或图片文件替换word文件中的特定字符。

  只要把下列内容复制到写字板中,另存为SetWord.cls文件,然后在把它添加到工程中,就可以使用了。

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "SetWord"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute 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()
Attribute HaveError.VB_Description = "出错时激发此事件.出错代码为ErrMsg属性"
'***************************************************************
'ErrMsg代码:1-word没有安装 2 - 缺少参数  3 - 没权限写文件
'           4 - 文件不存在
'
'***************************************************************

Public Function ReplacePic(FindStr As String, Optional Time As Integer = 0) As Integer
Attribute 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
Attribute 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
Attribute 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
Attribute 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


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

Public Sub MoveEnd()
Attribute 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)
Attribute 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
  &n

[1] [2] 下一页






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务