VB如对OLE控件中的word内容进行替换
发布网友
发布时间:2022-08-12 03:18
我来回答
共1个回答
热心网友
时间:2024-11-23 11:36
曾经做过些。复制下来给你参考
我是从XML里读到数据往Word里面替换数据的。
制作一个Word模板,里面可变的内容用{$TITLE}代替
{$TITLE}
Dim wordApp As Word.Application
Set wordApp = New Word.Application
wordApp.Visible = False
Dim wordSelection As Word.Selection
Dim objDoc As Word.Document
Set objDoc = wordApp.Documents.Open("template.doc")
Set wordSelection = wordApp.Selection
ReplaceChar "{$TITLE}", xmlNode.Text
If xmlNodes.Length > 1 Then
objDoc.Tables(1).Select
Selection.InsertRowsBelow xmlNodes.Length - 1
End If
'美化Word文件:去除掉重复的段落标记。
Public Sub ReceParagraph()
'必须写为wordApp.Selection
wordApp.Selection.Find.ClearFormatting
wordApp.Selection.Find.Replacement.ClearFormatting
With wordApp.Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wordApp.Selection.Find.Execute Replace:=wdReplaceAll
End Sub
'直接将全部匹配的标签替换为结果文本。
Public Sub ReplaceChar(ReplacedStr As String, ReplacementStr As String)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ReplacedStr
.Replacement.Text = ReplacementStr
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
'Selection.TypeText Text:=ReplacementStr
End Sub
'从前往后,查找图片标签,然后直接插入图片,图片文件可以本地全路径或者Web全路径。
Public Sub ReplaceImg(ReplacedStr As String, ReplacementStr As String)
Selection.Find.ClearFormatting
With Selection.Find
.Text = ReplacedStr
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=1
Selection.InlineShapes.AddPicture fileName:= _
ReplacementStr, LinkToFile:=False, _
SaveWithDocument:=True
End Sub追问谢谢
不过你用的这种方法是
Word.Application方式实现的,我现在是想对OLE控件里打开的Word文档进行替换
追答惭愧