Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 导入多个RTF文件';将数据保存到单独的工作表中_Excel_Vba_Rtf - Fatal编程技术网

Excel 导入多个RTF文件';将数据保存到单独的工作表中

Excel 导入多个RTF文件';将数据保存到单独的工作表中,excel,vba,rtf,Excel,Vba,Rtf,我是Excel VBA的新手 我在一个文件夹中有多个富文本格式文件(.rtf)。我希望在Excel中打开.rtf文件,每个文件由一个工作表表示 Sub Try() Dim WordApp As Object Dim FSO As New FileSystemObject Dim Fldr As Folder Dim Fl As file Dim WkSht As Worksheet Dim

我是Excel VBA的新手

我在一个文件夹中有多个富文本格式文件(.rtf)。我希望在Excel中打开.rtf文件,每个文件由一个工作表表示

Sub Try()
    Dim WordApp As Object
    Dim FSO         As New FileSystemObject
    Dim Fldr        As Folder
    Dim Fl          As file
    Dim WkSht       As Worksheet
    Dim StrName     As String
    Dim WkBk_Tmp    As Workbook
    Dim WkSht_Tmp   As Worksheet

    Set Fldr = FSO.GetFolder("C:\Users\NHWD78\Desktop\Report\Harmonic and Flicker") 'folder path
    Set WordApp = CreateObject("Word.Application")  'run in word application

    For Each Fl In Fldr.Files   'loop to search for rft files
        If Right(UCase(Fl.Name), 4) = ".RTF" Then

            StrName = Left(Fl.Name, Len(Fl.Name) - 4)
            Set WkSht = Worksheets.Add  'add worksheet
            WkSht.Name = StrName    'worksheet name equals to file name

            With WordApp    'open file and copy content
                .Documents.Open FileName:=(Fl.Path)
                .ActiveDocument.Select
                .Selection.Copy

                ActiveSheet.Range("A1").Select 'paste content
                ActiveSheet.Paste
                WordApp.Quit

                Columns.AutoFit
                Set WordApp = Nothing

            End With
        End If
    Next
End Sub
我试图修改代码,但它只复制文件夹中的第一个文件,无法循环其他文件