循环浏览文件夹和子文件夹,将路径和文件名作为文本插入到标题中,打印,关闭而不保存



我必须遍历许多启用数据的模板,并将路径和文件名作为文本插入到标题中,这样当我打印它们显示代码而不是值时,它不会影响路径/文件名。所以我的任务是按以下顺序执行此操作:

  1. 打开模板(除非可以通过编程方式完成!
  2. 将路径和文件名作为文本插入标题
  3. 发送到打印
  4. 退出而不保存模板
  5. 做下一个模板

这是我必须循环浏览文件夹的内容...

Sub PrintAllFilesInAFolder()
Dim sMyDir As String
Dim sDocName As String
' The path to obtain the files.
sMyDir = "C:SomeFolderSomeSubFolderSomeDocument"
sDocName = Dir(sMyDir & "*.dotx")
While sDocName <> ""
' Print the file.
Application.PrintOut FileName:=sMyDir & sDocName
' Get next file name.
sDocName = Dir()
Wend
End Sub

不知道该怎么做,所以如果有人可以将 vba 插入我需要的地方,我将不胜感激:-(

它们是 Word 2007 模板

Sub PrintAllFilesInAFolder()
Dim sMyDir As String
Dim sDocName As String
Dim doc As Document
' The path to obtain the files.
sMyDir = "H:WORK RELATEDTESTING MACROS"
sDocName = Dir(sMyDir & "*.doc")
While sDocName <> ""
' Open the file.
Set doc = Documents.Open(FileName:=sMyDir & sDocName)
Call PathFileNameInHeader ' Gets macro to insert field in header
Application.PrintOut FileName:=sMyDir & sDocName 'Prints document in current 
folder
doc.Close wdDoNotSaveChanges
' Get next file name.
sDocName = Dir()
Wend
End Sub

Sub PathFileNameInHeader()
'
' Inserts Path & Filename field in header, then converts field to plain text
'
'
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.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME  * Caps p ", PreserveFormatting:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.Fields.Unlink
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

我已经测试过很多次了,它对我有用。

最新更新