PDA

View Full Version : convert table in word



dikdikdik
12-15-2008, 08:04 PM
Hi every body. In my project, I want to convert word document to plain text, display the plain text in website, then search some word for few function. So that I use VBA code to convert table to text (to maintain table form for display purpose). But I don't need to search data in tables.My idea is to mark begin and end of tables by some symbol, it help me recognize tables and skip it when I search in plain text. Does anyone help me about the code to recognize begin point and end point of tables? I find it but no good result :(

alanc
12-15-2008, 09:03 PM
Does anyone help me about the code to recognize begin point and end point of tables?

Does this help?


Sub FindNextTable()
Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
End Sub

dikdikdik
12-16-2008, 03:04 AM
Thank you very much. With this code, I have found the beginning of table and add special symbol to mark it. But to mark the end of each table, how I do it ?

Ps : Depend on your idea, I find something like Selection.Collapse. I will try to test it.

alanc
12-16-2008, 09:02 AM
Perhaps there are more elegant ways to do this, but this seems to work...


Sub TableMarkAll()
' Go to start of document
Selection.HomeKey Unit:=wdStory
For Each aTable In ActiveDocument.Tables
With Selection
.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1
.MoveLeft Unit:=wdCharacter, Count:=1
.TypeText "{START}"
.EndOf Unit:=wdTable
.MoveRight Unit:=wdCharacter, Count:=1
.TypeText "{END}"
End With
Next aTable
End Sub

fumei
12-16-2008, 12:35 PM
It would be better to simply make a table object, and then use it. That is, if you were doing something with the table. However, if I understand correctly, you are converting the tables to text. In which case...there is no table.

Further, if I understand correctly, AFTER you convert the table to text, you are wanting to search the document text but NOT the text that was in the table.

"But I don't need to search data in tables.My idea is to mark begin and end of tables by some symbol, it help me recognize tables and skip it when I search in plain text. "

If you have converted the tables to text, there is no table.

Why is this needed? Searching can be very fast. Why is NOT searching in the text (that was in the table) significant?

In any case, here is an alternative to alanc's code, but does not use Selection. "{Start}" and {End} are placed before and after each table, and then the tables are converted to text.

Demo attached. Click "Change Tables" on the top toolbar.
Sub ChangeTables()
Dim r As Range
Dim oTable As Table
For Each oTable In ActiveDocument.Tables
Set r = ActiveDocument.Range _
(Start:=oTable.Range.Start - 1, _
End:=oTable.Range.Start - 1)
r.Text = "{Start}"
Set r = ActiveDocument.Range _
(Start:=oTable.Range.End, _
End:=oTable.Range.End)
r.Text = "{End}"
oTable.ConvertToText Separator:=wdSeparateByParagraphs
Next
End Sub

dikdikdik
12-16-2008, 07:08 PM
Thank all of you. Because I'm not good in English, so that I explain my purpose very hard. But your code do the same thing I want. Firstly, I read word documents and save it to plain text. With plain text, I use to display in web. Secondly, I search something in plain text. Depend on my search purpose, I only need to search in some parts of plain text which have no tables. You understand right my idea :)

I use Ruby language to write code. Ruby can convert doc -> txt easily but loss table format. So that, firstly I use VBA to convert word document which have table -> word document which have no table but maintain table form ( I use ConvertToText Separator:=wdSeparateByTabs). Then use Ruby code to convert it -> plain text. Because my search don't care about all thing in tables, so that I want to mark the beginning and end of tables. When I search in text file, if I meet structure { "start table" "..." "end table"}, I will don't search in "..."

Finally thank all of you very much. I will fix and convert your code to Ruby code and use it.

dikdikdik
12-16-2008, 10:01 PM
I have finished the code and I share it for anyone need them :)

VBA code

Sub ChangeTables()

Dim oTable As Table
Dim startpoint As Integer
Dim endpoint As Integer

For Each oTable In ActiveDocument.Tables

startpoint = oTable.Range.Start - 1
ActiveDocument.Range(startpoint , startpoint ).InsertAfter ("Start table")

endpoint = oTable.Range.End
ActiveDocument.Range(endpoint , endpoint ).InsertAfter ("End table")

oTable.ConvertToText (wdSeparateByTabs)
Next

End Sub

Ruby code

require 'win32ole'
word = WIN32OLE.new('Word.Application')
word.Visible = true

document = word.Documents.Open('abc.doc', 'ReadOnly' => true)

for table in document.Tables
startpoint = table.Range.Start - 1
document.Range(startpoint, startpoint).InsertAfter("\nStart table")
endpoint = table.Range.End
document.Range(endpoint, endpoint).InsertAfter("End table\n")
table.ConvertToText(1)
end

document.SaveAs('abc.txt', 2)
document.Close

Thank for your help

fumei
12-17-2008, 12:31 PM
I do not understand. That still does not exclude the text that was in the tables.

"When I search in text file, if I meet structure { "start table" "..." "end table"}, I will don't search in "..."

How are you going to do that?

dikdikdik
12-17-2008, 06:11 PM
My purpose in this code is only convert, then mark the begin and end of tables. The search function is in another code module. With Ruby I can do this function easily. Thank for your attention.

fumei
12-18-2008, 01:51 PM
Ok, sounds good.