How to copy all tables from one document into separated files?

A

avkokin

Hello.
There is one document which has some tables. I need to copy every
table from this document into separate files. Every files will has
only one table. I tried use next macro (thank's Helmut Weber), but I
get only values from table, not type of table. Help me, please.

Sub Test0031()
Dim iDoc As Integer ' number of dcouments
Dim dDc1 As Document ' active document
Dim dDc2 As Document ' new document
Dim rTmp As Range ' temporary range
Set dDc1 = ActiveDocument
For iDoc = 1 To ActiveDocument.Tables.Count
Set rTmp = ActiveDocument.Tables(iDoc).Range
Set dDc2 = Documents.Add(Visible:=False)
dDc2.Range = rTmp
dDc2.SaveAs "c:\test\" & Format(iDoc, "000") & ".doc"
dDc2.Close
Next
Set dDc1 = Nothing
Set dDc2 = Nothing
End Sub
 
H

Helmut Weber

Hi Anton,

Sub Test0031()
Dim iDoc As Integer ' number of dcouments
Dim dDc1 As Document ' active document
Dim dDc2 As Document ' new document
Dim oTbl As Table ' temporary range
Set dDc1 = ActiveDocument
For iDoc = 1 To ActiveDocument.Tables.Count
Set oTbl = ActiveDocument.Tables(iDoc)
oTbl.Range.Copy
Set dDc2 = Documents.Add(Visible:=True)
Selection.Paste
dDc2.SaveAs "c:\test\" & Format(iDoc, "000") & ".doc"
dDc2.Close
Next
Set dDc1 = Nothing
Set dDc2 = Nothing
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Sub Test0031improved()
Dim iDoc As Integer ' number of documents
Dim dDc1 As Document ' active document
Dim dDc2 As Document ' new document
Dim oTbl As Table ' a table
Set dDc1 = ActiveDocument
For iDoc = 1 To dDc1.Tables.Count
Set oTbl = dDc1.Tables(iDoc)
oTbl.Range.Copy
Set dDc2 = Documents.Add(Visible:=True)
Selection.Paste
dDc2.SaveAs "c:\test\" & Format(iDoc, "000") & ".doc"
dDc2.Close
Next
Set dDc1 = Nothing
Set dDc2 = Nothing
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Graham Mayor

Dim iDoc As Integer ' number of dcouments
Dim dDc1 As Document ' active document
Dim dDc2 As Document ' new document
Dim rTmp As Range ' temporary range
Set dDc1 = ActiveDocument
For iDoc = 1 To dDc1.Tables.Count
Set rTmp = ActiveDocument.Tables(iDoc).Range
rTmp.Copy
Set dDc2 = Documents.Add(Visible:=False)
dDc2.Activate
Selection.Paste
dDc2.SaveAs "C:\Test\" & Format(iDoc, "000") & ".doc"
dDc2.Close
Next
Set dDc1 = Nothing
Set dDc2 = Nothing

should work

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Sorry, I'm no good today,
Dim iDoc As Integer ' number of documents

don't dim iDoc as integer, but as long instead,
which is of pure theoretical importance.

To dim numbers as integer seems to be a bad habit hard to break,
at least on occasions.
 
H

Helmut Weber

Hi Graham,
Set dDc2 = Documents.Add(Visible:=False)

I don't know what made me think my first attempt at this would work.
:-(

Yours doesn't work either because of Visible:=False.
Should be true, when pasting. :)
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jules

That works great Helmut.

Helmut Weber said:
Hi Graham,


I don't know what made me think my first attempt at this would work.
:-(

Yours doesn't work either because of Visible:=False.
Should be true, when pasting. :)
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Graham Mayor

Whether or not it shouldn't work, it actually does, because of the
immediately following line.
dDc2.Activate
I did test it before posting. Trust me, it works ;)

I didn't bother to look after the finer point of integer viz a viz long for
the variable. I just new that paste would work for this issue:)
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jules

It works too. Show offs!


Graham Mayor said:
Whether or not it shouldn't work, it actually does, because of the
immediately following line.
dDc2.Activate
I did test it before posting. Trust me, it works ;)

I didn't bother to look after the finer point of integer viz a viz long
for the variable. I just new that paste would work for this issue:)
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Hi Graham,
Whether or not it shouldn't work, it actually does, because of the
immediately following line.
dDc2.Activate
I did test it before posting. Trust me, it works ;)

sorry, not my day. :-(

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top