472,325 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 7532
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
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...
2
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...
1
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...
5
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...
1
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...
6
by: Gary | last post by:
Hi, I am trying to use the "System.Windows.Forms.SendKeys" class for triggering the Ctrl+P key. Syntax:...
0
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...
6
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...
0
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.