Forum Discussion
ShazSh
Sep 24, 2021Brass Contributor
Convert Excel Range into Word without Tables
I have created below code to copy data and paste into word the code is working fine but the problem is when it paste the data into word document then i convert the table into text.
Then there are 2 to 3 tables in the Excel data (which i want to keep as it is in word document) which also removes in the word document.
Excel tables
https://ibb.co/G36M177
and Word removes every table and their words
https://ibb.co/vvTdkJT
Sub ExcelWordPaste()
Dim objWord As Object
Dim objDoc As Object
Application.ScreenUpdating = False
Application.EnableEvents = False
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Range("B3:AP198").Copy
Set objDoc = objWord.Documents.Add
With objDoc.Range
.PasteExcelTable False, False, False
.Tables(1).AutoFitBehavior 2
.InsertAfter vbCr
End With
objWord.Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:= _
True
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
6 Replies
- Juliano-PetrukioBronze Contributor
Interesting .
Sub ExportToWord() Dim WordApp As Word.Application, WordDocument As Word.Document Set WordApp = New Word.Application WordApp.Visible = True Set WordDocument = WordApp.Documents.Add ActiveSheet.UsedRange.Copy WordDocument.Range.Paste Application.CutCopyMode = False Dim tbl As Object For Each tbl In WordDocument.Tables tbl.AutoFitBehavior wdAutoFitContent Next WordDocument.SaveAs Filename:=Application.ActiveWorkbook.Path & "\" & ActiveSheet.Name Set WordDocument = Nothing Set WordApp = Nothing End Sub
If it helps you, remember to flag it as solved and hit the like button.
- ShazShBrass ContributorNo, Its not seems that i have the code in the workbook i forgot to paste. can you please check it.
- Juliano-PetrukioBronze ContributorThe code is different from yours. I developed it from scratch based on your post. Can you please test it to see if works as you wish?