![]() |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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] |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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] |
|
#5
|
|||
|
|||
|
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
|
|
#6
|
||||
|
||||
|
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] |
|
#7
|
|||
|
|||
|
Quote:
'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 |
|
#8
|
||||
|
||||
|
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] |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
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 |
extracting data from Table to text file
|
Anirudh_Dsp | Word Tables | 1 | 05-23-2010 07:48 AM |
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 |