![]() |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
I have a code that pastes word tables from a word file into the sheet1 in Excel. Then I go to the sheet1 and without changing header names and data's location, I revise the original data to new data. Now, here is my question: how to return those revised data back to the Word document. In other words I want to see all tables into the word file with updated information.
My question in different way: If you have a word document with a few tables in it, then if you go to Excel and run the bellow code from Excel VBA module? You will see how a browser opens to select a word file, then each table will pasted from row 1 and it goes all the way down and there would be 1 empty row between each table-range data. So i can revise all values I want. Now my question is: how to replace word tables with those individual ranges (table ranges) in excel? Note that in the excel we can recognize boundaries of each table since there is a empty row between them. Please let me know if you need more explanation. Just in case: my word file includes a lot of lines, paragraphs, and a few tables. Here is my code that I paste word-tables into the excel sheet (how to return it back?): Code:
Option Explicit Sub ImportWordTable() Dim wdDoc AsObject Dim wdFileName AsVariant Dim tableNo AsInteger'table number in Word Dim iRow AsLong'row index in Excel Dim iCol AsInteger'column index in Excel Dim resultRow AsLong Dim tableStart AsInteger Dim tableTot AsInteger OnErrorResumeNext ActiveSheet.Range("A:AZ").ClearContents wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _ "Browse for file containing table to be imported") If wdFileName = FalseThenExitSub'(user cancelled import file browser) Set wdDoc = GetObject(wdFileName) 'open Word file With wdDoc tableNo = wdDoc.tables.Count tableTot = wdDoc.tables.Count If tableNo = 0Then MsgBox "This document contains no tables", _ vbExclamation, "Import Word Table" ElseIf tableNo > 1Then tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _ "Enter the table to start from", "Import Word Table", "1") EndIf resultRow = 1 For tableStart = 1To tableTot With .tables(tableStart) 'copy cell contents from Word table cells to Excel cells For iRow = 1To .Rows.Count For iCol = 1To .Columns.Count Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text) Next iCol resultRow = resultRow + 1 Next iRow EndWith resultRow = resultRow + 1 Next tableStart EndWith EndSub Last edited by modiria50989; 08-31-2017 at 04:06 PM. |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
AustinBrister | Word VBA | 19 | 08-22-2016 05:18 PM |
![]() |
francesco | Word VBA | 1 | 01-07-2016 04:08 AM |
![]() |
bill3kradio | Mail Merge | 7 | 11-17-2014 01:06 PM |
![]() |
Legaly | Excel | 2 | 05-23-2014 08:30 AM |
![]() |
dineshtgs | Word Tables | 1 | 04-07-2011 01:27 AM |