Active IE 11 window for SendKeys - windows

Problem : I am trying to automate the saving of a file (manage to save the file when the below IE bar appears at the bottom of the page)
My code is going on my intranet, clicking here and there and then I click on an "Export" button which will trigger this from IE :
I didn't manage to find a way to automate the saving of the file because the only way (I think) to interact with this "window" is to use SendKeys.
In order to do so I have to "activate" this window (Yes, I have it activated for the HTML scraping with this bit of code, but it's not the active window though):
I tried using AppActivate but for some reason it doesn't work.
There are 2 options to pass this obstacle :
Find a way to activate the IE window containing that saving bar so that I can use Application.SendKeys "%{S}" on it
(second option only) : Disable this saving bar or manage to have it open in a new window
So far I have gone through lots of posts talking about that subject but none gave an operational solution for that issue on IE 11.
Let me know if you want to see any bit of code, I have a lot of different attempts gathered from different posts but this would highly increase the length of this post.

Front-end automation is a tricky business. It can be really hard to find a solution that always works (those pesky users are free to move the mouse and click buttons at will, disrupting your code). Because you are taking the data from an intranet site, this suggests the data you need already exists within your organisation. If at all possible (and I know it isn't always) take the data from the servers/source systems, rather than via the UI.
AppActivate and SendKeys can be very fussy. Because the url is always the same a better approach would be to directly download it. Here is a example, based on another answer.
Before you can run code you will need to add two references:
Microsoft XML, v6.0
Microsoft ActiveX Data Objects 2.8 Library
From the VBA IDE click Tools >> References... and select from the list (the original answer does not use references, instead it uses late binding).
' Downloads a file from a given URL.
' URL String URL to get file. Ex: http:\\MySite\MyFile.
' Path String Location to save file. Ex: C:\Myfile.csv.
' Requires: Microsoft XML, v6.0
' Requires: Microsoft ActiveX Data Objects 2.8 Library.
Sub DownloadURL(ByVal URL As String, ByVal Path As String)
Dim httpXML As XMLHTTP60 ' Used to download data.
Dim st As ADODB.Stream ' Used to write downloaded data to file system.
Set httpXML = New XMLHTTP60
' Request file.
httpXML.Open "GET", URL, False
httpXML.send
' Download response.
If httpXML.Status = 200 And httpXML.readyState = 4 Then
Set st = New ADODB.Stream
st.Open
st.Type = adTypeBinary
st.Write httpXML.responseBody
st.SaveToFile Path, adSaveCreateOverWrite
st.Close
Else
MsgBox "Something went wrong :(", vbCritical, "An error occured"
End If
End Sub

Related

How do I use VBIDE in a VB6 Addin to programmatically print source code..?

How do I programmatically print source code in a VB6 Addin..? There are no print or preview methods that I can find for VBIDE in the Object Browser.
I've searched high & low on Google, and there's a strange lack of information on VBIDE code module printing. I get lots of hits for PrettyPrint, but that's all. The lack is so great that it makes me wonder if there's some fundamental concept that I'm completely missing.
I scared up a copy of the O'Reilly book mentioned by Herb in https://stackoverflow.com/a/41034211/2705042, and it makes no mention of the printing of source code. The only way I can see is to export the code to text files and print those through usual means unrelated to VBIDE.
I also checked Chip Pearson's guide to VBE at http://www.cpearson.com/excel/vbe.aspx, which is almost identical to VBIDE, and even there is no clue to printing of code, other than the idea I mentioned of saving to text files and then printing.
** Ideally, I'd like to use the existing VB6 File > Print dialog, with one extra checkbox added to it. I realize the addition of controls to an existing dialog is another topic, and I'm not averse to creating my own version of the print dialog.
It is possible with a CommandBarButton proxy and SendKeys.
Getting a handle to the Print CommandBarControl is simple enough, but pressing the button throws a dialog in your way, so we have to use SendKeys to set the options and submit the form....
You can use code similar to the following:
Dim printCommand As CommandBarControl
Set printCommand = Application.VBE.CommandBars.FindControl(ID:=4)
printCommand.Execute
'Yep, SendKeys, erghhh
Application.SendKeys "P" 'Force the whole project to print
Application.SendKeys "{ENTER}"

How to save an Excel file via VB 2010 without any dialogs (such as "save as")

I am trying to Save an Excel file Via VB 2010, and I have these questions
How can I disable the "save as" dialog? I tried things such as only "save" instead of "save as", but it doesen't work...
After I saved the file (using the save as) I can't Delete it... (I tried closing the excel file, Visual basic etc...) all i get is an error saying it is allready open in excel...
Is there a way to make VB show me the tips for writing the excel stuff (Ie - when I write messagebox. - it pops up "Show" for help. how can I enable this for excel code [worksheets.cells. ect.])
the connection:
Sub Connect()
' Connect to the excel file
oExcel = CreateObject("Excel.Application")
'Devine the workbook
oBook = oExcel.workbooks.open("e:\Words\Heb.xls")
End Sub
the saveas:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
oExcel.SaveAs(oExcel.Path & ".xls")
End Sub
Thanks a lot
I think Inafiziger has solved your main issue, it should be a vanilla Save.
As it was unclear to me exactly what your are doind (ie Visual Studio/VB/BA) then
On (1)
I thought it worth clarifying that you can use code inside the ThisWorkbook module to detect and handle a SaveAs if you are providing users with a choice. This Event detects the SaveAs and cancels it
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
MsgBox "You cannot use SaveAs to save this file", , "Save Cancelled!"
Cancel = True
End If
End Sub
This code can be programmatically added to your target workbook but I doubt you would need to resort to this given you should be able to run the simple Save.
On (3)
You need to use Early Binding to get the benefit of intellisense. You are currently use late binding with oExcel = CreateObject("Excel.Application"). A commonly used approach is to write the code and get it working with early binding, then converting it to late binding for final code publication.
Conditional Compilation (see comment at bottom) can be used to switch between the two binding methods within the same code.
You should be saving the workbook. e.g. oBook.Save.
If you create a new file, you will need to use SaveAs with a valid filename in order to save it the first time.

QTP How to save images from webpages

I would like to know if it's possible to simulate the process: Right click on an image -> Click on "Save image as.." on the popup menu -> save the image in local.
I tried CaptureBitmap() function, but the result is just a screenshot taken by QTP, not the same image file obtained as the procedure above.
Are there other ways? Many thanks in advance.
Allen
I suppose it depends what you want to do. If you want to compare the bitmap then the CaptureBitmap options should work. If you want to compare the path to the image you can use Image("x").GetROProperty("src").
If you really want to save the src image file then unfortunately QTP doesn't supply a way to interact with the browser's context menu. You can try to use some third-party mechanism to download the image from the src URL (e.g. wget).
Edit: I just had another thought, I'm not at work so I can't verify that it will work but I'm pretty sure it will.
First cause the context menu to appear, in order to do this you have to change the replay mode to device and run a RightClick operation.
replayType = Setting.WebPackage("ReplayType") ' Store old replay mode
Setting.WebPackage("ReplayType") = 2 ' change to device replay mode
Browser("b").Page("p").Image("I").RightClick
Setting.WebPackage("ReplayType") = replayType ' Revert to old mode
Then send the letter v to the browser which will select the Save menu item (on both IE and Firefox) by using the device replay object
Set deviceReplay = CreateObject( “Mercury.DeviceReplay” )
deviceReplay.SendString "v"
Now interact with the save dialog as a usual Win32 control.
Moral: Never underestimate what QTP will let you do if you try hard enough

How can I programatically print a DataReport to a pdf file?

I'm updating old VB6 code to save its DataReports out to a PDF, rather than bringing up a print dialog.
I cannot simply write the PDF within the code (using a VB6 PDF library, etc.), since all our software already uses DataReports, and writing print code for each one would be tedious, at best. Currently, the process requires an employee to print the DataReport to a PDF print driver, naming it manually and saving it to where it needs to go. I need to automate this all, so that the name and location of the saved PDF can be specified programatically, rather than entered by hand.
The best solution would be if DataReport simply had a .SaveToPdf(filename) routine. Worst-case scenario, I see myself automating the process using SendKeys. However, this solution needs to work in an environment with multiple possible printers (so the PDF print driver might not be the default,) and on Windows XP, Vista, or 7.
I've fruitlessly tried Googling the problem, which returns only tutorials on how to do it by hand, much as we do now.
You might consider using a PDF Printer Driver that allows you to configure silent "printing" to a preset directory using auto-generated names.
For an example of such a product, see:
http://www.iteksoft.com/modules.php?op=modload&name=Sections&file=index&req=viewarticle&artid=21
I would create a dialog that lets the user enter the printer (driver) name, directory to save to, and a file naming guideline, then save that to either a local ini file or the registry. You would then need two print buttons / menus. One to print straight to the printer using the default (saved) settings, and one that opens the print window they see now so they can perform a custom print.
Remember an ellipsis on a menu item indicates additional dialogs, Print vs Print...
Just use Crystal Report Viewer Control and follow the steps:
Set objRpt = objApp.OpenReport("type report path and name")
objRpt.DiscardSavedData
dim filepath as string
filepath = report path & report filename
With objRpt
.ExportOptions.FormatType = crEFTPortableDocFormat
.ExportOptions.DestinationType = crEDTDiskFile
.ExportOptions.DiskFileName = 'filepath string goes here
.ExportOptions.PDFExportAllPages = True
.Export False
End With
Follow these steps and the export is done.

How to copy to clipboard using Access/VBA?

Using VBA inside Access2003/2007.
How to copy the contents of a string variable to the clipboard?
This site recommends a creating a zero length TextBox, copying the string to the TextBox, then running DoCmd.RunCommand acCmdCopy. Ugh. I mean, we may go down the route. But still. Ugh.
While the MS knowledgebase article shows us how to do it but it involves a number of Windows API calls. Yuk.
Are those the only two options?
VB 6 provides a Clipboard object that makes all of this extremely simple and convenient, but unfortunately that's not available from VBA.
If it were me, I'd go the API route. There's no reason to be scared of calling native APIs; the language provides you with the ability to do that for a reason.
However, a simpler alternative is to use the DataObject class, which is part of the Forms library. I would only recommend going this route if you are already using functionality from the Forms library in your app. Adding a reference to this library only to use the clipboard seems a bit silly.
For example, to place some text on the clipboard, you could use the following code:
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText "A string value"
clipboard.PutInClipboard
Or, to copy text from the clipboard into a string variable:
Dim clipboard As MSForms.DataObject
Dim strContents As String
Set clipboard = New MSForms.DataObject
clipboard.GetFromClipboard
strContents = clipboard.GetText
User Leigh Webber on the social.msdn.microsoft.com site posted VBA code implementing an easy-to-use clipboard interface that uses the Windows API:
http://social.msdn.microsoft.com/Forums/en/worddev/thread/ee9e0d28-0f1e-467f-8d1d-1a86b2db2878
You can get Leigh Webber's source code here
If this link doesn't go through, search for "A clipboard object for VBA" in the Office Dev Center > Microsoft Office for Developers Forums > Word for Developers section.
I created the two classes, ran his test cases, and it worked perfectly inside Outlook 2007 SP3 32-bit VBA under Windows 7 64-bit. It will most likely work for Access.
Tip: To rename classes, select the class in the VBA 'Project' window, then click 'View' on the menu bar and click 'Properties Window' (or just hit F4).
With his classes, this is what it takes to copy to/from the clipboard:
Dim myClipboard As New vbaClipboard ' Create clipboard
' Copy text to clipboard as ClipboardFormat TEXT (CF_TEXT)
myClipboard.SetClipboardText "Text to put in clipboard", "CF_TEXT"
' Retrieve clipboard text in CF_TEXT format (CF_TEXT = 1)
mytxt = myClipboard.GetClipboardText(1)
He also provides other functions for manipulating the clipboard.
It also overcomes 32KB MSForms_DataObject.SetText limitation - the main reason why SetText often fails. However, bear in mind that, unfortunatelly, I haven't found a reference on Microsoft recognizing this limitation.
-Jim
I couldn't figure out how to use the API using the first Google results. Fortunately a thread somewhere pointed me to this link:
http://access.mvps.org/access/api/api0049.htm
Which works nicely. :)
Easy TWO line code:
It's not so complicated, I don't understand why all solutions found in the net are so complicated.
Sub StoreData()
Set objCP = CreateObject("HtmlFile")
objCP.ParentWindow.ClipboardData.SetData "text", "Some text for clipboard"
End Sub
There are many examples listed here but none seem to cover the direct way of using the API that also works with Access and Excel 32 bit and 64 bit.
I don't want to steal anyone else's work so I'm pointing to an article that has a solution.
https://stackoverflow.com/a/35512118/1898524
Following up on David's idea, if you want to pass in an argument, it has to be double-quoted.
Public Sub SetClipboardText(ByVal Text As String)
Dim QuotedText As String
QuotedText = """" & Text & """"
Set HtmlFileObject = CreateObject("HtmlFile")
HtmlFileObject.ParentWindow.ClipboardData.SetData "text", Eval(QuotedText)
End Sub

Resources