word 2010下,如何批量删除Work的页眉和页脚,然后存为PDF文档

news/2024/10/23 20:28:53/


今天有这个需要,就尝试写了一段宏。

目的是把一个目录下的所有的word文件的页眉页脚删除,然后存成PDF。

这样的好处是,在电纸书上看时,能清爽一些。


再加上,办公室里,闷得我喘不上气来,也只能放弃干活,把这个事解决一下吧。


找了几个文章,写出了如下的代码,因为我装的office 2010,所以只在2010上验证过。


另外,我没有把处理work和导出PDF写在一起,目前是两个函数。



Sub 删除页眉内容()Dim j As SectionDim y As HeaderFooterFor Each j In ActiveDocument.SectionsFor Each y In j.Headersy.Range.Deletey.Range.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNoneNextNext
End SubSub 删除页脚内容()Dim j As SectionDim y As HeaderFooterFor Each j In ActiveDocument.SectionsFor Each y In j.Footersy.Range.DeleteNextNext
End Sub





Sub 批量删除页眉页脚()Dim MyPath As String, i As Integer, myDoc As DocumentWith Application.FileDialog(msoFileDialogFolderPicker).Title = "选择要处理目标文件夹" & "——(删除里面所有Word文档的页眉页脚)"If .Show = -1 ThenMyPath = .SelectedItems(1)ElseExit SubEnd IfEnd WithChangeFileOpenDirectory MyPathmyfilename = Dir(MyPath & "\*.doc")Do While myfilename <> ""'MsgBox myfilename''''''''''''''''''Dim curFileName
curFileName = MyPath & "\" & myfilenameSet myDoc = Documents.Open(FileName:=curFileName, Visible:=True)删除页眉内容删除页脚内容' 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:=wdCharascter, 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'强制执行“否”Call ActiveDocument.SaveActiveDocument.Close '退出'''''''''''''''''''myfilename = DirLoop
End Sub



Public Function GetFileExtName(ByVal FileNameData As String) As StringDim strFileName As StringstrFileName = FileNameDataIf InStr(1, strFileName, ".") = 0 ThenGetFileExtName = ""ElseDim iLenkiLenk = InStr(1, strFileName, ".")GetFileExtName = Left(strFileName, iLenk)End If
End FunctionSub 批量导出PDF()Dim MyPath As String, i As Integer, myDoc As DocumentWith Application.FileDialog(msoFileDialogFolderPicker).Title = "选择要处理目标文件夹" & "——(删除里面所有Word文档的页眉页脚)"If .Show = -1 ThenMyPath = .SelectedItems(1)ElseExit SubEnd IfEnd WithChangeFileOpenDirectory MyPathmyfilename = Dir(MyPath & "\*.doc")Do While myfilename <> ""'MsgBox myfilename''''''''''''''''''Dim curFileName
curFileName = MyPath & "\" & myfilenameSet myDoc = Documents.Open(FileName:=curFileName, Visible:=True)Dim curPdfNamecurPdfName = GetFileExtName(curFileName) + "pdf"ActiveDocument.ExportAsFixedFormat OutputFileName:= _curPdfName _, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _BitmapMissingFonts:=True, UseISO19005_1:=FalseApplication.DisplayAlerts = False '强制执行“是”ActiveDocument.Close '退出' ActiveWindow.Close'''''''''''''''''''myfilename = DirLoop
End Sub



http://www.ppmy.cn/news/797086.html

相关文章

itext Pdf页眉/页脚/水印

package com.company.common.core.utils.itext;import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import lombok.SneakyThrows;import java.io.IOException;/*** pdf的页眉页脚设置** author kally* date 2022/8/26*/ public class PdfHeaderFooter extends PdfPa…

电脑重装系统word从第二页开始有页眉页脚如何设置

word第一页不要页眉页脚&#xff0c;从第二页开始有&#xff0c;如何设置&#xff1f;许多用户在使用Word文档的时候经常会用到这个功能&#xff0c;那么具体要怎么设置呢&#xff1f;其实方法也非常的简单&#xff0c;下面就讲讲电脑重装系统word从第二页开始有页眉页脚如何设…

iText7添加页眉、页脚

iText7添加页眉、页脚 iText7添加页眉、页脚版本号及maven依赖PdfHeaderFooterEventPdfTest iText7添加页眉、页脚 因为iText5对部分pdf操作的支持没有iText7的多&#xff0c;所以后续将项目原本用的itext5升级到了iText7&#xff0c;本文就整理一下iText7添加页眉和页脚的实现…

word2016中的毕设论文——页眉页脚相关设置及页眉横线的删除

word2016中的毕设论文——页眉页脚相关设置及页眉横线的删除 毕设论文的格式要求双面打印设置页眉设置封面摘要ABSTRACT目录第一章 绪论后续页眉横线的删除 PS 毕设论文的格式要求 双面打印设置 由于毕设论文需要双面打印&#xff0c;为了方便装订&#xff0c;首先需要将论文…

怎么把word转换成只有一页的长页PDF?

来百度APP畅享高清图片 要将Word文档打印成一长页的PDF格式&#xff0c;我们得先知道word转PDF的工作原理。word转pdf其实就是将word打印出来&#xff0c;就是跟你用物理打印机打印的原理是差不多的&#xff0c;所不同的是&#xff0c;PDF虚拟打印的原理是利用虚拟打印机驱动程…

面试经验小点

作者&#xff1a;南极 链接&#xff1a;https://www.zhihu.com/question/23858584/answer/150506189 来源&#xff1a;知乎 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 注&#xff1a;适用于工作一到三年有一定经验的Java后端人员&#…

全世界的AI明星公司都在这!CB人工智能100深度拆解

12月13日&#xff0c;国家工业和信息化部发布《促进新一代人工智能产业发展三年行动计划&#xff08;2018-2020年&#xff09;》&#xff0c;宣布在接下来的2018-2020这三年内&#xff0c;国家要重点推动人工智能和实体经济深度融合&#xff0c;推进人工智能技术产业化、集成应…

html autocomplete 多个参数切换

<!doctype html> <html lang"en"> <head><meta charset"utf-8"><title>自助搜索</title><link rel"stylesheet" href"//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css"><…