I have a VBA macro that Imports Word tables preserving format, but it splits the content of cells. It seems that the break lines are causing the content to be split into several cells in excel. I am not very good at coding and could not find any solution. I can just ask for help from experts in this forum. Below is the macro. I would really appreciate your help. Thank you!!
Sub ImportTablesAndFormat()
Dim wdApp As Object
Dim wdDoc As Object
Dim wdTbl As Object
Dim wdCell As Object
Dim wdRange As Object
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Dim xlCell As Object
Dim myPath As String
Dim myFile As String
Dim numRows As Long
Dim numCols As Long
Dim i As Long
Dim j As Long
' Prompt user to select folder with Word files
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select Folder with Word Files"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
myPath = .SelectedItems(1) & "\"
End With
' Create new Excel workbook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlCell = xlBook.Sheets(1).Cells(1, 1)
' Loop through each Word file in folder
myFile = Dir(myPath & "*.docx")
Do While myFile <> ""
' Open Word document
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Open(myPath & myFile)
wdApp.Visible = False
' Loop through each table in Word document
For Each wdTbl In wdDoc.Tables
' Get dimensions of table
numRows = wdTbl.Rows.Count
numCols = wdTbl.Columns.Count
' Add new sheet to Excel workbook
Set xlSheet = xlBook.Sheets.Add(After:=xlBook.Sheets(xlBook.Sheets.Count))
xlSheet.Name = myFile & "Table" & xlSheet.Index
' Copy table to Word range
Set wdRange = wdTbl.Range
wdRange.Copy
' Paste table to Excel range
xlSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
' Clear clipboard
Application.CutCopyMode = False
' Adjust cell dimensions to match Word table
For i = 1 To numRows
For j = 1 To numCols
Set wdCell = wdTbl.Cell(i, j)
Set xlCell = xlSheet.Cells(i, j)
' Replace line breaks with a space
Dim cellText As String
cellText = Replace(wdCell.Range.Text, Chr(13), " ")
cellText = Replace(cellText, Chr(11), " ") ' Optional: Replace manual line breaks as well
xlCell.Value = cellText
xlCell.WrapText = wdCell.Range.ParagraphFormat.WordWrap
xlCell.Font.Bold = wdCell.Range.Font.Bold
xlCell.Font.Italic = wdCell.Range.Font.Italic
xlCell.Font.Color = wdCell.Range.Font.Color
xlCell.Interior.Color = wdCell.Range.Shading.BackgroundPatternColor
xlCell.Borders(xlEdgeLeft).LineStyle = wdCell.Borders(-1).LineStyle
xlCell.Borders(xlEdgeLeft).Weight = xlMedium
xlCell.EntireRow.AutoFit
Next j
Next i
' Clear contents of Word range
wdRange.Delete
Next wdTbl
' Close Word document
wdDoc.Close SaveChanges:=False
Set wdDoc = Nothing
' Move to the next Word file in the folder
myFile = Dir
Loop
' Set the column widths
For Each xlSheet In xlBook.Sheets
xlSheet.Columns(1).ColumnWidth = 82
xlSheet.Columns(2).ColumnWidth = 32
Next xlSheet
' Save and close the Excel workbook
xlBook.SaveAs Filename:=myPath & "Tables.xlsx", FileFormat:=51
xlBook.Close SaveChanges:=True
xlApp.Quit
' Clean up objects
Set xlCell = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
' Display completion message
MsgBox "All tables from Word files in " & myPath & " have been imported into the Excel workbook " & myPath & "Tables.xlsx.", vbInformation, "Tables Converted"
End Sub
Tried to modify the loop in several ways I found online, but nothing would work. I lack the knowledge to try anything deeper I guess. I just want the content of every cell in the tables in word to be in one cell also in excel. a copy and paste really. They have break lines, so most of cells have more than one line. Usually the second line start with a "(", if that helps. The format is being copied ok. I am sorry I cannot provide you with a file as a template due to GDPR. Thanks a lot.