簡體   English   中英

將.txt文件的文件夾導入到Word文檔中

[英]import folder of .txt files into word document

我在將.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

使用命令提示符(cmd.exe)和以下命令將所有文本文件復制到一個文件中:

copy *.txt NewFile.txt

然后,用word打開此文件,並修改您想要查看文本的方式。

這是一些讓您入門的東西

Word 2010

編輯此選項應允許您在一個文檔中打開所有txt文件並保存

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM