Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2012, 05:11 AM
mizankabir mizankabir is offline How to import multiple text file data into word Windows XP How to import multiple text file data into word Office 2007
Novice
How to import multiple text file data into word
 
Join Date: Jan 2012
Posts: 1
mizankabir is on a distinguished road
Default How to import multiple text file data into word

I am using windows xp and I want to know if there is any way like using a macro or any other way,I can import multiple text file data into word.I want a system like that I will select 50-60 text file and execute the operation that will bring the data of these files to the word file.



Thanks for your time.
Reply With Quote
  #2  
Old 01-02-2012, 03:22 PM
macropod's Avatar
macropod macropod is offline How to import multiple text file data into word Windows 7 64bit How to import multiple text file data into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,953
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-27-2012, 08:59 AM
cm770011 cm770011 is offline How to import multiple text file data into word Windows 2K How to import multiple text file data into word Office 2007
Novice
 
Join Date: Jun 2012
Posts: 1
cm770011 is on a distinguished road
Default to retrieve from multiple folders

hi,

really could use someone's help:
how can I adjust this code to retrieve from multiple folders?
IE there's one folder I can just click and this code will go into each one individually to retrieve the text from all of the docs in the subfolders.

thanks alot
Reply With Quote
  #4  
Old 06-27-2012, 06:39 PM
macropod's Avatar
macropod macropod is offline How to import multiple text file data into word Windows 7 64bit How to import multiple text file data into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,953
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Recursively processing sub-folders would require considerably more code - more than what's already there. There is no 'simple' change for this.

Unless you have a lot of folders that need frequent processing, it would take less time for you to process them manually than it would for me to develop the code. If you do have a lot of folders that need frequent processing, a web search for vba recursive folder processing will turn up links showing what's involved.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-28-2015, 09:26 AM
VincentBrown VincentBrown is offline How to import multiple text file data into word Windows 7 64bit How to import multiple text file data into word Office 2013
Novice
 
Join Date: May 2015
Posts: 2
VincentBrown is on a distinguished road
Default

Hi MacroPod,

I tried using your code but got Compile error: User-defined type not defined. I think it was referring to wdDoc As Document. I am trying to do something similar; I'm trying to write a code that will take a folder of .txt files and import it into a single word document, with the content of each .txt file having its own page. So far I have this:

Code:
Option Explicit
Sub AllFilesInFolder()
    Dim objDoc As Object
    Dim objWord As Object
    Dim myFolder As String
    Dim myFile As String

    ' Open the file dialog
    myFolder = Application.FileDialog(msoFileDialogFolderPicker)

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show

        If .SelectedItems.Count > 0 Then
            myFolder = .SelectedItems(1)
        End If

        myFile = Dir(myFolder & "\*.txt")
        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True
        
        Do While myFile <> ""
            Set objDoc = objWord.Documents.Open(FileName:=myFolder & "\" & myFile)
            'Call MergeDocs
            'Call MergeDocs
            myFile = Dir
            
        Loop
    'Saves document
    'objDoc.SaveAs ("P:\ImportedDescriptions.doc")
    
    End With

    Set objDoc = Nothing
    Set objWord = Nothing

End Sub

Public Sub MergeDocs()
    Dim Rng As Range
    Dim MainDoc As Document
    Dim strFile As String
    Const strFolder = "C:\Descriptions2\" 'change to suit
    Set MainDoc = Documents.Add
    strFile = Dir$(strFolder & "*.docx") ' can change to .docx
    Do Until strFile = ""
        Set Rng = MainDoc.Range
        Rng.Collapse wdCollapseEnd
        Rng.InsertFile strFolder & strFile
        strFile = Dir$()
    Loop
End Sub

Sub ImportIntoOne()
    Dim objDoc As Object
    Dim objWord As Object
    Dim myFolder As String
    Dim myFile As String

    ' Open the file dialog
    myFolder = Application.FileDialog(msoFileDialogFolderPicker)

    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show

        If .SelectedItems.Count > 0 Then
            myFolder = .SelectedItems(1)
        End If

        myFile = Dir(myFolder & "\*.txt")
        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True
        
        Do While myFile <> ""
            Set objDoc = objWord.Documents.Open(FileName:=myFolder & "\" & myFile)
            myFile = Dir
            
        Loop
    
    End With

    Set objDoc = Nothing
    Set objWord = Nothing

End Function
This code opens each .txt file as a word document and then closes them. Do you know how I can get it into one document and format it correctly? Any help would be appreciated. Thanks!
Reply With Quote
  #6  
Old 06-05-2015, 05:45 AM
macropod's Avatar
macropod macropod is offline How to import multiple text file data into word Windows 7 64bit How to import multiple text file data into word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,953
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi VincentBrown,

I've been away for 3 1/2 months, hence the delay in replying.

Is there a reason for not using the code as supplied in post #2? Assuming you're running the macro in Word, at most, you might need to change:
wdDoc.Range.InsertAfter txtFile.Range.Text & vbCr
to:
wdDoc.Range.InsertAfter txtFile.Range.Text & vbCr & Chr(12)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 09-14-2023, 04:59 AM
knpaddac knpaddac is offline How to import multiple text file data into word Windows 10 How to import multiple text file data into word 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
  #8  
Old 09-15-2023, 10:27 PM
macropod's Avatar
macropod macropod is offline How to import multiple text file data into word Windows 10 How to import multiple text file data into word Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,953
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could change:
wdDoc.Range.InsertAfter txtFile.Range.Text & vbCr
to:
wdDoc.Range.InsertAfter txtFile.Name & vbCr & txtFile.Range.Text & vbCr
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to import multiple text file data into word How to copy automatically data from Excel file to Word file? fuchsd Word 6 10-25-2011 05:52 AM
Import formatted text from Word into PowerPoint parboy PowerPoint 0 07-06-2011 08:52 AM
How to import multiple text file data into word extracting data from Table to text file Anirudh_Dsp Word Tables 1 05-23-2010 07:48 AM
How to import multiple text file data into word Import Text from File - Line Spacing marshalx Word 2 10-28-2009 02:37 AM
import multiple categories stormin.norm Outlook 0 01-30-2006 01:36 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:41 AM.


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