473,508 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SendKeys out of sync...

I am writing a C# Windows App that updates out Excel reports' modules. The
app is complete, but has a problem. The only way MS allows you to unprotect
the VBA code in Excel is to do it by hand or sendkeys.

I hate using SendKeys, but I am forced to use them. Anyway, I have written a
function that will unprotect the VBA code in the current work book. It works
about 60% of the time. For some reason it gets out of sync and the SendKeys
don't go to the correct window or are just simply not sent. This means that
VBA never gets unprotected and the rest of the code after that fails. In
this case it throws exceptions every which way. Does anyone know of a way to
make sure that my sendkeys get sent correctly? I have provided my code
below... Thanks in advanced!!

Bryan
using System;

using System.Windows.Forms;

using utils = UpdateWkBks.utils;

using System.Runtime.InteropServices;

namespace UpdateWkBks

{

/// <summary>

/// Summary description for xlProtection.

/// </summary>

public class xlProtection

{

public xlProtection()

{

//

// TODO: Add constructor logic here

//

}

//declare those two apis in the declaration section of your form

[DllImport("User32",EntryPoint="FindWindow")]

private static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);

[DllImport("User32",EntryPoint="BringWindowToTop")]

private static extern bool BringWindowToTop(IntPtr wHandle);

/// <summary>

/// Unprotect Excel VB Code

/// </summary>

/// <param name="Pwd"></param>

public static void UnprotectVBA(string Pwd)

{

Excel.Application objApp = utils.GetXlApp(); //Current XL app

Excel.Workbook WB = utils.GetWkBk(); //Current Wkbk

bool ret;

//This function will UnProtect a VBProject in an Excel file.

WB.Activate(); //Activate the correct WorkBook.

objApp.VBE.MainWindow.Visible = true;
IntPtr hWnd = FindWindow(null, objApp.VBE.MainWindow.Caption);//Find window

if(hWnd != IntPtr.Zero)

{

ret = BringWindowToTop(hWnd); //Bring VBE to top.

}

//Use SendKeys to unlock VBProject.

SendKeys.SendWait("%T");

SendKeys.SendWait("E");

SendKeys.SendWait(Pwd);

SendKeys.SendWait("{ENTER}");

SendKeys.SendWait("{ENTER}");

SendKeys.SendWait("%F");

SendKeys.SendWait("C");

}

/// <summary>

/// Password Protect Excel VB code.

/// </summary>

/// <param name="Pwd"></param>

public static void ProtectVB(string Pwd)

{

Excel.Application objApp = utils.GetXlApp(); //Current XL app

Excel.Workbook WB = utils.GetWkBk(); //Current Wkbk

bool ret;

//This function will protect a VBProject in an Excel file.
//If VBProject isn't already protected.

if (WB.VBProject.Protection.Equals(1) == true)

{

WB.Activate(); //Activate the correct WorkBook.

objApp.VBE.MainWindow.Visible = true;
IntPtr hWnd = FindWindow(null, objApp.VBE.MainWindow.Caption);//change this
caption to the caption of you outlook window

if(hWnd != IntPtr.Zero)

{

ret = BringWindowToTop(hWnd);

}

//Use SendKeys to set VBProject to protected.

SendKeys.SendWait("%T");

SendKeys.SendWait("E");

SendKeys.SendWait("+{TAB}");

SendKeys.SendWait("{RIGHT}");

SendKeys.SendWait("%V");

SendKeys.SendWait("{TAB}");

SendKeys.SendWait(Pwd);

SendKeys.SendWait("{TAB}");

SendKeys.SendWait(Pwd);

SendKeys.SendWait("{ENTER}");

SendKeys.SendWait("%F");

SendKeys.SendWait("C");

objApp.VBE.MainWindow.Visible = false;

}

}

}

}
Nov 15 '05 #1
1 7705
Try something like this:

Excel.Application oXL;
Excel._Workbook oWB;

oXL = new Excel.Application();
oXL.Visible = true;
oWB = (Excel._Workbook)(oXL.Workbooks.Open("c:\\MyWorkbo ok.xls",
Typee.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing));

oXL.VBE.ActiveVBProject = oWB.VBProject;

SendKeys.SendWait("{ENTER}");
SendKeys.SendWait("p");
SendKeys.SendWait("a");
SendKeys.SendWait("s");
SendKeys.SendWait("s");
SendKeys.SendWait("w");
SendKeys.SendWait("o");
SendKeys.SendWait("r");
SendKeys.SendWait("d");
SendKeys.SendWait("{ENTER}");

--

"Bryan" <bm****@elite-oregon.com> wrote in message news:<##*************@TK2MSFTNGP09.phx.gbl>...
I am writing a C# Windows App that updates out Excel reports' modules. The
app is complete, but has a problem. The only way MS allows you to unprotect
the VBA code in Excel is to do it by hand or sendkeys.

I hate using SendKeys, but I am forced to use them. Anyway, I have written a
function that will unprotect the VBA code in the current work book. It works
about 60% of the time. For some reason it gets out of sync and the SendKeys
don't go to the correct window or are just simply not sent. This means that
VBA never gets unprotected and the rest of the code after that fails. In
this case it throws exceptions every which way. Does anyone know of a way to
make sure that my sendkeys get sent correctly? I have provided my code
below... Thanks in advanced!!

Bryan
using System;

using System.Windows.Forms;

using utils = UpdateWkBks.utils;

using System.Runtime.InteropServices;

namespace UpdateWkBks

{

/// <summary>

/// Summary description for xlProtection.

/// </summary>

public class xlProtection

{

public xlProtection()

{

//

// TODO: Add constructor logic here

//

}

//declare those two apis in the declaration section of your form

[DllImport("User32",EntryPoint="FindWindow")]

private static extern IntPtr FindWindow(string lpClassName,string
lpWindowName);

[DllImport("User32",EntryPoint="BringWindowToTop")]

private static extern bool BringWindowToTop(IntPtr wHandle);

/// <summary>

/// Unprotect Excel VB Code

/// </summary>

/// <param name="Pwd"></param>

public static void UnprotectVBA(string Pwd)

{

Excel.Application objApp = utils.GetXlApp(); //Current XL app

Excel.Workbook WB = utils.GetWkBk(); //Current Wkbk

bool ret;

//This function will UnProtect a VBProject in an Excel file.

WB.Activate(); //Activate the correct WorkBook.

objApp.VBE.MainWindow.Visible = true;
IntPtr hWnd = FindWindow(null, objApp.VBE.MainWindow.Caption);//Find window

if(hWnd != IntPtr.Zero)

{

ret = BringWindowToTop(hWnd); //Bring VBE to top.

}

//Use SendKeys to unlock VBProject.

SendKeys.SendWait("%T");

SendKeys.SendWait("E");

SendKeys.SendWait(Pwd);

SendKeys.SendWait("{ENTER}");

SendKeys.SendWait("{ENTER}");

SendKeys.SendWait("%F");

SendKeys.SendWait("C");

}

/// <summary>

/// Password Protect Excel VB code.

/// </summary>

/// <param name="Pwd"></param>

public static void ProtectVB(string Pwd)

{

Excel.Application objApp = utils.GetXlApp(); //Current XL app

Excel.Workbook WB = utils.GetWkBk(); //Current Wkbk

bool ret;

//This function will protect a VBProject in an Excel file.
//If VBProject isn't already protected.

if (WB.VBProject.Protection.Equals(1) == true)

{

WB.Activate(); //Activate the correct WorkBook.

objApp.VBE.MainWindow.Visible = true;
IntPtr hWnd = FindWindow(null, objApp.VBE.MainWindow.Caption);//change this
caption to the caption of you outlook window

if(hWnd != IntPtr.Zero)

{

ret = BringWindowToTop(hWnd);

}

//Use SendKeys to set VBProject to protected.

SendKeys.SendWait("%T");

SendKeys.SendWait("E");

SendKeys.SendWait("+{TAB}");

SendKeys.SendWait("{RIGHT}");

SendKeys.SendWait("%V");

SendKeys.SendWait("{TAB}");

SendKeys.SendWait(Pwd);

SendKeys.SendWait("{TAB}");

SendKeys.SendWait(Pwd);

SendKeys.SendWait("{ENTER}");

SendKeys.SendWait("%F");

SendKeys.SendWait("C");

objApp.VBE.MainWindow.Visible = false;

}

}

}

}

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
460
by: Mr. Bungle | last post by:
I would like to send email automatically via a command button. I have accomplished this just fine through the following code: (Outlook should already be open for reliable results) Private Sub...
2
5679
by: RBohannon | last post by:
I need to create a report in MS Word populated with data from A2K. I have been asked to create the report in Word so that parts of it can be edited as necessary later. The data in the report are...
1
9518
by: George | last post by:
Every time I used the Sendkeys command in my application the "Numlock" turned off and I couldn't use the keypad to hit numbers...... The old code was: Private Sub Command1_Click() SendKeys...
5
8739
by: Wayne Gibson | last post by:
Hi, Was wondering if somebody could help.. I'm trying to use Sendkeys on a Windows forms. I have entered the following command to simulate a CTRL+ALT+1.. ...
1
27165
by: GrantS | last post by:
I need to use a sendkeys key combination to automate the "accept files" that a remote user wants to send to me via Windows messenger. I am using automation to work with Windows Messenger client in...
6
30988
by: Gary | last post by:
Hi, I am trying to use the "System.Windows.Forms.SendKeys" class for triggering the Ctrl+P key. Syntax: System.Windows.Forms.SendKeys.Send("^(P)") This is not working ..what could be the...
0
1528
by: Austin | last post by:
Hello: I have written a very simple vb program (using the new .net interface). I have a website to which I must upload many files. The filenames are incremented in such a way that a simple...
6
3305
by: Michael Maes | last post by:
Hello, I'm invoking successive SendKeys.SendWait("{BACKSPACE}") SendKeys.SendWait("{DELETE}") in a loop. The issue is that it causes a Beep on every Pass. Setting the 'Handled = True' on...
0
2739
by: dtshedd | last post by:
I have a database with hundreds of embedded photos (Microsoft Photo 3.0) Many are larger than 1 MB. I tried Stephen Lebans macro but it did not work probably for the aforementioned reasons Now...
0
7124
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...
1
7046
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
7498
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...
0
4707
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...
0
3195
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...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
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 ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.