PDA

View Full Version : [SOLVED:] Word Table - Saving Each Row as a New Document File



dj44
04-11-2016, 07:25 AM
Folks,

very good day to all on this monday.:)

I have got stuck on my table task.

I don't know if its possible at all - but i did get the idea from here.

http://stackoverflow.com/questions/13077740/create-text-files-from-every-row-in-an-excel-spreadsheet

I would like to save each row of my word table as a new document file.

it is set up like this

15891





Sub SaveTableRowAsNewFile()

' To save each row as a new document file

Dim strFolder As String ' For the folder where it will be saved
Dim strFile As String ' Table Column 1 - name of file?

Dim wdDoc As Document
Dim oTable As Table
Dim longindex as long


Set oTable = ActiveDocument.Tables(1) ' Table contains rows - each row to be saved as a file



oTable.Cell(oTable.Rows.Count, 2).Range.FormattedText = wdDoc.Range.FormattedText
'Keep original line breaks in content

'Extract the data?

' Filename Column 1 - start in Row 2

wdDoc.SaveAs2 Filename:=strPath & oTable.Cell(oTable.Rows.Count, longindex, 2) & ".docx", AddToRecentFiles:=False


wdDoc.Close

Next longindex

Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub





I have tried googling for any information to get it up and running but i dont seem to be able to find anything.

I am grateful if anyone can point me in the right direction

thank you very much to the pro coders for your valuable time

DJ

gmaxey
04-11-2016, 02:59 PM
Something like this:


Sub SaveTableRowAsNewFile()
Dim strFolder As String ' For the folder where it will be saved
Dim wdDoc As Document
Dim oTbl As Table
Dim lngIndex As Long

strFolder = "D:\"
Set oTbl = ActiveDocument.Tables(1)
For lngIndex = 1 To oTbl.Rows.Count
Set wdDoc = Documents.Add
wdDoc.Range.FormattedText = oTbl.Rows(lngIndex).Range.FormattedText
wdDoc.SaveAs2 strFolder & Left(oTbl.Cell(lngIndex, 1).Range, Len(oTbl.Cell(lngIndex, 1).Range) - 2) & ".docx", wdFormatDocumentDefault, , , False
wdDoc.Close
Next lngIndex

End Sub

gmayor
04-11-2016, 09:20 PM
You could do this with mail merge combined with http://www.gmayor.com/MergeAndSplit.htm
All you need to do is setup a merge document with the table e.g.
15894

then merge that with your table document

dj44
04-12-2016, 05:35 AM
Greg,

I don't believe it.

I wasn't really sure the job could be done,

But it made the documents like magic. :)


One table was 50 rows, that was a huge headached, losing my position in the document to keep copying and pasting.

This will save me hours of time.

Also Graham Thank you very much for the additional idea, the mail merge addin - I never knew such a good tool existed -state-of-the-art

I am very excited and happy - I will be setting up all sorts now this week :grinhalo:

I used to shy away from computers and dump all my files in folders, but now i have to pay the price and sort them all out

Thank you for the tremendous and fine pro coding help - I am still learning the ropes but one error took me 5 hours last week and i still didnt manage to solve it - so i came for help


Thank you for the help again and i hope you will have the the greatest week Greg

and you folks too
cheers:beerchug:
DJ