PDA

View Full Version : [SOLVED:] VBA Table - Import Word Documents into Table Cell



dj44
03-25-2016, 03:26 PM
Folks,

Good day and evening to all.:)

I am needing some help on my table task.

I am trying to import the text found in word documents. These documents have various descriptions of data that I need to organise.

- 1 word document into 1 table cell in column 2.

I am hoping I can keep the source document formatting too - it was nicely formatted in the document.

Example:


Column 2
==================
Content of File1.docx
==================
Content of File 2.docx
==================
Content of File 3.docx
==================


I have got started but its gone wrong. I don't know what i did wrong - actually there is a lot that's gone wrong - but i'm not sure how to explain.






Sub ImportWordDocumentIntoTableCell()

' import my files into a table -
' 1 file goes into 1 table cell in column 2



Application.ScreenUpdating = False
Dim strFolder As String
Dim strFile As String
Dim wdDoc As Document
Dim oTable As Table

strFolder = GetFolder


If strFolder = "" Then Exit Sub
Set wdDocTmp = Documents.Add
strFile = Dir(strFolder & "\*.docx", vbNormal)


While strFile <> ""
i = 0
Set wdDoc = Documents.Add
With wdDoc
On Error Resume Next


Set oTable = ActiveDocument.Tables(1)

'Start of loop

For i = 2 To oTable.Rows.Count

'Put Each File into the table cell in column 2

strFile.Import strFolder & "\" & strFile = oTable.Cell(i, 2).Range ' This is wrong





On Error GoTo 0


End With


strFile = Dir()


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

Function GetFolder() As String ' To browse for the folder that has the word documents

End Function
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing




Now i have been searching googles brains for articles on this but i cant find anything to help me get it started up

If you can assist on this poorly code I will be grateful.

Thank you for your expert time and advice.

cheers:)
DJ

gmaxey
03-25-2016, 04:42 PM
Something like this:


Sub ImportWordDocumentIntoTableCell()
Dim strFolder As String
Dim strFile As String
Dim wdDoc As Document
Dim oTable As Table

Application.ScreenUpdating = False
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\" & "*.doc*", vbNormal)
'Lets assume the active document is open and contains the table.
Set oTable = ActiveDocument.Tables(1)
While strFile <> ""
Set wdDoc = Documents.Open(strFolder & "\" & strFile)
oTable.Cell(oTable.Rows.Count, 2).Range.FormattedText = wdDoc.Range.FormattedText
oTable.Rows.Add
wdDoc.Close wdDoNotSaveChanges
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

dj44
03-25-2016, 05:42 PM
Greg,

thank you very much for your fine help.

It's done a mighty job to import those files perfectly.:grinhalo:


I was faced with a lot of copy and pasting - now that's no way to spend a weekend, so I've been tinkering with this code, but I fell flat with the code not starting up, thanks to you it's been solved now.

So I am very happy.

Thanks for making my weekend now.

Have a great weekend Greg, and you folks too.

Cheers:beerchug:

DJ