Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-14-2023, 08:28 AM
knpaddac knpaddac is offline Add filename to imported text Windows 10 Add filename to imported text Office 2016
Novice
Add filename to imported text
 
Join Date: Jan 2018
Posts: 13
knpaddac is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 09-14-2023, 10:38 AM
rollis13's Avatar
rollis13 rollis13 is offline Add filename to imported text Windows 11 Add filename to imported text Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

In the macro change the line of code to this:
Code:
wdDoc.Range.InsertAfter rtfFile & vbCr & rtfFile.Range.Text & vbCr
Reply With Quote
  #3  
Old 09-14-2023, 10:40 AM
vivka vivka is offline Add filename to imported text Windows 7 64bit Add filename to imported text Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 223
vivka is on a distinguished road
Default

Hi, try replacing
wdDoc.range.InsertAfter rtfFile.range.text & vbCr
with:
wdDoc.range.InsertAfter strFile & vbCr & rtfFile.range.text & vbCr
Reply With Quote
  #4  
Old 09-14-2023, 01:54 PM
knpaddac knpaddac is offline Add filename to imported text Windows 10 Add filename to imported text Office 2016
Novice
Add filename to imported text
 
Join Date: Jan 2018
Posts: 13
knpaddac is on a distinguished road
Default

This did exactly what I needed. thanks



Quote:
Originally Posted by rollis13 View Post
In the macro change the line of code to this:
Code:
wdDoc.Range.InsertAfter rtfFile & vbCr & rtfFile.Range.Text & vbCr
Reply With Quote
  #5  
Old 09-14-2023, 01:56 PM
knpaddac knpaddac is offline Add filename to imported text Windows 10 Add filename to imported text Office 2016
Novice
Add filename to imported text
 
Join Date: Jan 2018
Posts: 13
knpaddac is on a distinguished road
Default

had to change the single instance of 'strFile' to 'rtfFile' but that did the trick.



Quote:
Originally Posted by vivka View Post
Hi, try replacing
wdDoc.range.InsertAfter rtfFile.range.text & vbCr
with:
wdDoc.range.InsertAfter strFile & vbCr & rtfFile.range.text & vbCr
Reply With Quote
  #6  
Old 09-14-2023, 11:47 PM
rollis13's Avatar
rollis13 rollis13 is offline Add filename to imported text Windows 11 Add filename to imported text Office 2016
Competent Performer
 
Join Date: Jan 2021
Location: Cordenons
Posts: 140
rollis13 will become famous soon enough
Default

Thanks for the positive feedback , glad we were able to help.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add filename to imported text formatting of imported text philh Word 4 03-23-2020 02:56 PM
Add filename to imported text 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
Add filename to imported text Save Filename using Document Text Knawl Word 11 10-10-2011 03:00 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:06 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft