#1
|
|||
|
|||
Add filename to imported text
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 |
#2
|
||||
|
||||
In the macro change the line of code to this:
Code:
wdDoc.Range.InsertAfter rtfFile & vbCr & rtfFile.Range.Text & vbCr |
#3
|
|||
|
|||
Hi, try replacing
wdDoc.range.InsertAfter rtfFile.range.text & vbCr with: wdDoc.range.InsertAfter strFile & vbCr & rtfFile.range.text & vbCr |
#4
|
|||
|
|||
This did exactly what I needed. thanks
|
#5
|
|||
|
|||
had to change the single instance of 'strFile' to 'rtfFile' but that did the trick.
|
#6
|
||||
|
||||
Thanks for the positive feedback , glad we were able to help.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
formatting of imported text | philh | Word | 4 | 03-23-2020 02:56 PM |
Convert Imported picture to text | Baldeagle | Excel Programming | 2 | 03-23-2019 01:51 PM |
filename field not displaying correct filename when that name starts with # | plrsmith | Word | 1 | 07-06-2018 03:10 AM |
How to merge text boxes in a large imported document | boatfly | Word | 3 | 04-20-2016 06:15 AM |
Save Filename using Document Text | Knawl | Word | 11 | 10-10-2011 03:00 AM |