word vba 打印文件夹选中的部分文档,主要没有选中的就不打印

动态 未结 0 129
小小草
小小草 LV1 2023年9月23日 23:40 发表
以下是 VBA 代码示例,用于打印文件夹中选定的部分 Word 文档: ``` Sub PrintSelectedDocuments() ' 获取选中的文件路径 Dim folderPath As String folderPath = Application.FileDialog(msoFileDialogFolderPicker).Show ' 如果用户取消选择文件夹,则退出过程 If folderPath = "" Then Exit Sub ' 遍历文件夹中的所有文件 Dim fileName As String Dim filePath As String Dim doc As Document For Each fileName In FileSystem.GetFiles(folderPath) filePath = folderPath & "\" & fileName ' 检查文件是否为 Word 文档 If Right(filePath, 4) = ".doc" Or Right(filePath, 5) = ".docx" Then ' 打开文档并检查是否选中 Set doc = Documents.Open(filePath) If Selection.Range.InRange(doc.Range) Then ' 如果选中,则打印文档 doc.PrintOut End If doc.Close End If Next fileName End Sub ``` 在这个示例中,我们首先创建了一个名为 `PrintSelectedDocuments` 的过程,用于打印选定的文件夹中的 Word 文档。我们使用 `Application.FileDialog` 方法显示文件夹选择对话框,并使用 `FileSystem.GetFiles` 方法遍历文件夹中的所有文件。 对于每个文件,我们检查其扩展名是否为 `.doc` 或 `.docx`,以确定它是否为 Word 文档。如果是 Word 文档,则使用 `Documents.Open` 方法打开文档,并使用 `Selection.Range.InRange`
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复