txt文件的文件夹导入到Word文档中

txt文件的文件夹导入到Word文档中

本文介绍了将.txt文件的文件夹导入到Word文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于将.txt文件的文件夹导入到excel中,我发现很多,但是对于将.txt文件导入到word中,却发现不多.我正在尝试让我的宏打开特定文件夹中的所有.txt文件,并将它们导入到单个Word文档中,每个.txt文件都有自己的页面.这是我到目前为止(在网上找到的)代码:

I've found a lot on importing folder of .txt files into excel, but not many on importing .txt files into word. I'm trying to get my macro to open all .txt files in a specific folder and import them into a single word document, with each .txt file having its own page. This is the code I have so far (that I found online):

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

推荐答案

以下是一些入门指南

编辑,这应允许您在一个文档中打开所有txt文件并将其保存

Edit this should allow you to open all txt files in one document and save it

Option Explicit
Sub AllFilesInFolder()
    Dim myFolder    As String
    Dim myFile      As String
    Dim wdDoc       As Document
    Dim txtFiles    As Document

    Application.ScreenUpdating = False

    myFolder = openFolder

    If myFolder = "" Then Exit Sub

    myFile = Dir(myFolder & "\*.txt", vbNormal)
    Set wdDoc = ActiveDocument

    While myFile <> ""
        Set txtFiles = Documents.Open(FileName:=myFolder & "\" & myFile, AddToRecentFiles:=False, Visible:=False, ConfirmConversions:=False)
        wdDoc.Range.InsertAfter txtFiles.Range.Text & vbCr
        txtFiles.Close SaveChanges:=True
    myFile = Dir()
    Wend

    Set txtFiles = Nothing
    Set wdDoc = Nothing

    Application.ScreenUpdating = True
End Sub
Function openFolder() As String

    Dim oFolder     As Object

    openFolder = ""

    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
        If (Not oFolder Is Nothing) Then openFolder = oFolder.Items.Item.Path

    Set oFolder = Nothing
End Function

这篇关于将.txt文件的文件夹导入到Word文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:42