web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Vbscript change the or...
Power Automate
Suggested Answer

Vbscript change the original format from copy excel to word action

(1) ShareShare
ReportReport
Posted on by 95
Hi All,
 
Would like to enquire, I wanted to copy a datatable from excel & transfer it to an existing microsoft word document via vbscript. But no matter how i set, the format doesn't follow. Let's say if my excel was under Arial, font 10, during script runs and paste this datatable to word, it will change to Time New Romans font 11. Not just that, the width column would also change.  Is there any possibilities that thru vbscripts i could preserve the format?
 

' Create Excel and Word objects

Set excelApp = CreateObject("Excel.Application")

Set wordApp = CreateObject("Word.Application")



excelApp.DisplayAlerts = False 



' Open the Excel workbook

Set excelWorkbook = excelApp.Workbooks.Open("C:\Users\....\Masterlisted.xlsx")

Set excelSheet = excelWorkbook.Sheets(1)



' Select all the data in the used range

excelSheet.UsedRange.Copy ' Copies with formatting



' Open the Word document

Set wordDocument = wordApp.Documents.Open("C:\Users\.....\International A2P Blocking List (test.docx")



' Make Word visible

wordApp.Visible = True



' Find the last free space in the Word document

Set wordRange = wordDocument.Content

wordRange.Collapse 0 ' Collapse to the end of the document



' Paste the data with "Keep Source Formatting"

' Equivalent of keeping source formatting is just using Paste (Word usually retains Excel formatting)

wordRange.Paste



' Clean up

excelWorkbook.Close

excelApp.Quit

wordDocument.Close

wordApp.Quit





Set excelSheet = Nothing

Set excelWorkbook = Nothing

Set excelApp = Nothing



Set wordRange = Nothing

Set wordDocument = Nothing

Set wordApp = Nothing



 
I have the same question (0)
  • Suggested answer
    VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at
    Hey Hi,
     
    Try this below Vbscript code.
     
    ' Create Excel and Word objects
    Set excelApp = CreateObject("Excel.Application")
    Set wordApp = CreateObject("Word.Application")
    
    excelApp.DisplayAlerts = False 
    
    ' Open the Excel workbook
    Set excelWorkbook = excelApp.Workbooks.Open("C:\Users\....\Masterlisted.xlsx")
    Set excelSheet = excelWorkbook.Sheets(1)
    
    ' Select all the data in the used range
    excelSheet.UsedRange.Copy ' Copies with formatting
    
    ' Open the Word document
    Set wordDocument = wordApp.Documents.Open("C:\Users\.....\International A2P Blocking List (test.docx")
    
    ' Make Word visible
    wordApp.Visible = True
    
    ' Find the last free space in the Word document
    Set wordRange = wordDocument.Content
    wordRange.Collapse 0 ' Collapse to the end of the document
    
    ' Paste with "Keep Source Formatting" using Word's built-in PasteSpecial function
    wordRange.PasteSpecial DataType:=12 ' DataType 12 is wdPasteHTML, which retains formatting
    
    ' You could also try this for an embedded Excel object:
    ' wordRange.PasteSpecial DataType:=1 ' DataType 1 is wdPasteOLEObject
    
    ' Clean up
    excelWorkbook.Close False
    excelApp.Quit
    wordDocument.Save ' Save Word document
    wordDocument.Close False
    wordApp.Quit
    
    ' Release objects
    Set excelSheet = Nothing
    Set excelWorkbook = Nothing
    Set excelApp = Nothing
    Set wordRange = Nothing
    Set wordDocument = Nothing
    Set wordApp = Nothing
    
     
    (Note:- if you got your solution you can mark as solution and gives kudos)
     
     
    Thanks & Regards
    Vishnu Reddy

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard