How to click button in MS Access using C# and Office Interop? - c#

I am trying to automate some tasks in MS Access which are involving some button presses. Here are my best guesses so far, but I never find a way to finally execute a click...
using Microsoft.Office.Interop.Access;
Application access = new Application();
access.OpenCurrentDatabase("SomeDatabaseName.accdb");
access.DoCmd.OpenForm("SomeFormName", AcFormView.acNormal);
access.DoCmd.GoToControl("SomeButtonName");
access.DoCmd... // I can go to the control, but how do I click it?
Or maybe there is another approach using the Microsoft.Office.Interop.Access.CommandButton instance?
var form = access.Forms["SomeFormName"];
foreach (var control in form.Controls)
{
if (control is CommandButton button)
{
// I can get the CommandButton, but how do I execute the click procedure?
string onClick = button.OnClick; // it's just this string: "[Event Procedure]"
}
}
access.CloseCurrentDatabase();
access.Quit(AcQuitOption.acQuitSaveNone);
Is this possible using the MS Office Interop assemblies? Or are there any alternatives? Unfortunately, these interop assemblies are very poorly documented.

You can use the CommandBars.ExecuteMso method which executes the control identified by the idMso parameter. This method is useful in cases where there is no object model for a particular command. Works on controls that are built-in buttons, toggleButtons, and splitButtons. On failure it returns E_InvalidArg for an invalid idMso, and E_Fail for controls that are not enabled or not visible. For example:
Application.CommandBars.ExecuteMso("Copy")
If you need to execute non-ribbon controls you may consider using Windows API functions or Accessibility API for that.

Related

How do I programmatically add a file to a fileupload control from a windows form to a webpage

I am using WebView2 in a C# windows application and I want to be able to upload screen captures.
I capture the screen and save it to a file (jpg) on the local hard drive. I want to attach this file (upload) it to the FILEUPLOAD control on a web page being displayed on a windows form using WebView2.
I was using CEFSHARP browser and was using DOM to upload the file before we switched to WebView2.
CEFSHAPRP had the DOM objects wrapped in the browser control so it was very
easy to use this code :
if (client == null)
{
client = browser.GetDevToolsClient();
dom = client.DOM.GetDocumentAsync();
}
await Task.Run(async () =>
{
QuerySelectorResponse querySelectorResponse = await client.DOM.QuerySelectorAsync(dom.Result.Root.NodeId, "#fileupload");
_ = client.DOM.SetFileInputFilesAsync(new string[] { filename }, querySelectorResponse.NodeId);
});
I do not see anything like this built into the WebView2 control. I did see that WebView has another nuget called WebView2.DOM but it requires me to convert my entire project from .NET framework 4.7.2 to .netCore.
Any help would be greatly appreciated. I am very new to WebView2 and cannot see how I can access the DOM to make this happen. I am using .net framework 4.7.2
******* June 17 Update *****
I installed the Microsoft.Web.WebView2.DevToolsProtocolExtension and tried to use the existing framework I already had working with CEFSHARP.
DevToolsProtocolHelper helper = webView21.CoreWebView2.GetDevToolsProtocolHelper();
Task<DOM.Node> t = helper.DOM.GetDocumentAsync(-1,false);
await Task.Run(async () =>
{
var querySelectorResponse = await helper.DOM.QuerySelectorAsync(t.Result.NodeId, "#fileupload");
_ = helper.DOM.SetFileInputFilesAsync(new string[] { filename }, querySelectorResponse);
});
It gives me the following error when I try to execute the line :
var querySelectorResponse = await helper.DOM.QuerySelectorAsync(t.Result.NodeId, "#fileupload");
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Web.WebView2.Core.Raw.ICoreWebView2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{76ECEACB-0462-4D94-AC83-423A6793775E}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Any ideas. Thank you for all the advice and help I have received already.
I believe there would be three ways to do this;
Get the screen co-ordinates of the button, and then using send keys send an enter key to open the dialog. Then (currently webview2 cannot intercept this dialog like CefSharp) you would need to use the WinAPI to manipulate the file dialog window.
I have used 1 before. It is a little unreliable with the send keys and in waiting for the dialog to appear and then getting the timing right with the dialog.
Using javascript you should be able to add to the filelist files and then add them to the element. I have not tried this with Webview2.
Finally, and the one you already know can be used. In your question here you use DOM.SetFileInputFilesAsync which as amaitland pointed out comes from the chrome devtools protocol.. WebView2 also supports the Chrome Devtools protocols so you can effectively use the same code you already know.

Create instance of internet explorer working with visual c#

