Microsoft Office Forums How to import multiple text file data into word

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: 19,616
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
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
[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: 19,616
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
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
[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: 19,616
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
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
[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


All times are GMT -7. The time now is 10:27 PM.


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