Copy entire worksheet from one instance of Excel to another

Danatela

According to this article, you cannot move or copy worksheet from one instance of Excel to another. Unfortunately, it's the only way I have to make my program function properly.

I have 2 instances of Excel: one is run by our ancient ERP system and another through OLE call. The macros running in the second should copy first worksheet from opened workbook (ThisWorkbook) into workbook opened in the first instance (Wb). I'm using ForEachLoop's solution to get Wb:

Public Function GetExcelObjectFromHwnd(ByVal hWnd As Long) As Boolean
...
    If AccessibleObjectFromWindow(hWnd, OBJID_NATIVEOM, iid, obj) = 0 Then 'S_OK
        Dim objApp As Excel.Application
        Set objApp = obj.Application
        Dim Wb As Workbook
        For Each Wb In objApp.Workbooks
            ProcessWorkbook Wb
        Next Wb

        fOk = True
    End If
...
End Function

Sub ProcessWorkbook(Wb as Worksheet)
...
    'This produces error because ThisWorkBook and Wb are opened in different instances of Excel:
    ThisWorkbook.Sheets(1).Copy , Wb.Sheets(1)
    'What I developed so far
    Wb.Sheets.Add , Wb.Sheets(1)
    'this doesn't work too:
    ThisWorkbook.Sheets(1).UsedRange.Copy Wb.Sheets(2).Range("A1")
    'and this works but doesn't copy formatting:
    With ThisWorkbook.Sheets(1).UsedRange
        Wb.Sheets(2).Range("A1").Resize(.Rows.Count, .Columns.Count) = .Value
    End With
    ' later I perform some operations with cells
...
End Sub

As you can guess, I first tried to use Worksheet.Copy method then Range.Copy method and they both don't work. And the direct range assignment copies only values and I need also formatting to be copied.

So, apparently, solution which will copy formatting is appropriate, but I'd prefer direct copying if there is any way to do it. Also, please don't suggest to use clipboard, as it is always bad idea.

Jean-François Corbett

I suggest you SaveAs your workbook from Excel instance A to a temp file, and then open this temp file in Excel instance B in order to copy the sheet you need.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy entire worksheet from one instance of Excel to another

From Dev

excel Copy shapes from one worksheet to another

From Dev

Excel VBA Copy matching information from one worksheet to another with a loop

From Dev

Copy from all rows one Worksheet and paste in to Another in alternate rows(Excel vba)

From Dev

copying of specific range of excel cells from one worksheet to another worksheet

From Dev

copy a row in one worksheet and paste in another worksheet

From Dev

Copy fields from one instance to another in Django

From Dev

Copy VBA code from one Worksheet to another using VBA code

From Dev

Copy Worksheet from one workbook to another. Type Mismatch 13

From Dev

Copy a range from one worksheet to another using cells property

From Dev

Copy data from one worksheet to another based on column

From Dev

copy and paste a cell from one worksheet to another and multiply it by a value

From Dev

how to copy a cell from one worksheet to another, to the correct row

From Dev

Copy range from one worksheet to another based on criterion

From Dev

Copy and Paste Specific Cells from one worksheet to another

From Dev

Copy data from a Pivot table in one worksheet ot another

From Dev

Copy data from one worksheet to another for dynamic lastrow

From Dev

Copy Range from Worksheet to another Worksheet VBA

From Dev

Copy entire file system hierarchy from one drive to another

From Dev

VBA Excel copying range of formulas from one worksheet to another

From Dev

Excel - Need to get a figure from one worksheet to another (it's complicated)

From Dev

Copy multiple rows from one worksheet to another worksheet using macro/vba

From Dev

How do I copy a worksheet from one workbook into another new workbook with just that worksheet in (with pastevalues)?

From Dev

VBA Excel 2013 Copy certain information from one worksheet to anther worksheet - weekly reports

From Dev

How to read the entire worksheet from excel

From Dev

Java: Copy attributes from one object instance to another?

From Dev

Copy worksheet from another workbook including charts

From Dev

Excel: Use value in one worksheet to unhide rows in another worksheet

From Dev

Copy specific cells from one row and paste into different cells on another worksheet

Related Related

  1. 1

    Copy entire worksheet from one instance of Excel to another

  2. 2

    excel Copy shapes from one worksheet to another

  3. 3

    Excel VBA Copy matching information from one worksheet to another with a loop

  4. 4

    Copy from all rows one Worksheet and paste in to Another in alternate rows(Excel vba)

  5. 5

    copying of specific range of excel cells from one worksheet to another worksheet

  6. 6

    copy a row in one worksheet and paste in another worksheet

  7. 7

    Copy fields from one instance to another in Django

  8. 8

    Copy VBA code from one Worksheet to another using VBA code

  9. 9

    Copy Worksheet from one workbook to another. Type Mismatch 13

  10. 10

    Copy a range from one worksheet to another using cells property

  11. 11

    Copy data from one worksheet to another based on column

  12. 12

    copy and paste a cell from one worksheet to another and multiply it by a value

  13. 13

    how to copy a cell from one worksheet to another, to the correct row

  14. 14

    Copy range from one worksheet to another based on criterion

  15. 15

    Copy and Paste Specific Cells from one worksheet to another

  16. 16

    Copy data from a Pivot table in one worksheet ot another

  17. 17

    Copy data from one worksheet to another for dynamic lastrow

  18. 18

    Copy Range from Worksheet to another Worksheet VBA

  19. 19

    Copy entire file system hierarchy from one drive to another

  20. 20

    VBA Excel copying range of formulas from one worksheet to another

  21. 21

    Excel - Need to get a figure from one worksheet to another (it's complicated)

  22. 22

    Copy multiple rows from one worksheet to another worksheet using macro/vba

  23. 23

    How do I copy a worksheet from one workbook into another new workbook with just that worksheet in (with pastevalues)?

  24. 24

    VBA Excel 2013 Copy certain information from one worksheet to anther worksheet - weekly reports

  25. 25

    How to read the entire worksheet from excel

  26. 26

    Java: Copy attributes from one object instance to another?

  27. 27

    Copy worksheet from another workbook including charts

  28. 28

    Excel: Use value in one worksheet to unhide rows in another worksheet

  29. 29

    Copy specific cells from one row and paste into different cells on another worksheet

HotTag

Archive