Copy data from Word to Excel using VBA

xPukwe

New Member
Joined
Feb 25, 2020
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi everybody,
---

I want to copy data from a Word document containing tables. These tables could contain paragraphs of words or images.

I used the code below found in a video of youtuber "Dinesh Kumar Takyar",

This code dosen't copy images and i have a problem with the formatting of the row height and column width as it just copies it in one continuing line of text,

I want to be able to copy images and to format the row height and column width,

VBA Code:
Sub importTableDataWord()
    'We declare object variables for Word Application and document
    Dim WdApp As Object, wddoc As Object
    'Declare a string variable to access our Word document
    Dim strDocName As String
    'Error handling
    On Error Resume Next
    'Activate Word it is already open
    Set WdApp = GetObject(, "Word.Application")
    If Err.Number = 429 Then
        Err.Clear
        'Create a Word application if Word is not already open
        Set WdApp = CreateObject("Word.Application")
    End If
    WdApp.Visible = True
    
    strDocName = "C:\Users\mtouahri\Desktop\TestWord.docx" ' ### change the name of the document
    'Check relevant directory for relevant document
    'If not found then inform the user and close program
    If Dir(strDocName) = "" Then
        MsgBox "The file " & strDocName & vbCrLf & "was not found in the folder path" & vbCrLf & _
        "C:\Users\mtouahri\.", vbExclamation, "Sorry, that document name does not exist." '###
        Exit Sub
    End If
    
    WdApp.Activate
    
    Set wddoc = WdApp.Documents(strDocName)
    
    If wddoc Is Nothing Then Set wddoc = WdApp.Documents.Open(strDocName)
    wddoc.Activate
    'define variables to access the tables in the word document
    Dim Tble As Integer
    Dim rowWd As Long
    Dim colWd As Integer
    Dim x As Long, y As Long
    x = 1
    y = 1
    
    With wddoc
        Tble = wddoc.Tables.Count
        If Tble = 0 Then
        
            MsgBox "No Tables found in the Word document", vbExclamation, "No Tables to Import"
            Exit Sub
        End If
        'start the looping process to access tables and their rows, columns
        For i = 1 To Tble
            With .Tables(i)
                For rowWd = 1 To .Rows.Count
                    For colWd = 1 To .Columns.Count
                        Cells(x, y) = WorksheetFunction.Clean(.cell(rowWd, colWd).Range.Text)
                        'Access next column
                        y = y + 1
                    Next colWd
                'go to next row and start from column 1
                y = 1
                x = x + 1
        
                Next rowWd
             End With
        
        Next
    End With
    'we don’t need to save the word document
    wddoc.Close Savechanges:=False
    'we quit Word
    WdApp.Quit
    'We finally release system memory allocated to the 2 object variables
    Set wddoc = Nothing
    Set WdApp = Nothing
End Sub


xPukwe,
 

Attachments

  • Capture.PNG
    Capture.PNG
    167.3 KB · Views: 6

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,136,640
Messages
5,676,942
Members
419,660
Latest member
Fred Cailloux

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
Top