Excel VBA code to get Word document data

jilualx

New Member
Joined
Jul 18, 2010
Messages
14
Hi,
I am trying this from long. I need a help from all experts out there.
I have a word document with contents and tables. I am able to get the table numbers and also able to extract the data in table. But I want to get the heading of the table also which is the line above each table.
Is there any way to understand the line number of the table and get the data in the line above?
I want to do all these from Excel.
Please help.Hi,
I am trying this from long. I need a help from all experts out there.
I have a word document with contents and tables. I am able to get the table numbers and also able to extract the data in table. But I want to get the heading of the table also which is the line above each table.
Is there any way to understand the line number of the table and get the data in the line above?
I want to do all these from Excel.
Please help.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
post the code you have tried
 
Upvote 0
Sub ImportWordTables()




Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer
Dim iTable As Integer
Dim iRow As Long
Dim iCol As Integer


wdFileName = Application.GetOpenFilename("Word files (*.doc*),*.doc*", , _
"Browse for file containing table to be imported")


If wdFileName = False Then Exit Sub '(user cancelled import file browser)


Set wdDoc = GetObject(wdFileName) 'open Word file


With wdDoc
TableNo = wdDoc.tables.Count
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")
End If
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With
Set wdDoc = Nothing
End Sub
 
Upvote 0
Hi
I want to get the heading of the table also which is the line above each table.
If you have not any empty paragraph between a heading and a table you can try with a such function
Code:
Public Function GetHeadingText(ByVal wdDoc As Object, ByVal TableNum As Long) As String
    Dim firstCharNum As Long
    firstCharNum = wdDoc.Tables(TableNum).Range.Characters(1).Start
    GetHeadingText = wdDoc.Characters(firstCharNum - 2).Paragraphs(1).Range.Text
End Function
Regards,
 
Upvote 0
Thanks Anvg!!!

It worked. You are brilliant.

I need one more help. If you could please.

In word table to move one selected row down we could use
Selection.Range.Relocate wdRelocateDown

And, to move it up we could use
Selection.Range.Relocate wdRelocateUp

Is there any alternative for this to be used from excel. I tried using the same from excel its not working.

Regards,
 
Upvote 0
Is there any alternative for this to be used from excel
Yes
Code:
Public Sub RelocateUp(ByVal RowId As Long)
    ActiveSheet.Cells(RowId, 1).EntireRow.Cut
    ActiveSheet.Cells(RowId - 1, 1).EntireRow.Insert Shift:=xlDown
End Sub

Public Sub RelocateDown(ByVal RowId As Long)
    ActiveSheet.Cells(RowId, 1).EntireRow.Cut
    ActiveSheet.Cells(RowId + 2, 1).EntireRow.Insert Shift:=xlDown
End Sub
Regards,
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,813
Members
449,469
Latest member
Kingwi11y

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