473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problems exporting Data to Excel

I've worked 3 hours to solve this problem and I can't.

I'm trying to write code to move data into an excel spreadsheet. When I first open Access and run the code everything works fine. As you can see from my code, data is placed in excel and the worksheet is left on the screen for me to view. After I close excel and immediately run the same code excel will open but it does not appear to open my worksheet. As a matter of fact it kind of freezes as it only captures a picture of the screen. If I then close excel and open it from the desktop and then open the worksheet everything's fine. I can close and reopen Access and it would again work 1 time before this problem.

Here's my code:
Dim app As Excel.Application
Dim wb As Excel.Workbook
Dim sht As Excel.Worksheet
Dim rng As Excel.Range
Dim cht As Excel.Chart

Set app = Excel.Application
app.Visible = True
Set wb = Workbooks.Open("TestTemplate.xls")
Set sht = wb.ActiveSheet

sht.Cells(4, 4) = "Test"

Any thoughts on why this is happening?
Oct 24 '07 #1
3 1878
nico5038
3,080 Expert 2GB
Did you try closing the objects after filling the cells like:
Expand|Select|Wrap|Line Numbers
  1. Set sht = Nothing
  2. Set wb = Nothing
  3. app.quit
  4.  
Nic;o)
Oct 24 '07 #2
I tried that and it didn't work. This must be something simple that I'm missing but I can't figure it out. Any other ides?
Oct 25 '07 #3
nico5038
3,080 Expert 2GB
Hmm, I used the following code myself in the past without this trouble:

Create a new module "modAPI" with:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Declare Function acb_apiFindWindow Lib "user32" Alias "FindWindowA" (ByVal strClassName As String, ByVal lpWindowName As Any) As Long
  5.  
  6. Function acbIsWordLoaded() As Long
  7.     ' Report if a Word application is currently loaded
  8.     ' In:
  9.     '     strClassName: the Windows class name for the main window
  10.     '                   of the app to check for.
  11.     ' Out:
  12.     '     Return Value: 0 if App isn't loaded, non-zero (the
  13.     '         app's hWnd) otherwise.
  14.  
  15.    acbIsWordLoaded = acb_apiFindWindow("OpusApp", 0&)
  16.  
  17. End Function
  18. Function acbIsExcelLoaded() As Long
  19.     ' Report if a Word application is currently loaded
  20.     ' In:
  21.     '     strClassName: the Windows class name for the main window
  22.     '                   of the app to check for.
  23.     ' Out:
  24.     '     Return Value: 0 if App isn't loaded, non-zero (the
  25.     '         app's hWnd) otherwise.
  26.  
  27.    acbIsExcelLoaded = acb_apiFindWindow("XLmain", 0&)
  28.  
  29. End Function
  30.  
Now we can test or there's already an excel app running before opening the file and the closure should now work OK.

Expand|Select|Wrap|Line Numbers
  1. Function fncExportExcel()
  2.  
  3. Dim appExcel As Excel.Application
  4. Dim wkbExcel As Excel.Workbook
  5. Dim chtExcel As Excel.Chart
  6. Dim strFile As String
  7.  
  8. If acbIsExcelLoaded Then
  9.    Set appExcel = Excel.Application
  10. Else
  11.    Set appExcel = New Excel.Application
  12. End If
  13.  
  14. strFile = "c:\xxx\xls"
  15. Set wkbExcel = appExcel.Workbooks.Open(strFile)
  16.  
  17. wkbExcel.Sheets("Sheet1").Cells(4, 4) = "Test"
  18.  
  19. wkbExcel.Save
  20. appExcel.Application.Workbooks.Close
  21.  
  22. End Function
  23.  
Nic;o)
Oct 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
4
by: D | last post by:
I've created a report with many subreports of aggregate data. I want my client to be able to export this data to Excel to make her charts, etc. Only one problem: one of the fields is a "SchoolYear"...
2
by: G | last post by:
When I export data from access to excel by with "export" or "Analyze with" I seem to loose parts of some fields (long text strings). Is there a way to export it all to excel? Thanks G
2
by: pmud | last post by:
Hi, I am exporting data from an EDITABLE DATA GRID EXCEL. But the 1st column in data grid is Edit Column. I want to display all columns in Excel except for the Edit column. The following...
2
by: bienwell | last post by:
Hi, I have a question about exporting data from datagrid control into Excel file in ASP.NET. On my Web page, I have a linkbutton "Export data". This link will call a Sub Function to perform...
1
by: mark.heyden | last post by:
Hi Frends, I am facing problems while trying to export data into pipe format. In fact I am fetching records from mysql database and then trying to create a text file ( using pipe as delimeter) ,...
2
by: Snozz | last post by:
The short of it: If you needed to import a CSV file of a certain structure on a regular basis(say 32 csv files, each to one a table in 32 databases), what would be your first instinct on how to...
2
by: Kevin G. | last post by:
Hello, I'm exporting an ASP page to Excel using 'Response.ContentType = "application/vnd.ms-excel"' and it is working fine for the most part except the particular page I'm exporting has images,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.