[VBA] Copy Range from Excel Paste into Word Autofit Contents to Window & Page & Keep Source Format

Seba Robles

Board Regular
Joined
May 16, 2018
Messages
73
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
The title pretty much sums up what I am trying to accomplish but have had no luck whatsoever.

I'm trying to copy a range from an excel worksheet to word but when I do, it changes the text formatting and the table extends beyond the first page (both to the right and onto the next page).

So I'm trying to;

- autofit contents to window
- keep source fromatting when pasting
- maintin the pasted table within one page
- paste new table onto a new blank page

Rich (BB code):
Sub ExcelWordPaste()

Dim objWord As Object
Dim objDoc As Object

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.documents.Add
    
    Range("A9:H48").Copy
    objWord.Selection.PasteSpecial Placement:=wdInLine
    objWord.Selection.TypeParagraph

End Sub

Any help is appreciated. Thanks in advanced!
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try:
Code:
Sub ExcelWordPaste()

Dim objWord As Object
Dim objDoc As Object

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
    
Range("A9:H48").Copy

Set objDoc = objWord.documents.Add
With objDoc.Range
    .PasteExcelTable False, False, False
    .Tables(1).AutoFitBehavior 2 'wdAutoFitWindow
    .InsertAfter vbCr
End With
End Sub
Do note that:
- autofit contents to window
- keep source fromatting when pasting
is contradictory. Whether you can achieve
- maintain the pasted table within one page
depends on how high the table is.
 
Upvote 0
So what I meant when keeping source formatting was more for font & font size. For some reason when I copy paste from Excel to Word with VBA it changes the text to Calibri size 11 I think... but the table in Excel has headers etc and each header vs content has different font sizes.

As for your last point, the table isn't very long so they fit in 1 page, but after pasting from Excel to Word, it extends it for some reason. I need to manually adjust it so that it fits in 1 page. Is there a way to do this with VBA? Similar to AutoFit to window but with length (top to bottom)?

And as for the code, it worked, but my full code is actually to copy/paste multiple tables and what you shared will only autofit the first table. Do you know how I could do it so that it autofits each table it pastes inWord?

Rich (BB code):
For Each c In inputRange
    dvCell = c.Value
    
    Range("A9:H43").Copy  

With objDoc.Range
    .PasteExcelTable False, False, False
    .Tables(1).AutoFitBehavior 2 'wdAutoFitWindow
    .InsertAfter vbCr
End With

Next c
 
Last edited:
Upvote 0
I'm not seeing any font changes from what was originally in the worksheet, regardless of what the underlying fonts in the document are. The spacing changes are probably because you have a conflict between Word and Excel as to the characteristics of the 'Normal' Style. To overcome that and provide for multiple tables to be output, use:
Code:
With objDoc.Range.Characters.Last
  .PasteExcelTable False, False, False
  With .Tables(1)
    .Range.ParagraphFormat.SpaceBefore = 0
    .Range.ParagraphFormat.SpaceAfter = 0
    '.AutoFitBehavior 2 'wdAutoFitWindow
  End With
  .InsertAfter Chr(12)
End With
Note that I've commented-out AutoFitBehavior - you should test what you're getting without it. There is no vertical equivalent to AutoFitBehavior.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top