Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Have c# program open in middle of excel window

Posted on 2011-03-04
5
Medium Priority
?
407 Views
Last Modified: 2012-08-13
I have a c# program that consists of a form and a few controls on it.  I have a VBA macro that will open this program using this code

dblShellRetn = Shell("C:\theProgram.exe", vbNormalFocus)

This opens the program however I want it to be centered within the excel application window.  

Is there some way with Excel vba to not only open an external application but center it within the excel window?  Otherwise how would I program it in c# so that whenever it is opened it finds out the center of the excel application and positions itself to be in the middle of the excel window?  
0
Comment
Question by:ecoder27
  • 3
5 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35041451
One way that I can think of doing this is using APIs.

If you have the name of the C# Exe then You can use the API FindWindow to get the handle of that C# application and then use the API SetWindowPos to change the position of your C# window.

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35041454
>>> C# Exe

I meant C# Window Caption

Sid
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 1000 total points
ID: 35041621
I searched my database if I had some similar kind of code but I couldn't find it. I then searched Google but couldn't find any so I created this from scratch for you :)

I have also put comments in the code so that you may not have any problems understanding it.

Sid

Tested and Tried Successfully with Notepad and Excel

Please paste this code in a module.

'~~> Code Created By Siddharth Rout on 5th March 2011
'~~> You may freely use it in your Application however
'~~> Please do not delete these 3 lines from the code

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Public Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, _
lpRect As RECT) As Long

Sub Center3rdPartyApp()
    Dim Ret As Long
    
    '~~> Caption of C# Application
    Dim strCaption As String
    
    '~~> For Storing Width and Height of C# Application
    Dim CAppWidth As Long, CAppHeight As Long
    
    '~~> For Storing Width and Height of Excel Workbook
    Dim ExcelAppWidth As Long, ExcelAppHeight As Long
    
    '~~> This is required to get the Height and Width of
    '~~> C# Application and Excel Workbook
    Dim rc As RECT
    
    '~~> For new position of the C# App
    Dim NewLeft As Long, NewTop As Long
    
    '~~> Testing it with Notepad. Replace this with the C# App caption
    strCaption = "Untitled - Notepad"
    
    '~~> Get the handle of the Excel - Book1. Please change the caption
    '~~> accordingly. This is one classic case where WYSIWYG doesn't apply
    '~~> Window will show "Book1 - Microsoft Excel" but it is the opposite
    Ret = FindWindow(vbNullString, "Microsoft Excel - Book1")
    
    '~~> Get the width and height of "Microsoft Excel - Book1"
    GetClientRect Ret, rc
    ExcelAppWidth = rc.Right - rc.Left
    ExcelAppHeight = rc.Bottom - rc.Top
    
    '~~> Get the width and height of C# App
    Ret = FindWindow(vbNullString, strCaption)
    GetClientRect Ret, rc
    CAppWidth = rc.Right - rc.Left
    CAppHeight = rc.Bottom - rc.Top
    
    '~~> Calculate the new position of the C# App
    NewLeft = (ExcelAppWidth / 2) - (CAppWidth / 2) + ActiveWindow.Left
    NewTop = (ExcelAppHeight / 2) - (CAppHeight / 2) + ActiveWindow.Top
    
    '~~> Set the position
    SetWindowPos Ret, 0, NewLeft, NewTop, CAppWidth, CAppHeight, 0
End Sub

Open in new window

0
 
LVL 24

Expert Comment

by:Tracy
ID: 35357000
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question