HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Auto-upload tool

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
autouploadtool

Problem

I created an automatic upload tool, which is working great (most of the time). As I want to improve the code I have several questions:

  • Is it possible to make the iexplorer process smoother? I don't like it that people can see the "sendkeys" actions. Is there possibly way to run the process in the background/lay a picture over the process? In addition to that, is there a way to block the user's input for some seconds? I know the opportunity of the blockinput command, but unfortunately it doesn't work.



  • Is it possible to close the browsers site (only the one, which was opened for the upload!) and return to Excel?



```
Sub speichern_unter()
Application.DisplayAlerts = False
Dim str_pfad As String
str_pfad = Environ("UserProfile") & "\Desktop\" 'Die Datei wird auf den Desktop gespeichert
Rem MsgBox str_pfad
ActiveWorkbook.SaveAs str_pfad & ActiveSheet.Range("m1").Value & ".xlsm" 'Diese Zelle gibt den Tabellennamen vor

'Zur Sicherheit wird eine zusätzliche Backup-Datei an eine E-Mail verschickt (optional)
MsgBox "Eine Backup-Datei wird erstellt und per Mail verschickt. Bitte bestätigen Sie im nachfolgenden Fenster mit 'erteilen'", , "OK"
ActiveWorkbook.SendMail "###@###.de", "###Dateiname###"
Application.DisplayAlerts = True

'Ab hier beginnt der Upload-Vorgang
MsgBox "Die Datei wird nun ins ### geladen. Dies kann einige Sekunden dauern! Bitte beachten Sie, dass Sie hierfür im ### eingeloggt sein müssen!", , "OK"
MsgBox "Berühren Sie während des Vorganges nicht die Maus oder Tastatur! (Dauer ca. 30 Sekunden)", , "OK"
Dim appIE As Object
Set appIE = CreateObject("InternetExplorer.Application")
'appIE.Visible = False
appIE.navigate "https://###/docs/DOC-257603/upload#" 'Link zur "Bearbeiten" Seite im TSN
While appIE.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:05"))
SendKeys "+{TAB}", True
SendKeys "{ENTER}"
Application.Wait (Now + TimeValue("0:00:04"))
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{ENTER}"
Appl

Solution

There's a huge problem with using SendKeys to do anything. The very second the target UI changes at all, your code is broken. As was mentioned in the comments, the right way to do this is by POSTing to the URL.

  • Use a tool like Telerik Fiddler to sniff out the contents of the http request that occurs when you click the Submit button on the target page.



  • Add a reference to the WinHttpRequest library.



-
Use the library to POST to the target URL.

const url As String = "https://###/docs/DOC-257603/upload#"

Dim httpRequest = New WinHttpRequest
httpRequest.Open "POST", url, False
httpRequest.SetRequestHeader "headerName", "and header values you sniffed out"
httpRequest.Send "content based on request you spied on"

Code Snippets

const url As String = "https://###/docs/DOC-257603/upload#"

Dim httpRequest = New WinHttpRequest
httpRequest.Open "POST", url, False
httpRequest.SetRequestHeader "headerName", "and header values you sniffed out"
httpRequest.Send "content based on request you spied on"

Context

StackExchange Code Review Q#129454, answer score: 6

Revisions (0)

No revisions yet.