
原来链接了Word文档的OLE,ole.object就相当于word.docunment,呵呵呵
曾经做过些。复制下来给你参考
我是从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 ReduceParagraph()
'必须写为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