Error in copying Word tables into a new worksheet

JohanPoulsen

New Member
Joined
Jan 29, 2018
Messages
4
I am working with a macro, where I select a folder, then the macro is to open 4 different word files, stored in subfolders, copy a table in each into a new worksheet in Excel.

It works for the first Word file, then it seems to skip the next two, after which it pastes the table from the last file into "Sheet1" intstead of the new worksheet.

This is my code (warning: it is not pretty):

Code:
Sub SelectFolderPaste()
    Dim diaFolder As FileDialog
    Dim Dansk As String
    Dim Historie As String
    Dim Matematik As String
    Dim NF As String
    Dim Path As String
    Dim vFiles
    Dim oWord As Object
    Dim iFile As Integer
    Dim R As Range


    Dansk = "\Dansk\Formativ feedbackskema dansk.docx"
    Historie = "\Historie\Formativ feedbackskema historie.docx"
    NF = "\Nf\Formativ feedbackskema nf.docx"
    Matematik = "\Matematik\Formativ feedbackskema matematik.docx"
    
    ' Open the file dialog
    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show


    If diaFolder = "False" Or diaFolder = "" Then
        
        Exit Sub
    
    Else
    
    Path = diaFolder.SelectedItems(1)
    
    vFiles = Array(Path & Dansk, Path & Historie, Path & Matematik, Path & NF)


    Set oWord = CreateObject("Word.Application")
    oWord.Visible = True
    Set R = Worksheets.Add.Range("A1")


        oWord.Documents.Open vFiles(0)
        oWord.ActiveDocument.Tables(1).Select
        oWord.Selection.Copy
        ActiveSheet.Paste R
        Set R = Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1)
        oWord.ActiveDocument.Close False
        
        oWord.Documents.Open vFiles(1)
        oWord.ActiveDocument.Tables(1).Select
        oWord.Selection.Copy
        ActiveSheet.Paste R
        Set R = Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1)
        oWord.ActiveDocument.Close False


        oWord.Documents.Open vFiles(2)
        oWord.ActiveDocument.Tables(1).Select
        oWord.Selection.Copy
        ActiveSheet.Paste R
        Set R = Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1)
        oWord.ActiveDocument.Close False
        
        oWord.Documents.Open vFiles(3)
        oWord.ActiveDocument.Tables(1).Select
        oWord.Selection.Copy
        ActiveSheet.Paste R
        Set R = Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1)
        oWord.ActiveDocument.Close False


    oWord.Quit
    Set oWord = Nothing
    ActiveSheet.Columns.AutoFit


    Set diaFolder = Nothing
    
    End If
    
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,223,774
Messages
6,174,453
Members
452,565
Latest member
curtoliver68

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top