|
-
May 24th, 2006, 03:37 AM
#1
Thread Starter
New Member
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.
-
May 24th, 2006, 04:56 AM
#2
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:
option Explicit
'Add a reference to ms excel xx.0 object library
Private sub command1_click()
Dim oApp as Excel.Application
Dim oWBFrom As Excel.workbook
Dim oWBTo As Excel.Workbook
Dim strFromA1 As String
Dim strToA1 As String
Set oApp = New Excel.Application
oApp.Visible = False
Set oWBFrom = oApp.workbooks.open(FileName:="C:\from_mydata.xls", ReadOnly:=True)
Set oWBTo = oApp.Workbooks.open(FileName:="C:\to_mydata.xls", ReadOnly:=True)
strFromA1 = oWBFrom.Sheets("Sheet1").Cells(1, 1).Value
strToA1 = oWBTo.sheets("Sheet1").Cells(1, 1).Value
MsgBox strFrom & ", " & strTo, vbOkOnly
oWBFrom.Close SaveChanges:=False
oWBTo.Close SaveChanges:=False
Set oWBFrom = Nothing
Set oWBTo = Nothing
oApp.Quit
Set oApp = nothing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 24th, 2006, 09:09 AM
#3
Thread Starter
New Member
Re: Read data from closed file (Excel)
thank you very much to welcome me and for your helpful advice
-
Dec 2nd, 2011, 10:21 AM
#4
New Member
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.
-
Dec 2nd, 2011, 03:28 PM
#5
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
-
Dec 8th, 2011, 07:08 AM
#6
New Member
Re: Read data from closed file (Excel)
I tried this
VB Code:
Application.DisplayAlerts = False Application.Visible = True Set wks_to = Worksheets.Add Set app_from = New Excel.Application app_from.DisplayAlerts = False app_from.Visible = False Set file_from = Workbooks.Open(file_from_name, 3, True) file_from.Worksheets("Sheet1").Activate file_from.Worksheets("Sheet1").UsedRange.Copy wks_to.Activate wks_to.Paste 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.
-
Dec 8th, 2011, 02:58 PM
#7
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
-
Dec 13th, 2011, 04:47 PM
#8
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|