I frequently need to import the content from hundreds of files into a single document. I would like to have the filename included as text above each file's content in the final document. Is there a way to do this by adding it into the code I use (listed below):
Code:
Sub Import_Text()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document, rtfFile As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.rtf", vbNormal)
Set wdDoc = ActiveDocument
While strFile <> ""
Set rtfFile = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False, ConfirmConversions:=False)
wdDoc.Range.InsertAfter rtfFile.Range.Text & vbCr
rtfFile.Close SaveChanges:=True
strFile = Dir()
Wend
Set rtfFile = Nothing: Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function