How to preserve source formatting while copying data from word table to excel sheet using VB macro?

前端 未结 1 1433
情深已故
情深已故 2020-12-06 08:56

I am trying to copy some data from a word table to an excel sheet using a VB Macro.

It is copying the text perfectly as desired.

Now i want to preserve the s

相关标签:
1条回答
  • 2020-12-06 09:03

    To interact with Word from Excel, you can choose either Early Binding or Late Binding. I am using Late Binding where you do not need to add any references.

    I will cover the code in 5 parts

    1. Binding with a Word Instance
    2. Opening the Word document
    3. Interacting with Word Table
    4. Declaring Your Excel Objects
    5. Copying the word table to Excel

    A. Binding with a Word Instance


    Declare your Word objects and then bind with either an existing instance of Word or create a new instance. For example

    Sub Sample()
        Dim oWordApp As Object, oWordDoc As Object
    
        '~~> Establish an Word application object
        On Error Resume Next
        Set oWordApp = GetObject(, "Word.Application")
    
        If Err.Number <> 0 Then
            Set oWordApp = CreateObject("Word.Application")
        End If
        Err.Clear
        On Error GoTo 0
    
        oWordApp.Visible = True
    End Sub
    

    B. Opening the Word document


    Once you have connected with/created the Word instance, simply open the word file.. See this example

    Sub Sample()
        Dim oWordApp As Object, oWordDoc As Object
        Dim FlName As String
    
        FlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*", , _
                 "Browse for file containing table to be imported")
    
        '~~> Establish an Word application object
        On Error Resume Next
        Set oWordApp = GetObject(, "Word.Application")
    
        If Err.Number <> 0 Then
            Set oWordApp = CreateObject("Word.Application")
        End If
        Err.Clear
        On Error GoTo 0
    
        oWordApp.Visible = True
    
        '~~> Open the Word document
        Set oWordDoc = oWordApp.Documents.Open(FlName)
    End Sub
    

    C. Interacting with Word Table


    Now you have the document open, Let's connect with say Table1 of the word document.

    Sub Sample()
        Dim oWordApp As Object, oWordDoc As Object
        Dim FlName As String
        Dim tbl As Object
    
        FlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*", , _
                 "Browse for file containing table to be imported")
    
        '~~> Establish an Word application object
        On Error Resume Next
        Set oWordApp = GetObject(, "Word.Application")
    
        If Err.Number <> 0 Then
            Set oWordApp = CreateObject("Word.Application")
        End If
        Err.Clear
        On Error GoTo 0
    
        oWordApp.Visible = True
    
        Set oWordDoc = oWordApp.Documents.Open(FlName)
    
        Set tbl = oWordDoc.Tables(1)
    End Sub
    

    D. Declaring Your Excel Objects


    Now we have the handle to the Word Table. Before we copy it, let's set our Excel objects.

    Sub Sample()
        Dim oWordApp As Object, oWordDoc As Object
        Dim FlName As String
        Dim tbl As Object
    
        FlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*", , _
                 "Browse for file containing table to be imported")
    
        '~~> Establish an Word application object
        On Error Resume Next
        Set oWordApp = GetObject(, "Word.Application")
    
        If Err.Number <> 0 Then
            Set oWordApp = CreateObject("Word.Application")
        End If
        Err.Clear
        On Error GoTo 0
    
        oWordApp.Visible = True
    
        Set oWordDoc = oWordApp.Documents.Open(FlName)
    
        Set tbl = oWordDoc.Tables(1)
    
        '~~> Excel Objects
        Dim wb As Workbook, ws As Worksheet
    
        Set wb = Workbooks.Open("C:\Temp\Documents Page XX_US-VC Combo Template.xlsx")
    
        Set ws = wb.Sheets(5)
    End Sub
    

    E. Copying the word table to Excel


    And finally when we have the destination all set, simply copy the table from word to Excel. See this.

    Sub Sample()
        Dim oWordApp As Object, oWordDoc As Object
        Dim FlName As String
        Dim tbl As Object
    
        FlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*", , _
                 "Browse for file containing table to be imported")
    
        '~~> Establish an Word application object
        On Error Resume Next
        Set oWordApp = GetObject(, "Word.Application")
    
        If Err.Number <> 0 Then
            Set oWordApp = CreateObject("Word.Application")
        End If
        Err.Clear
        On Error GoTo 0
    
        oWordApp.Visible = True
    
        Set oWordDoc = oWordApp.Documents.Open(FlName)
    
        Set tbl = oWordDoc.Tables(1)
    
        '~~> Excel Objects
        Dim wb As Workbook, ws As Worksheet
    
        Set wb = Workbooks.Open("C:\Temp\Documents Page XX_US-VC Combo Template.xlsx")
    
        Set ws = wb.Sheets(1)
    
        tbl.Range.Copy
    
        ws.Range("A1").Activate
    
        ws.Paste
    End Sub
    

    SCREENSHOT

    Word Document

    enter image description here

    Excel (After Pasting)

    enter image description here

    Hope this helps.

    0 讨论(0)
提交回复
热议问题