Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Import Table / Field Code Data from Word

Hi,

I am trying to create a macro that will import data from every file in
a folder (all .DOC) into Excel in a structured format (one row per
document). Initially I thought that Copying and Pasting the tables
from each document would work but the formatting is very
inconsistant.

My preference would be to cycle through the field codes (table cells I
guess) and then place the contents in a cell on the spreadsheet. Since
code is not my strong point and controlling Word through Excel really,
really isn't a strong point any help would be greatly appreciated!

This is how far I have got just now for copying tables, ideally
copying the field codes would be betteralthough I am open for other
ideas if someone can help?

Sub PopulateExcel()

'Build the file list
Dim strFile As String, strFileList() As String, intFile As Integer,
myFilename As String
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document

'Folder location
path1 = "C:\123456\"
strFile = Dir(path1 & "*.doc")

While strFile < ""
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()

Wend

For intFile = 1 To UBound(strFileList)
myFilename = path1 & strFileList(intFile)
'Next intFile

Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Open(myFilename)

'For r = 1 To wrdDoc.Tables.Count

With wrdDoc
.unprotect “12345”
.ActiveWindow.Selection.Tables(1).Select
.Content.Copy
'.Tables(r).Range.Copy
End With

'Specify Excel File
Windows("Test.xls").Activate

'This new sheet was just for ease just now to let me see what was
actualyl working!
Sheets.Add

ActiveCell.PasteSpecial xlPasteValues
'Next r
Next intFile

'Close off files

wrdDoc.Close
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing


MsgBox "All Done"
End Sub

Best Regards,

CalumMurdo


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Import Table / Field Code Data from Word

I think the way you are going is the best method. I think you need to change
the formating after the data is entered into excel. first check to make usre
there aren't extra spaces at the beginning of end of the data then try this

Put code after this line
ActiveCell.PasteSpecial xlPasteValues


With ActiveSheet.Cells

.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False

With .Font
.Bold = False
.Italic = False
.Underline = xlUnderlineStyleNone
.Name = "Arial"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.ColorIndex = xlAutomatic
End With
End With


" wrote:

Hi,

I am trying to create a macro that will import data from every file in
a folder (all .DOC) into Excel in a structured format (one row per
document). Initially I thought that Copying and Pasting the tables
from each document would work but the formatting is very
inconsistant.

My preference would be to cycle through the field codes (table cells I
guess) and then place the contents in a cell on the spreadsheet. Since
code is not my strong point and controlling Word through Excel really,
really isn't a strong point any help would be greatly appreciated!

This is how far I have got just now for copying tables, ideally
copying the field codes would be betteralthough I am open for other
ideas if someone can help?

Sub PopulateExcel()

'Build the file list
Dim strFile As String, strFileList() As String, intFile As Integer,
myFilename As String
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document

'Folder location
path1 = "C:\123456\"
strFile = Dir(path1 & "*.doc")

While strFile < ""
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()

Wend

For intFile = 1 To UBound(strFileList)
myFilename = path1 & strFileList(intFile)
'Next intFile

Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Open(myFilename)

'For r = 1 To wrdDoc.Tables.Count

With wrdDoc
.unprotect €œ12345€
.ActiveWindow.Selection.Tables(1).Select
.Content.Copy
'.Tables(r).Range.Copy
End With

'Specify Excel File
Windows("Test.xls").Activate

'This new sheet was just for ease just now to let me see what was
actualyl working!
Sheets.Add

ActiveCell.PasteSpecial xlPasteValues
'Next r
Next intFile

'Close off files

wrdDoc.Close
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing


MsgBox "All Done"
End Sub

Best Regards,

CalumMurdo



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run a VB code when you click on the data field of a Pivot Table pgarcia Excel Discussion (Misc queries) 1 March 4th 09 08:27 PM
Code for a Macro that allows a Pivot table to be repeatedly run (fora dynamic rows in a table, but static field names) Mike C[_5_] Excel Programming 1 February 11th 08 04:30 AM
how to import Ms word Table to Ms Excel for Various Calculations reneabesmer Excel Worksheet Functions 1 August 3rd 05 09:06 AM
Can I import a Word table to an Excel spreadsheet? Liz Excel Worksheet Functions 1 November 25th 04 04:41 PM
Pivot table field swapping in code Janene[_2_] Excel Programming 0 June 9th 04 01:01 AM


All times are GMT +1. The time now is 11:44 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"