VBA代码,用于获取文件夹中的所有文档,并生成横向视图、更改字体大小、使表格内容适合窗口
我对一些VBA代码有问题。我想获取目录中的所有文件,打开它们,将它们的方向转换为横向,然后将表内容更改为“fittowindow”,将字体大小更改为9,然后保存并关闭该文件,并转到目录中的下一个文件。以下是我的示例代码: 选项显式VBA代码,用于获取文件夹中的所有文档,并生成横向视图、更改字体大小、使表格内容适合窗口,vba,ms-word,Vba,Ms Word,我对一些VBA代码有问题。我想获取目录中的所有文件,打开它们,将它们的方向转换为横向,然后将表内容更改为“fittowindow”,将字体大小更改为9,然后保存并关闭该文件,并转到目录中的下一个文件。以下是我的示例代码: 选项显式 Public Sub tablechanger() Dim FS As New FileSystemObject Dim FSfolder As Folder Dim MyFile As File Dim mydoc As Document Dim sFolderP
Public Sub tablechanger()
Dim FS As New FileSystemObject
Dim FSfolder As Folder
Dim MyFile As File
Dim mydoc As Document
Dim sFolderPath As String
Dim Table As Table
sFolderPath = "//network/location/"
Set FSfolder = FS.GetFolder(sFolderPath)
For Each MyFile In FSfolder.Files
If Right(MyFile.Name, 3) = "rtf" And MyFile.Name <> ThisDocument.Name Then
Set mydoc = Application.Documents.Open(MyFile.Path)
Application.DisplayAlerts = False
ActiveDocument.PageSetup.Orientation = wdOrientLandscape
ActiveDocument.Find.Font.Size = 9
ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitWindow
mydoc.SaveAs FileName:=Left(MyFile.Path, (InStrRev(MyFile.Path, ".", -1, vbTextCompare) - 1)), FileFormat:=wdFormatRTF
mydoc.Close savechanges:=True
Application.DisplayAlerts = True
End If
Next
End
End Sub
公共子表转换器()
将FS设置为新的FileSystemObject
将文件夹设置为文件夹
将MyFile设置为文件
将mydoc设置为文档
将文件夹路径设置为字符串
把桌子调暗
sFolderPath=“//网络/位置/”
设置FSfolder=FS.GetFolder(sFolderPath)
对于FSfolder.Files中的每个MyFile
如果正确(MyFile.Name,3)=“rtf”和MyFile.Name ThisDocument.Name,则
设置mydoc=Application.Documents.Open(MyFile.Path)
Application.DisplayAlerts=False
ActiveDocument.PageSetup.Orientation=WDOrient横向
ActiveDocument.Find.Font.Size=9
ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitWindow
mydoc.SaveAs文件名:=左(MyFile.Path,(InStrRev(MyFile.Path,“.”,-1,vbTextCompare)-1),文件格式:=wdFormatRTF
mydoc.Close savechanges:=True
Application.DisplayAlerts=True
如果结束
下一个
终止
端接头
现在,程序可以打开每个文件,然后停止任何操作。有什么帮助吗?“停止做任何事情”?或者你有错误吗?我在“ActiveDocument.Find.Font.Size=9”处遇到错误 无论如何,在文档对象上找不到。您需要使用
选择
或范围
进行查找。但是,如果您只想更改整个文档的字体,只需选择文本并将font
属性设置为所需的大小:
Selection.Font.Size = 18
另外-如果在没有任何表的文档上运行此操作,请使用复选框包装表操作:
If ActiveDocument.Tables.Count <> 0 Then
ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitWindow
End If
如果ActiveDocument.Tables.Count为0,则
ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitWindow
如果结束
因此,这里:
Public Sub tablechanger()
Const FONT_SIZE_TO_CHANGE_TO As Integer = 18
Dim FS As New FileSystemObject
Dim FSfolder As Folder
Dim MyFile As File
Dim mydoc As Document
Dim sFolderPath As String
Dim Table As Table
sFolderPath = "//network/location/"
Set FSfolder = FS.GetFolder(sFolderPath)
For Each MyFile In FSfolder.Files
If Right(MyFile.Name, 3) = "rtf" And MyFile.Name <> ThisDocument.Name Then
Set mydoc = Application.Documents.Open(MyFile.Path)
Application.DisplayAlerts = False
ActiveDocument.PageSetup.Orientation = wdOrientLandscape
ActiveDocument.Select
Selection.Font.Size = FONT_SIZE_TO_CHANGE_TO
If ActiveDocument.Tables.Count <> 0 Then
ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitWindow
End If
mydoc.SaveAs FileName:=Left(MyFile.Path, (InStrRev(MyFile.Path, ".", -1, vbTextCompare) - 1)), FileFormat:=wdFormatRTF
mydoc.Close savechanges:=True
Application.DisplayAlerts = True
End If
Next
End
End Sub
公共子表转换器()
常量字体大小更改为整数=18
将FS设置为新的FileSystemObject
将文件夹设置为文件夹
将MyFile设置为文件
将mydoc设置为文档
将文件夹路径设置为字符串
把桌子调暗
sFolderPath=“//网络/位置/”
设置FSfolder=FS.GetFolder(sFolderPath)
对于FSfolder.Files中的每个MyFile
如果正确(MyFile.Name,3)=“rtf”和MyFile.Name ThisDocument.Name,则
设置mydoc=Application.Documents.Open(MyFile.Path)
Application.DisplayAlerts=False
ActiveDocument.PageSetup.Orientation=WDOrient横向
ActiveDocument。选择
Selection.Font.Size=Font\u Size\u TO\u CHANGE\u TO
如果ActiveDocument.Tables.Count为0,则
ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitWindow
如果结束
mydoc.SaveAs文件名:=左(MyFile.Path,(InStrRev(MyFile.Path,“.”,-1,vbTextCompare)-1),文件格式:=wdFormatRTF
mydoc.Close savechanges:=True
Application.DisplayAlerts=True
如果结束
下一个
终止
端接头
谢谢!工作得很好。