Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
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
 



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 02:04 PM.


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