+ Reply to Thread
Results 1 to 3 of 3

Loop without Selecting Files: VBA Copy different Word files in a folder to Excel

  1. #1
    Registered User
    Join Date
    01-16-2023
    Location
    Frankfurt, Germany
    MS-Off Ver
    2016
    Posts
    3

    Question Loop without Selecting Files: VBA Copy different Word files in a folder to Excel

    Hi,

    I'm a VBA newbie. I've failed to solve my problem on my own. Maybe you have an idea?
    I'm trying to loop through all word files in a folder and copy the entire content to a new Excel worksheet in my Excel workbook.
    I managed to get the copying done, but if I loop through the different files, I have to open them by clicking on each file. Is there a way to change my code so it opens them without clicking on it first?

    My (pretty ugly) code:

    Sub LoopThroughFolder()

    Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
    Dim Rws As Long, Rng As Range
    Dim WordApp As Object, objDoc As Object
    Dim wdFileName As Variant

    Set Wb = ThisWorkbook
    'change the address to suite
    MyDir = "C:akr987DesktopTest Word"
    MyFile = Dir(MyDir & "*.docx")
    ChDir MyDir
    Application.ScreenUpdating = 0
    Application.DisplayAlerts = 0
    Do While MyFile <> ""

    wdFileName = Application.GetOpenFilename("Word Documents, *.doc*") 'MultiSelect:=True
    Set WordApp = CreateObject("Word.Application")
    Set objDoc = WordApp.Documents.Open(wdFileName)
    WordApp.Visible = False

    'With
    WordApp.Selection.WholeStory
    WordApp.Selection.Copy
    ThisWorkbook.Sheets(Sheets.Count).Range("A1").PasteSpecial xlPasteValues

    ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)


    objDoc.Close False
    WordApp.Quit SaveChanges:=wdDoNotSaveChanges

    Set WordApp = Nothing
    Set objDoc = Nothing


    'End With
    Application.DisplayAlerts = 1
    MyFile = Dir()

    Loop

    End Sub

  2. #2
    Registered User
    Join Date
    01-16-2023
    Location
    Frankfurt, Germany
    MS-Off Ver
    2016
    Posts
    3

    Re: Loop without Selecting Files: VBA Copy different Word files in a folder to Excel

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-16-2023
    Location
    Frankfurt, Germany
    MS-Off Ver
    2016
    Posts
    3

    Re: Loop without Selecting Files: VBA Copy different Word files in a folder to Excel

    Found the answer: in case someone else needs this kind of code:


    Sub LoopThroughFolder()

    Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
    Dim Rws As Long, Rng As Range
    Dim WordApp As Object, objDoc As Object
    Dim wdFileName As Variant

    Set Wb = ThisWorkbook
    'change the address to suite
    MyDir = "C:akr987DesktopTest Word/"
    MyFile = Dir(MyDir & "*.docx")
    ChDir MyDir
    Application.ScreenUpdating = 0
    Application.DisplayAlerts = 0
    Do While MyFile <> ""

    wdFileName = MyDir & MyFile
    Set WordApp = CreateObject("Word.Application")
    Set objDoc = WordApp.Documents.Open(wdFileName)
    WordApp.Visible = False

    'With
    WordApp.Selection.WholeStory
    WordApp.Selection.Copy
    ThisWorkbook.Sheets(Sheets.Count).Range("A1").PasteSpecial xlPasteValues

    ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)


    objDoc.Close False
    WordApp.Quit SaveChanges:=wdDoNotSaveChanges

    Set WordApp = Nothing
    Set objDoc = Nothing


    'End With
    Application.DisplayAlerts = 1
    MyFile = Dir()

    Loop

    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Loop through all files in a folder and rename the files considering a pattern
    By excelactuary in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-22-2022, 10:57 AM
  2. [SOLVED] Excel VBA code for selecting a folder then search & open specified files in that folder
    By Excelrookie_1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2021, 03:09 AM
  3. [SOLVED] Macro to copy .xlsm files in folder and sub-folder to foldr C:\old pull files
    By Howardc1001 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-15-2021, 09:55 PM
  4. [SOLVED] Loop thru files in folder and copy things
    By rpinxt in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-18-2019, 09:25 AM
  5. Loop macro for all Excel files in folder
    By akenney151 in forum Excel Programming / VBA / Macros
    Replies: 42
    Last Post: 02-08-2018, 02:57 PM
  6. Replies: 12
    Last Post: 03-09-2015, 05:52 PM
  7. Loop to open excel files in a folder
    By elmarko123 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-02-2010, 12:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1