我是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
我尝试修改代码,但它只复制文件夹中的第一个文件,无法循环其他文件。