批量删除word中的某一页(求VBA代码)
1个回答
展开全部
Public StartPageNum As Integer, EndPageNum As Integer
Sub aaa()
Dim myDialog As FileDialog, oFile As Variant, oDoc As Document
On Error Resume Next
Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
myDialog.Filters.Clear '清除所有文件筛选器中的项目
myDialog.Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件
myDialog.AllowMultiSelect = True '允许多项选择
If myDialog.Show <> -1 Then Exit Sub
Dim SelectRange As Range
DlgDelePage.Show vbModal
If StartPageNum = 0 And EndPageNum = 0 Then
Exit Sub
End If
For Each oFile In myDialog.SelectedItems '在所有选取项目中循环
Set oDoc = Documents.Open(FileName:=oFile, Visible:=True) 'False)
Dim Pages As Integer, StartPage As Long, EndPage As Long
Pages = Selection.Information(wdNumberOfPagesInDocument)
If Not (StartPageNum > Pages) Then
If EndPageNum > Pages Then EndPageNum = Pages
If StartPageNum = 1 Then
StartPage = Selection.Range
Else
StartPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=StartPageNum - 1).Start
End If
If EndPageNum = Pages Then
EndPage = ActiveDocument.Content.End
Else
EndPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=IIf(EndPageNum - StartPageNum > 0, EndPageNum - StartPageNum + 1, 1)).End '.Start
End If
ActiveDocument.Range(StartPage, EndPage).Select
Selection.Delete
End If
'删除第3页批注
ActiveDocument.Words(1).Select '将光标移到文档开始位置
Dim myRange As Range, oComment As Comment
Set myRange = Selection.Range
StartPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=3 - 1).Start
EndPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=3 - 1).End
myRange = ActiveDocument.Range(StartPage, EndPage)
If myRange.Comments.Count > 0 Then
For Each oComment In myRange.Comments
oComment.Delete
Next
End If
oDoc.Save
oDoc.Close
Next oFile
End Sub
Sub aaa()
Dim myDialog As FileDialog, oFile As Variant, oDoc As Document
On Error Resume Next
Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
myDialog.Filters.Clear '清除所有文件筛选器中的项目
myDialog.Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件
myDialog.AllowMultiSelect = True '允许多项选择
If myDialog.Show <> -1 Then Exit Sub
Dim SelectRange As Range
DlgDelePage.Show vbModal
If StartPageNum = 0 And EndPageNum = 0 Then
Exit Sub
End If
For Each oFile In myDialog.SelectedItems '在所有选取项目中循环
Set oDoc = Documents.Open(FileName:=oFile, Visible:=True) 'False)
Dim Pages As Integer, StartPage As Long, EndPage As Long
Pages = Selection.Information(wdNumberOfPagesInDocument)
If Not (StartPageNum > Pages) Then
If EndPageNum > Pages Then EndPageNum = Pages
If StartPageNum = 1 Then
StartPage = Selection.Range
Else
StartPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=StartPageNum - 1).Start
End If
If EndPageNum = Pages Then
EndPage = ActiveDocument.Content.End
Else
EndPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=IIf(EndPageNum - StartPageNum > 0, EndPageNum - StartPageNum + 1, 1)).End '.Start
End If
ActiveDocument.Range(StartPage, EndPage).Select
Selection.Delete
End If
'删除第3页批注
ActiveDocument.Words(1).Select '将光标移到文档开始位置
Dim myRange As Range, oComment As Comment
Set myRange = Selection.Range
StartPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=3 - 1).Start
EndPage = Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=3 - 1).End
myRange = ActiveDocument.Range(StartPage, EndPage)
If myRange.Comments.Count > 0 Then
For Each oComment In myRange.Comments
oComment.Delete
Next
End If
oDoc.Save
oDoc.Close
Next oFile
End Sub
博思aippt
2024-07-20 广告
2024-07-20 广告
博思AIPPT是基于ai制作PPT的智能在线工具,它提供了4种AI制作PPT的方式,包括AI生成大纲、AI直接生成PPT、文本生成PPT、AI提炼word文档生成PPT,一站式集成多种AI生成PPT的方式,可满足办公用户的不同需求和使用场景...
点击进入详情页
本回答由博思aippt提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |