friendsvasup.blogg.se

How to copy and paste in word and preserve formatting
How to copy and paste in word and preserve formatting




how to copy and paste in word and preserve formatting

Before we copy it, let's set our Excel objects. Now we have the handle to the Word Table. Now you have the document open, Let's connect with say Table1 of the word document. "Browse for file containing table to be imported") See this example Sub Sample()įlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*",, _ Once you have connected with/created the Word instance, simply open the word file. Set oWordApp = CreateObject("Word.Application") Set oWordApp = GetObject(, "Word.Application") '~~> Establish an Word application object For example Sub Sample()ĭim oWordApp As Object, oWordDoc As Object I am using Late Binding where you do not need to add any references.ĭeclare your Word objects and then bind with either an existing instance of Word or create a new instance. To interact with Word from Excel, you can choose either Early Binding or Late Binding. ObjTemplateSheetExcelWkBk.SaveAs strFld & "\" & strFileName WorksheetFunction.Clean(.cell(iRow + 1, iCol + 1).Range.Text) ObjTemplateSheetExcelSheet.Cells(2, yourArray(arrycnt) + 1) = _ WorksheetFunction.Clean(.cell(iRow, iCol + 1).Range.Text) ObjTemplateSheetExcelSheet.Cells(2, yourArray(arrycnt)) = _ StrEach = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text) 'copy cell contents from Word table cells to Excel cells Set objTemplateSheetExcelSheet = (5) '(Selecting the desired tab) ("C:\Temp\Documents Page XX_US-VC Combo Template.xlsx") Set objTemplateSheetExcelApp = CreateObject("Excel.Application")

#How to copy and paste in word and preserve formatting code

Ok I am attaching the remaining piece of code as well 'Creating TemplateSheet object Then for all the tables present in the word doc accessing each row and column of the tables using the above mentioned code. I am running a table count on the word file. MsgBox "This document contains no tables", _ TableNo = '(Counting no of tables in the document) Set wdDoc = GetObject(wdFileName) '(open Word file) If wdFileName = False Then Exit Sub '(user cancelled import file browser) "Browse for file containing table to be imported") '(Browsing for a file)

how to copy and paste in word and preserve formatting

The logic I am using is as follows - wdFileName = Application.GetOpenFilename("Word files (*.*),*.*",, _ ObjTemplateSheetExcelSheet.Cells(1, 2) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text) Kindly let me know how I can edit this so as to preserve source formatting. Now i want to preserve the source formatting present in word doc. It is copying the text perfectly as desired. I am trying to copy some data from a word table to an excel sheet using a VB Macro.






How to copy and paste in word and preserve formatting