I am working on a project to automate login and form fillup of a booking website.The user enters all the required info into a windows form.When i fire the Go button there is an automatic login followed by form fillup(it's an autofill thing i am trying to).I dont want to use the inbuilt web browser of visual c# of which i can easily create new instances.
Now i can start up Internet explorer but i need to tell the PC what info to fill in what page.So i think i need to create an instance of Internet Explorer for every different page
SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
IE.Navigate("https://www.irctc.co.in/eticketing/loginHome.jsf");
HtmlElementCollection inputs = IE.Document.GetElementsByTagName("input");
foreach (HtmlElement field in inputs)
{
if (field.GetAttribute("name").Equals("j_username"))
{
field.SetAttribute("value", "username");
}
if (field.GetAttribute("name").Equals("j_password"))
{
field.SetAttribute("value", "password");
}
}
When i use a web browser object instead of IE everything is fine.However now i get a runtime error
"Error HRESULT E_FAIL has been returned from a call to a COM component" in the getElemetsbytagname line.
You need to wait for the login page load to be completed before you try pulling and setting the inputs, add this line of code
while (IE.Busy);
before
HtmlElementCollection inputs = IE.Document.GetElementsByTagName("input");

Automated tests using MessageBox with MessageBoxButtons.YesNoCancel

I'm using the TestStack.White framework for a WinForms .Net 2.0 C# application's coded automated regression tests. I have no problem handling OK/Cancel message boxes (MessageBox.Show(...)) but I'm having trouble handling the Yes/No buttons when using MessageBoxButtons.YesNoCancel. Thing is, I do not know what the button names are.
These work in my test jigs:
public ButtonTestJig OkButton
{
get { return new ButtonTestJig(Window, "okButton"); }
}
public ButtonTestJig CancelButton
{
get { return new ButtonTestJig(Window, "cancelButton"); }
}
Does anybody know the equivalent "cancelButton" for the NO button and the equivalent "okButton" for the YES button? Or perhaps even better, how to find them. My googling failed me.
Thanks
You can use http://uiautomationverify.codeplex.com/releases/view/11366 or inspect.exe (which comes in the windows SDK to see what the automation Id's for those controls are.

How to click on a link using Webkit Browser?

I want to click on link after navigating to a website
webKitBrowser1.Navigate("http://www.somesite.com");
How to click on a link on this website assuming that the link's id is lnkId ?
Go to Google
In the default browser control that comes with Visual Studio, I can do that using the code below :
foreach (HtmlElement el in webBrowser1.Document.GetElementTagName("a")) {
if (el.GetAttribute("id") == "lnkId") {
el.InvokeMember("click");
}
}
What is the equivalent of the code above when I'm using WebkitDotNet control?
As the WebKit doesn't provide a Click() event (see here for details), you cannot do that in the above way. But a small trick may work as an equivalent of the original winforms way as below:
foreach (Node el in webKitBrowser1.Document.GetElementsByTagName("a"))
{
if (((Element) el).GetAttribute("id") == "lnkId")
{
string urlString = ((Element) el).Attributes["href"].NodeValue;
webKitBrowser1.Navigate(urlString);
}
}
Here what I am doing is casting the WebKit.DOM.Node object to its subclass WebKit.DOM.Element to get its Attributes. Then providing href to the NamedNodeMap, i.e. Attributes as the NodeName, you can easily extract the NodeValue, which is the target url in this case. You can then simply invoke the Navigate(urlString) method on the WebKitBrowser instance to replicate the click event.
I don't work with Windows and all my experience is on Webkit GTK. Following comments are based on that experience.
I am not sure which webkit .NET version you are using. Looks like there are multiple implementations. Assuming you are using the one mentioned by Wasif, you can evaluate javascript as mentioned in the example https://code.google.com/p/open-webkit-sharp/source/browse/JavaScriptExample/Form1.cs.
Actually if implementation is supporting javascript execution then you can do most, if not all the DOM operations. The API functions are usually same as javascript functions and most of the time call exact same functions internally despite of origination. Communication between your application and javascript can be little challenging, but if you can read alert messages, that also can be solved. It looks like this library does support alert handling mechanism. A tool I wrote at https://github.com/nhrdl/notesMD will show some examples of achieving this communication though it uses GTK version and is written in python.
Incidentally if you know the id of the element, then Document.GetElementById will save you the loop.
webKitBrowser1.StringByEvaluatingJavaScriptFromString("var inpt = document.createElement(\"input\"); inpt.setAttribute(\"type\", \"submit\"); inpt.setAttribute(\"id\", \"nut\"); inpt.setAttribute(\"type\", \"submit\"); inpt.setAttribute(\"name\", \"tmp\"); inpt.setAttribute(\"value\", \"tmp\"); var element = document.getElementById(\"lnk\"); element.appendChild(inpt);");
webKitBrowser1.StringByEvaluatingJavaScriptFromString("document.getElementById('nut').click();");

I must be a heretic for wanting a C# browser with both NewWindow2 and GetElementsByTagName

You can't have your cake and eat it too, apparently.
I'm currently using the System.Windows.Forms.WebBrowser in my application. The program currently depends on using the GetElementsByTagName function. I use it to gather up all the elements of a certain type (either "input"s or "textarea"s), so I can sort through them and return the value of a specific one. This is the code for that function (my WebBrowser is named web1):
// returns the value from a element.
public String FetchValue(String strTagType, String strName)
{
HtmlElementCollection elems;
HtmlDocument page = web1.Document.Window.Frames[1].Document;
elems = page.GetElementsByTagName(strTagType);
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("name") == strName ||
elem.GetAttribute("ref") == strName)
{
if (elem.GetAttribute("value") != null)
{
return elem.GetAttribute("value");
}
}
}
return null;
}
(points to note: the webpage I need to pull from is in a frame, and depending on circumstances, the element's identifying name will be either in the name or the ref attribute)
All of that works like a dream with the System.Windows.Forms.WebBrowser.
But what it is unable to do, is redirect the opening of a new window to remain in the application. Anything that opens in a new window shoots to the user's default browser, thus losing the session. This functionality can be easily fixed with the NewWindow2 event, which System.Windows.Forms.WebBrowser doesn't have.
Now forgive me for being stunned at its absence. I have but recently ditched VB6 and moved on to C# (yes VB6, apparently I am employed under a rock), and in VB6, the WebBrowser possessed both the GetElementsByTagName function and the NewWindow2 event.
The AxSHDocVw.WebBrowser has a NewWindow2 event. It would be more than happy to help me route my new windows to where I need them. The code to do this in THAT WebBrowser is (frmNewWindow being a simple form containing only another WebBrowser called web2 (Dock set to Fill)):
private void web1_NewWindow2(
object sender,
AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
{
frmNewWindow frmNW = new frmNewWindow();
e.ppDisp = frmNW.web2.Application;
frmNW.web2.RegisterAsBrowser = true;
frmNW.Visible = true;
}
I am unable to produce on my own a way to replicate that function with the underwhelming regular NewWindow event.
I am also unable to figure out how to replicate the FetchValue function I detailed above using the AxSHDocVw.WebBrowser. It appears to go about things in a totally different way and all my knowledge of how to do things is useless.
I know I'm a sick, twisted man for this bizarre fantasy of using these two things in a single application. But can you find it in your heart to help this foolish idealist?
I could no longer rely on the workaround, and had to abandon System.Windows.Forms.WebBrowser. I needed NewWindow2.
I eventually figured out how to accomplish what I needed with the AxWebBrowser. My original post was asking for either a solution for NewWindow2 on the System.Windows.Forms.WebBrowser, or an AxWebBrowser replacement for .GetElementsByTagName. The replacement requires about 4x as much code, but gets the job done. I thought it would be prudent to post my solution, for later Googlers with the same quandary. (also in case there's a better way to have done this)
IHTMLDocument2 webpage = (IHTMLDocument2)webbrowser.Document;
IHTMLFramesCollection2 allframes = webpage.frames;
IHTMLWindow2 targetframe = (IHTMLWindow2)allframes.item("name of target frame");
webpage = (IHTMLDocument2)targetframe.document;
IHTMLElementCollection elements = webpage.all.tags("target tagtype");
foreach (IHTMLElement element in elements)
{
if (elem.getAttribute("name") == strTargetElementName)
{
return element.getAttribute("value");
}
}
The webbrowser.Document is cast into an IHTMLDocument2, then the IHTMLDocument2's frames are put into a IHTMLFramesCollection2, then I cast the specific desired frame into an IHTMLWindow2 (you can choose frame by index # or name), then I cast the frame's .Document member into an IHTMLDocument2 (the originally used one, for convenience sake). From there, the IHTMLDocument2's .all.tags() method is functionally identical to the old WebBrowser.Document.GetElementsByTagName() method, except it requires an IHTMLElementCollection versus an HTMLElementCollection. Then, you can foreach the collection, the individual elements needing to be IHTMLElement, and use .getAttribute to retrieve the attributes. Note that the g is lowercase.
The WebBrowser control can handle the NewWindow event so that new popup windows will be opened in the WebBrowser.
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
// navigate current window to the url
webBrowser1.Navigate(webBrowser1.StatusText);
// cancel the new window opening
e.Cancel = true;
}
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/361b6655-3145-4371-b92c-051c223518f2/
The only solution to this I have seen was a good few years ago now, called csExWb2, now on Google code here.
It gives you an ExWebBrowser control, but with full-on access to all the interfaces and events offered by IE. I used it to get deep and dirty control of elements in a winforms-hosted html editor.
It may be a bit of a leap jumping straight into that, mind.

Categories