有一软件,有如下通用模式,只是根据系统运行结果要填5个空,如下[]所示,不知在Word中做成如下形式,在VB中能否实现动态填写样本:
根据系统运行结果,该地区综合得分[1]分。表明该地区[2]年经济快速增长,综合实力明显增强,人民生活水平显著提高,社会事业协调发展,生态环境取得成效,党建工作成绩突出,文明建设步伐加快,整体评价领导班子政绩[3]。
但[4]指标出现警示,在[5]方面需注意改进。
根据系统运行结果,该地区综合得分[1]分。表明该地区[2]年经济快速增长,综合实力明显增强,人民生活水平显著提高,社会事业协调发展,生态环境取得成效,党建工作成绩突出,文明建设步伐加快,整体评价领导班子政绩[3]。
但[4]指标出现警示,在[5]方面需注意改进。
上面的那段话是一个WORD的模版在相应的[]处写上一个变量的名字。然后录制一个宏,就是查找-替换的功能就可以,然后在VB中调用WORD应用程序。打开WORD模版文件。将宏的代码考到VB中稍微修改点点,就可以实现在VB中进行查找WORD模版中的[]内容并替换成你想换成的变量值了。
录制好的宏的代码如下:
Sub Macro1()
'
' Macro1 Macro
' 宏在 2004-10-31 由 Joe 录制
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[UNIT1]"
.Replacement.Text = "2004"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
End Sub
在VB中的代码大概给你写一些Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Set wdApp = CreateObject("Word.application")dim strFileName as string
strFilename = "c:/test.doc"
wdApp.Documents.Open FileName:=strFileName, ConfirmConversions _
:=False, ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto现在已经打开一个WORD模版文件了。后面的操作就是把变量替换上去就行了。用上面录制的宏的内容来做
wdApp.Selection.Find.ParagraphFormat.Borders.Shadow = False
wdApp.Selection.Find.Frame.TextWrap = True
wdApp.Selection.Find.ClearFormatting
wdApp.Selection.Find.Replacement.ClearFormattingdim strTemp as string
strTemp = "2004"If strTemp <> "" Then
With wdApp.Selection.Find
.Text = "[UNIT1]"
.Replacement.Text = strTemp
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With wdApp.Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
End If这样就完成了。你自己改一下好了