Select 除最后 3 页外的所有页面
Select all pages except the 3 last ones
我正在尝试 select 文档的所有页面,最后 3 页除外。我的最终目标是将样式应用于具有特定字体名称和特定字体大小的文本。当 运行 下面的代码关于我的第 3 行代码时,我收到一条错误消息:“对象不支持此 属性 或方法”。知道这是关于什么的吗?谢谢!
Sub aHeadlines()
Dim V As Integer
Dim Z As Integer
V = ActiveDocument.Information(wdNumberOfPagesInDocument)
Z = 3
Dim rgePages As Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=1
Set rgePages = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=V - Z
rgePages.End = Selection.Bookmarks("\Page").Range.End
rgePages.Select
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Size = 10
.Font.Name = "Arial"
.Font.Bold = True
With .Replacement
.ClearFormatting
.Text = ""
.Style = ActiveDocument.Styles("Heading 1")
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
Information
是 Range
对象的 属性,而不是 Document
。
V = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
注意:
使用 VBA 时很少需要 select 任何东西。使用 Selection
对象会减慢您的代码,因为光标会随着 selection 的每次更改而移动,这意味着每次都必须重新绘制屏幕。而不是 Selection
使用适当的对象来做你想做的事情,例如Range
、Table
、Shape
等
您的代码可以使用 Range 重写如下:
Sub aHeadlines()
Dim V As Integer
Dim Z As Integer
V = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
Z = 3
Dim rgePages As Range
Set rgePages = ActiveDocument.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=V - Z)
rgePages.End = rgePages.Bookmarks("\Page").Range.End
rgePages.Start = ActiveDocument.Range.Start
With rgePages.Find
.ClearFormatting
.Text = ""
.Font.Size = 10
.Font.Name = "Arial"
.Font.Bold = True
.Wrap = wdFindStop
With .Replacement
.ClearFormatting
.Text = ""
.Style = ActiveDocument.Styles("Heading 1")
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
更简单:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument
With .Range(0, .Range.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, _
Count:=.ComputeStatistics(wdStatisticPages) - 2).End - 1).Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Font.Size = 10
.Font.Name = "Arial"
.Font.Bold = True
.Replacement.Style = wdStyleHeading1
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
我正在尝试 select 文档的所有页面,最后 3 页除外。我的最终目标是将样式应用于具有特定字体名称和特定字体大小的文本。当 运行 下面的代码关于我的第 3 行代码时,我收到一条错误消息:“对象不支持此 属性 或方法”。知道这是关于什么的吗?谢谢!
Sub aHeadlines()
Dim V As Integer
Dim Z As Integer
V = ActiveDocument.Information(wdNumberOfPagesInDocument)
Z = 3
Dim rgePages As Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=1
Set rgePages = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=V - Z
rgePages.End = Selection.Bookmarks("\Page").Range.End
rgePages.Select
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Size = 10
.Font.Name = "Arial"
.Font.Bold = True
With .Replacement
.ClearFormatting
.Text = ""
.Style = ActiveDocument.Styles("Heading 1")
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
Information
是 Range
对象的 属性,而不是 Document
。
V = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
注意:
使用 VBA 时很少需要 select 任何东西。使用 Selection
对象会减慢您的代码,因为光标会随着 selection 的每次更改而移动,这意味着每次都必须重新绘制屏幕。而不是 Selection
使用适当的对象来做你想做的事情,例如Range
、Table
、Shape
等
您的代码可以使用 Range 重写如下:
Sub aHeadlines()
Dim V As Integer
Dim Z As Integer
V = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
Z = 3
Dim rgePages As Range
Set rgePages = ActiveDocument.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=V - Z)
rgePages.End = rgePages.Bookmarks("\Page").Range.End
rgePages.Start = ActiveDocument.Range.Start
With rgePages.Find
.ClearFormatting
.Text = ""
.Font.Size = 10
.Font.Name = "Arial"
.Font.Bold = True
.Wrap = wdFindStop
With .Replacement
.ClearFormatting
.Text = ""
.Style = ActiveDocument.Styles("Heading 1")
End With
.Execute Replace:=wdReplaceAll
End With
End Sub
更简单:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument
With .Range(0, .Range.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, _
Count:=.ComputeStatistics(wdStatisticPages) - 2).End - 1).Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Font.Size = 10
.Font.Name = "Arial"
.Font.Bold = True
.Replacement.Style = wdStyleHeading1
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub