473,626 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Switch Focus to Excel with Access VBA Automation

124 New Member
Hi, I am trying to figure out some way to open an Excel workbook from Access vba and have the focus automatically shift to Excel. What happens is is that the Excel workbook shows up as a blinking Excel icon in the taskbar. I call the following code from Access to open the Excel workbook.

Expand|Select|Wrap|Line Numbers
  1. Function OpenExcel(strFileName As String)
  2.  
  3. Dim XL As Excel.Application
  4. Dim WKB As New Excel.Workbook
  5.  
  6. Set XL = New Excel.Application
  7. XL.Visible = True
  8.  
  9. Set WKB = XL.Workbooks.Open(strFileName)
  10. WKB.Activate
  11.  
  12. 'WKB.Close SaveChanges:=False
  13.  
  14.  
  15. Set WKB = Nothing
  16. Set XL = Nothing
  17.  
  18.  
  19. End Function
  20.  
  21.  
Thanks in advance for any advice.
Aug 26 '13 #1
4 9963
zmbd
5,501 Recognized Expert Moderator Expert
Try inserting etween lines 6 and 7:
xl.UserControl = True

I have a similar code that dumps stuff out to excel using basically Line 6, myline, 7 and the focus is left on the worksheet... (I also have code to select range a1 too)

Let us know if this works.

(you might take a look a this too: 163. Application Automation starts with Outlook; however, has an excel example too.
Aug 26 '13 #2
ADezii
8,834 Recognized Expert Expert
If you are really stuck, you can use the API to retrieve the Window's Handle in the Task Bar, then Maximize it.
Aug 27 '13 #3
cjswimmer
1 New Member
I know this is a really old thread but I found a solution and thought I should share. You can use
Expand|Select|Wrap|Line Numbers
  1. AppActivate XL.Windows(1).Caption
Mar 25 '19 #4
NeoPa
32,567 Recognized Expert Moderator MVP
Nice. I'll go ahead and set that to Best Answer.

I'm sure the other suggestions also work but this is exactly what was requested.
Mar 28 '19 #5

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

Similar topics

0
1054
by: John Henry | last post by:
Does anybody know how to find an opened copy of Word and switch focus to it, wait until Word is closed, before continuing my Python script? I tried to search for the answer from Google and nothing stands out. Running under Windows XP. Thanks, -- John
4
1287
by: John Spiegel | last post by:
Hi All, Does anyone know of a site that might give some decent documentation on Excel 2000 objects, particularly their methods and parameters, in order? The little I've found so far seems to mainly focus on using named parameters which doesn't do me much good in C#! TIA, John
3
2547
by: Pete | last post by:
Is there any possiblity of writing an Access or Visual Basic application that provides a method of sharing the window focus between Access and the Shell application? i.e. Shell("c:\windows\calculator.exe", vbNormalNoFocus) In other words are there Win2000/XP api's that would allow you to control the functionality of the calculator. I'm just using the calculator application as an example I am actually trying to control a main frame app...
7
5336
by: taylor.bryant | last post by:
I am running: Win XP SP2 Excel 2002, Access 2002 (Office XP SP3) Using Visual Basic (not VB.NET) At one point (prior to XP SP2?!? - I can't pin it down), this did not happen and I was easily able to destroy instances of excel (with the exact same code). I have read many, many posts, and they seem to get bogged down in specifics. So I cribbed this program from the automation help file, simplified it further, so hopefully someone can
1
5046
by: cybertof | last post by:
Hello, Is there a way to connect (through automation) a c# application to a running Excel 2003 instance on a specific workbook ? In the past, i used to use GetObject(...) function in VB6. Regarding C#, I have heard about System.Runtime.InteropServices.marshal.GetActiveObject ("Excel.Application")
3
2799
by: Carlos Magalhaes | last post by:
Hey All, I am doing some excel automation using the excel COM. I can do most of the functions and its working well until I come across a formula. I can run a formula and insert the formula value into a cell. BUT this is where it comes "complex".
0
927
by: Dylan | last post by:
I have written a program using VB.NET 2005 that writes macros for a redundant processing of data. This program works when using it on Office 2000. However, I had to change the code to work with Office 2003. When I did that, it would come up with an error saying that there is an HRESULT. I have used the code that is in the MSDN for the solution to fix the problem, but that code does not work. I even ran that code alone to see if that...
3
2037
by: hayuti | last post by:
Hi there This Question was posted on a wrong site. Just as introduction I am working in Access VBA where I coded a routine to retrive data from Access Database, exporting it on to Excel file (sucessful opens and posts the data on the required number of sheets) and finally is supposed to plot a scattered graph. At this stage the routine successful exports the data to excel but I am having a problem with the chart plotting part. Even...
1
1434
by: Romanopoly | last post by:
Hello, I have written Code in .Net to export Data from a DataGridView to Excel, it works but the object is not released. Problem is: - The program is closed, Excel is still open, the COM object is not released in the .Net program. - Excel is closed, but still the COM object is not released. - I can write CreateObject in .Net, but not Release, Release is not recognized. - Also COM object ( the ExcelApplication) = Nothing does not work. ...
1
1145
by: =?Utf-8?B?TWFyaw==?= | last post by:
What I want to do is bring data into an excel 2007 worksheet that is stored in a sql database table. The idea is to parameterize the "where" clause in the sql select statement limiting the result set. There are lots of examples in the literature with regard to bringing in data stored in cubes --- but not so much from data in sql table. The basic issue is I want bring in descriptive information about say a
0
8259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8696
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7188
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.