在日常工作中,我們有時會碰到一大堆檔案需要批量刪除頁首與頁尾,如果一個個開啟檔案手工刪除的話,會很耗時間精力,下面小編就為大家介紹一個小技巧。
工具/原料
電腦
方法/步驟
首先,我先製作一個測試資料夾,裡面包含幾個word文件,都有頁首--這四個字,當然其他的文字也行,只是一個測試;
下面我們需要開啟巨集,03版的巨集不需要去開啟開發工具,只需要點選選單欄中的工具-巨集-巨集;
進入巨集對話方塊,我們可以在巨集名隨便輸入英文字母,在右側點選一下建立;
接著,進入了巨集程式碼編寫框,將下面程式碼複製進來:
Sub 批量刪除頁首頁尾()
Application.ScreenUpdating = False
Dim MyPath As String, i As Integer, myDoc As Document
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "選擇要處理目標資料夾" & "——(刪除裡面所有Word文件的頁首頁尾)"
If .Show = -1 Then
MyPath = .SelectedItems(1)
Else
Exit Sub
End If
End With
With Application.FileSearch
.LookIn = MyPath
.FileType = msoFileTypeWordDocuments
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
Set myDoc = Documents.Open(FileName:=.FoundFiles(i))
' B可以替換的巨集
' 以下是處理格式所錄製的巨集,可根據所需錄製
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.WholeStory
With Selection.ParagraphFormat
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
With .Borders
.DistanceFromTop = 1
.DistanceFromLeft = 4
.DistanceFromBottom = 1
.DistanceFromRight = 4
.Shadow = False
End With
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth075pt
.DefaultBorderColor = wdColorAutomatic
End With
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True
' 以上可以換成是你自己錄製的巨集
' C公共部分的程式碼
Application.DisplayAlerts = False '強制執行“是”
'ActiveDocument.Saved = True'強制執行“否”
ActiveDocument.Close '退出
Next
End If
End With
Application.ScreenUpdating = True
MsgBox "所選Word文件的頁首頁尾已刪除!!!", 64, "☆★處理完畢★☆"
End Sub
現在,我們關閉視窗,重新回到word介面,按下巨集快捷鍵,Alt+F8,看到剛才的巨集名,點選執行;
接著軟體會讓我們選擇要批量處理的資料夾,選中之,然後稍等片刻,即會彈出--所選Word文件的頁首頁尾已刪除。