|
楼主 |
发表于 2013-7-1 18:23
|
显示全部楼层
那么的帅 发表于 2013-6-29 21:51
文件中的要替换的"数据7",在word文件中出现了两次,代码只能替换一个,要如何才能替换两次呀?
原代码如下
Private Sub CommandButton1_Click()
Dim f$, f2$, p$, arr, i&, j&, lr&, s$
Application.ScreenUpdating = False
arr = [a1].CurrentRegion
lr = UBound(arr)
For i = 2 To lr
If arr(i, 9) <> "已打印" Then
s = s & arr(i, 2)
K = K + 1
End If
Next
If s <> "" Then
p = ThisWorkbook.Path & "\"
d = Format(Date, "yyyy年mm月dd日")
f = p & "新建 Microsoft Word 文档.doc"
f2 = p & s & ".doc"
FileCopy f, f2
With CreateObject("Word.Application")
.Documents.Open f2
.ActiveWindow.ActivePane.View.SeekView = 0 ' wdSeekMainDocument '设置位置在正文
.Selection.WholeStory '全选
.Selection.Copy '复制
For i = 1 To K - 1 '复制页
.Selection.EndKey Unit:=6 'wdStory '光标置于文件尾
.Selection.InsertBreak Type:=7 'wdPageBreak '分页
.Selection.PasteAndFormat (0) '粘贴wdPasteDefault
Next i
For i = 2 To lr
If arr(i, 9) <> "已打印" Then
arr(i, 1) = WorksheetFunction.Text(arr(i, 1), "[DBNum1]yyyy年m月d日")
For j = 1 To 8
If j <> 3 Then
.Selection.HomeKey Unit:=6
If .Selection.Find.Execute("数据" & j) Then
.Selection.Text = arr(i, j)
If j = 2 Then .Selection.Font.Name = "隶书"
End If
End If
Next
Range("I" & i) = "已打印"
End If
Next
.Documents.Save
.Selection.HomeKey Unit:=6
.Visible = True
End With
Else
MsgBox "没有需要打印的数据"
End If
Application.ScreenUpdating = True
End Sub
|
|