Results 1 to 8 of 8

Thread: Read data from closed file (Excel)

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Read data from closed file (Excel)

    Hi,
    I hope someone could help me with my problem.
    I have an excel file "from_mydata.xls" closed. It has only one sheet ("data1"). I need to read the cell "a1" value and put it in a variable in order to confront this value with another, read from a second file "to_mydata.xls" already open.
    I would like to read this value without open "from_mydata" file. Is it possible ?

    thanks.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Read data from closed file (Excel)

    Welcome to the Forums.

    You can use the Excel Object Model to open the file but hidden or you can use ADO to run a SELECT query against the file. Writting to the file while the user has it open can not be done. If a user has a file open the you can open it as read only and it wont conflict using eithe method.


    VB Code:
    1. option Explicit
    2. 'Add a reference to ms excel xx.0 object library
    3. Private sub command1_click()
    4.     Dim oApp as Excel.Application
    5.     Dim oWBFrom As Excel.workbook
    6.     Dim oWBTo As Excel.Workbook
    7.     Dim strFromA1 As String
    8.     Dim strToA1 As String
    9.     Set oApp = New Excel.Application
    10.     oApp.Visible = False
    11.     Set oWBFrom = oApp.workbooks.open(FileName:="C:\from_mydata.xls", ReadOnly:=True)
    12.     Set oWBTo = oApp.Workbooks.open(FileName:="C:\to_mydata.xls", ReadOnly:=True)
    13.     strFromA1 = oWBFrom.Sheets("Sheet1").Cells(1, 1).Value
    14.     strToA1 = oWBTo.sheets("Sheet1").Cells(1, 1).Value
    15.     MsgBox strFrom & ", " & strTo, vbOkOnly
    16.     oWBFrom.Close SaveChanges:=False
    17.     oWBTo.Close SaveChanges:=False
    18.     Set oWBFrom = Nothing
    19.     Set oWBTo = Nothing
    20.     oApp.Quit
    21.     Set oApp = nothing
    22. End sub
    Last edited by RobDog888; May 24th, 2006 at 05:13 AM. Reason: Forgot the ReadOnly:=True parts
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Read data from closed file (Excel)

    thank you very much to welcome me and for your helpful advice

  4. #4
    New Member
    Join Date
    Dec 2011
    Posts
    6

    Re: Read data from closed file (Excel)

    Two Excel 2007 files contain identical sheet structure (but different data) and identical macro. Each macro should be able to read (in the background if possible) whole sheet from the other file. Reading command is controlled with a button. So far I have decided to read/copy one sheet into a new sheet in the active file and then work with the data locally. I have come to two approaches:

    1. I can open the file but this runs its macro and changes the focus. Can the file be opened with its macro disabled, while the macro that opens it remains active? Will "Application.Visible = False" apply to the workbook that is running the macro?

    2. The "ExecuteExcel4Macro" function works fine but rather slow, as the sheets are about 1000 rows by 40 columns.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Read data from closed file (Excel)

    Will "Application.Visible = False" apply to the workbook that is running the macro?
    yes, unless you open the other workbook in a separate instance of excel, then you can have one visible and the other not
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    New Member
    Join Date
    Dec 2011
    Posts
    6

    Re: Read data from closed file (Excel)

    I tried this
    VB Code:
    1. Application.DisplayAlerts = False
    2. Application.Visible = True
    3. Set wks_to = Worksheets.Add
    4. Set app_from = New Excel.Application
    5. app_from.DisplayAlerts = False
    6. app_from.Visible = False
    7. Set file_from = Workbooks.Open(file_from_name, 3, True)
    8. file_from.Worksheets("Sheet1").Activate
    9. file_from.Worksheets("Sheet1").UsedRange.Copy
    10. wks_to.Activate
    11. wks_to.Paste
    12. file_from.Close SaveChanges:=False
    but it looks like lines 5 and 6 have no effect, and both workbooks are affected by lines 1 and 2. If I remove line 1, I receive questions about updating links (line 7), about emptying the clipboard (line 12), plus both workbooks are visible. It works but it's not elegant so I'm still digging.
    Last edited by Boyko; Dec 8th, 2011 at 09:50 AM.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Read data from closed file (Excel)

    while you have created a separate instance of excel, you have opened both workbooks in the same instance

    line 7 should specify app_from.workbooks.open

    app_from by default would not be visible anyway, but make sure not to allow alerts, as they also would not be visible
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    New Member
    Join Date
    Dec 2011
    Posts
    6

    Re: Read data from closed file (Excel)

    That was helpful, thank you. But the dialog about the Clipboard continued to appear and I stopped it with

    app_from.CutCopyMode = False

    so I consider it resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width