View Single Post
 
Old 05-28-2015, 09:26 AM
VincentBrown VincentBrown is offline Windows 7 64bit 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