【发布时间】:2015-08-10 04:03:32
【问题描述】:
我发现很多关于将 .txt 文件的文件夹导入到 excel 中,但在将 .txt 文件导入到 word 中的情况并不多。我试图让我的宏打开特定文件夹中的所有 .txt 文件并将它们导入到单个 word 文档中,每个 .txt 文件都有自己的页面。这是我到目前为止的代码(我在网上找到的):
Sub AllFilesInFolder()
Dim myFolder As String, myFile As String
myFolder = Application.FileDialog(msoFileDialogFolderPicker)
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then
myFolder = .SelectedItems(1)
End If
End With
myFile = Dir(myFolder & "\*.txt") '
Do While myFile <> ""
Open myFolder & "\" & myFile For Input As #1
'Copy & Paste Macro?
myFile = Dir
Loop
End Sub
【问题讨论】:
-
很好地找到了代码。现在告诉我们您在代码中遇到了哪些问题以及您尝试对其进行哪些更改以使其正常工作:)
-
Word 有一个 Insert ► Text ► Object ► Text from File 命令更适合从文件中插入文本。如果您录制自己带入一两个 TXT 文件,其余的应该很明显。如果遇到问题,请回来。
标签: vba automation ms-word