View Single Post
 
Old 09-14-2023, 04:59 AM
knpaddac knpaddac is offline Windows 10 Office 2016
Novice
 
Join Date: Jan 2018
Posts: 13
knpaddac is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Hi mizankabir,

the following macro allows you to simply select a folder and have Word import the contents of all txt files in that folder.
Code:
Sub Import_Text()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document, txtFile As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.txt", vbNormal)
Set wdDoc = ActiveDocument
While strFile <> ""
  Set txtFile = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False, ConfirmConversions:=False)
  wdDoc.Range.InsertAfter txtFile.Range.Text & vbCr
  txtFile.Close SaveChanges:=True
  strFile = Dir()
Wend
Set txtFile = 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
Is there a way to add to this code so that the filenames would be included in the content that is put into the document to which all the content is being put? So that it would look something like:

'DOC1-FILENAME'
"DOC1-CONTENT"

'DOC2-FILENAME'
"DOC2-CONTENT"

'DOC3-FILENAME'
"DOC3-CONTENT"

'DOC4-FILENAME'
"DOC4-CONTENT"

And on and on based on the folder chosen
Reply With Quote