又要麻烦守柔斑主,为什么上面你帮我写的代码,在EXCEL中只能做到打开指定的文档,却不运行替换功能,麻烦你再帮我看看:(下面是我的代码,运行后的结果是只打开了文档,而相关于页面设置以及替换英文字母及空格却没有运行) Option Compare Text '
Option Explicit '显式变量声明 Sub PrintInWordPages()
Sheet10.Cells(1, 16).FormulaR1C1 = "=DATE(YEAR(NOW()),MONTH(NOW()),0)" '写上月末日期
Sheet10.Cells(1, 17).FormulaR1C1 = "=YEAR(RC[-1])&IF(LEN(MONTH(RC[-1]))=1,""0""&MONTH(RC[-1]),MONTH(RC[-1]))&DAY(RC[-1])"
Sheet3.Activate
Sheet3.Range(Cells(1, 18), Cells(1, 18)).NumberFormatLocal = "@"
Sheet3.Cells(1, 18) = Sheet10.Cells(1, 17)
Dim WdApp As Word.Application, WdDoc As Word.Document
Dim DocFullName As String, FindString As String, PageItem As String
Dim Fcount As Integer
On Error Resume Next '忽略错误
'获得文档路径
DocFullName = ThisWorkbook.Path & "\往来对账单\" & Sheet3.Cells(1, 18) & ".Doc"
FindString = Sheet10.[m1] '获得搜索字符
' MsgBox (FindString)
'如果找不到该文档,则友情提示并退出程序
If Dir(DocFullName) = "" Then MsgBox "Excel没有找到" & DocFullName & " ,请确认WORD文档名是否正确!", vbExclamation, "Warnning": Exit Sub
Set WdApp = New Word.Application '定义一个新的WORD Application
WdApp.Visible = True '显示程序窗口
'定义一个WdDoc文档,为指定打开的文档
Set WdDoc = WdApp.Documents.Open(Filename:=DocFullName, PasswordDocument:="gzw212535")
WdDoc.Active
With ActiveDocument.Styles(wdStyleNormal).Font
If .NameFarEast = .NameAscii Then
.NameAscii = ""
End If
.NameFarEast = ""
End With
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(0.9)
.BottomMargin = CentimetersToPoints(1.2)
.LeftMargin = CentimetersToPoints(1.4)
.RightMargin = CentimetersToPoints(1.4)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1)
.FooterDistance = CentimetersToPoints(1)
.PageWidth = CentimetersToPoints(24)
.PageHeight = CentimetersToPoints(28)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
.LayoutMode = wdLayoutModeLineGrid
End With Dim i As Range, DelText As String
DelText = "In case error*changed."
With ActiveDocument '活动文档
Application.ScreenUpdating = False '关闭屏幕更新
With .Content.Find '查找
.ClearFormatting '清除查找格式
.MatchWildcards = True '使用通配符
'全部替换为空格
.Execute FindText:=DelText, ReplaceWith:="", Replace:=wdReplaceAll
End With
For Each i In .Words '遍历每个词组
If i Like "[A-Z]*" And Not i Like "*#*" Then i = VBA.Space(Len(i))
Next
With .Content.Find '查找
.ClearFormatting '清除查找格式
'将如NO.等形式的.替换为空格
.Execute FindText:=" .", ReplaceWith:=" ", Replace:=wdReplaceAll
End With
End With
Dim J As Integer, FindString1 As String
With ActiveDocument.Content.Find '在全文中查找
.ClearFormatting '清除查找格式
For J = 6 To 0 Step -1 '循环
FindString1 = VBA.Space(2 ^ J) & "^p" '生成指定的查找项目文字
'全部替换"
.Execute FindText:=FindString1, ReplaceWith:="^p", Replace:=wdReplaceAll
Next
End With
“我想在此再加上保存及设置密码后再退出
WdApp.Quit '退出WORD
Set WdApp = Nothing '释放对象变量
End Sub 还请斑主指教! |