Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1700to1704
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

VBA Excel Login auf Webseite und Download und Save

VBA Excel Login auf Webseite und Download und Save
12.07.2019 21:51:26
leon8107
Hallo zusammen ich habe jetzt echt lang gesucht und weiß nicht wie ich das Problem lösen kann. Ich hoffe ihr könnt mir helfen. Was will ich erreichen? Ich will automatisiert per VBA durch Workbook_Open mithilfe von Excel VBA auf einer Webseite anmelden und dann die Seite wo der Download durchgeführt werden kann ansteuern. Download durchführen und Datei in einem bestimmten Ordner abspeichern. Ich nutze den Internet Explorer IE 11 und Excel 2016 (auf zwei Rechner eine 32Bit und eine 64 Bit). Die Windows 10 Version ist bei beiden Rechner 64 Bit. Bisher habe ich folgenden Erfolg: Ich kann mich auf einer Webseite anmelden (nicht ganz sauber, wenn man noch angemeldet ist im IE dann gibt es Fehlermeldung). Ich kann den Downloadlink aufrufen. Das Downloadfenster am unteren Rand erscheint. Ab hier an komme ich nicht weiter. Bei meinen aktuellen Codevarianten erfolgt dann eine Fehlermeldung: Fehler beim Kompilieren Typen unverträglich. Markiert wird die Function FindWindow. Folgende Referenzen sind für das Projekt gesetzt: Visual Basic For Application; Microsoft Excel 14.0 Object Libary; OLE Autmation; Microsoft Office 14.0 Object Libary, Microsoft Internet Control Microsoft XML, v6.0; Microsoft HTL Object Libary, UIAutomationClient
Folgenden Code habe ich bisher verwendet:
1. Variante:

Option Explicit
Private Sub Workbook_Open()
Dim IEApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim sSource, sTable As String
Dim saveInFolder As String, saveAsFilename As String
Dim dlWb As Workbook
'create a new instance of ie
Set IEApp = New InternetExplorer
IEApp.Visible = True
'assume we?re not logged in and just go directly to the login page
IEApp.Navigate "https://login.test.de/"
Do While IEApp.Busy: DoEvents: Loop
Do Until IEApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = IEApp.Document
'fill in the login form ? View Source from your browser to get the control names
With ieDoc.forms(0)
.partnernummer.Value = "XXXXX"
.kennwort.Value = "YYYYYY"
.submit
End With
Do While IEApp.Busy: DoEvents: Loop
Do Until IEApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
IEApp.Navigate "https://partnerlogin.test/download.php?datom_von=01.01.2018&datum_bis=31.12. _
2019"
Do While IEApp.Busy: DoEvents: Loop
Do Until IEApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
saveInFolder = "C:\Users\name\Desktop\Test2"
saveAsFilename = "Downloaded Excel data.xls"
Download_File saveInFolder, saveAsFilename
Set dlWb = Workbooks.Open(saveInFolder & saveAsFilename)
'Here, copy data from downloaded workbook to destination workbook
dlWb.Close False
End Sub
Private Sub IE_Navigate_and_Download(URL As String, startDate As Date, saveInFolder As String,   _
_
saveAsFilename As String)
Dim IE As Object
Dim dateInput As Object
'Use existing IE window or open a new IE window
Set IE = Get_IE_Window()
If IE Is Nothing Then
Set IE = CreateObject("InternetExplorer.Application")
End If
With IE
SetForegroundWindow .hwnd
.Visible = True
.Navigate URL
While .Busy Or .ReadyState  4: DoEvents: Wend
'Populate HTML text box with specified date and refresh page
Set dateInput = .Document.forms(0).elements("txtStartDate")
If Not dateInput Is Nothing Then
dateInput.Value = Format(startDate, "dd-mm-yyyy")
.Navigate "javascript:__doPostBack('txtStartDate','')"
While .Busy Or .ReadyState  READYSTATE_COMPLETE: DoEvents: Wend
Else
MsgBox "txtStartDate HTML element not found - unable to set specified date"
End If
'Click the download ncr link
.Navigate "javascript:__doPostBack('downloadncr','')"
End With
Download_File saveInFolder, saveAsFilename
SetForegroundWindow Application.hwnd
End Sub
Private Sub Download_File(saveInFolder As String, saveAsFilename As String)
'Handles the whole IE download file sequence
Dim fileExists As Boolean
'File Download window, with Open, Save and Cancel buttons - click Save
File_Download_Click_Save
'Save As window - set full filename
Save_As_Set_Filename saveInFolder, saveAsFilename
'Save As window - click Save and replace file if the ' already exists' popup window appears
fileExists = Save_As_Click_Save
If fileExists Then
File_Already_Exists Replace:=True
End If
'Optional Download complete window, with Open, Open Folder and Close buttons - click Close,  _
_
or time out if window is not present
'This window also has a checkbox 'Close this dialog box when download completes'.  This  _
checkbox setting is controlled
'by 'Notify when downloads complete' in IE advanced options.  If this checkbox is ticked,   _
_
the following line is not required,
'though it shouldn't matter if it is left in as the routine will time out if the window isn' _
_
t found.
Download_complete_Click_Close
Debug.Print "Download finished"
End Sub
Private Sub File_Download_Click_Save()
Dim hwnd As Long
Dim timeout As Date
Debug.Print "File_Download_Click_Save"
'Find the File Download window, waiting a maximum of 30 seconds for it to appear
timeout = Now + TimeValue("00:00:30")
Do
hwnd = FindWindow("#32770", "File Download")
DoEvents
Sleep 200
Loop Until hwnd Or Now > timeout
Debug.Print "   File Download window "; Hex(hwnd)
If hwnd Then
'Find the child Save button
hwnd = FindWindowEx(hwnd, 0, "Button", "&Save")
Debug.Print "   Save button "; Hex(hwnd)
End If
If hwnd Then
'Click the Save button
SetForegroundWindow hwnd
Sleep 600  'this sleep is required and 600 milliseconds seems to be the minimum that  _
works
SendMessage hwnd, BM_CLICK, 0, 0
End If
End Sub
Private Sub Save_As_Set_Filename(folder As String, filename As String)
'Populate the 'File name:' edit window in the Save As dialogue with the specified folder  _
and/or filename.
'If folder = "" a folder path is not prepended to the filename and therefore the default  _
save folder is used.
'If filename = "" the default file name (already populated) is used.
'The Save As window has the following child window hierarchy:
'   "Save As", #32770 Dialog
'       "FileName", ComboBoxEx32        (FileName is the default File name value in  _
combobox)
'           "", ComboBox
'               "FileName", Edit        (FileName is the default File name value in  _
combobox's edit box)
Dim hwnd As Long
Dim timeout As Date
Dim fullFilename As String
Debug.Print "Save_As_Set_Filename"
Debug.Print "folder   = " & folder
Debug.Print "filename = " & filename
'Find the Save As window, waiting a maximum of 10 seconds for it to appear
timeout = Now + TimeValue("00:00:10")
Do
hwnd = FindWindow("#32770", "Save As")
DoEvents
Sleep 200
Loop Until hwnd Or Now > timeout
If hwnd Then
SetForegroundWindow hwnd
'Find the child ComboBoxEx32 window
hwnd = FindWindowEx(hwnd, 0, "ComboBoxEx32", vbNullString)
Debug.Print "   ComboBoxEx32 "; Hex(hwnd)
End If
If hwnd Then
'Find the child ComboBox window
hwnd = FindWindowEx(hwnd, 0, "ComboBox", "")
Debug.Print "   ComboBox "; Hex(hwnd)
End If
If hwnd Then
SetForegroundWindow hwnd
'Find the child Edit window
hwnd = FindWindowEx(hwnd, 0, "Edit", "")
Debug.Print "   Edit "; Hex(hwnd)
End If
If hwnd Then
If filename = "" Then
'Get default filename (already populated in Edit window)
filename = Get_Window_Text(hwnd)
End If
'If specified, ensure folder ends with \
If folder  "" And Right(folder, 1)  "\" Then folder = folder & "\"
fullFilename = folder & filename
Debug.Print "Full filename " & fullFilename
'Populate the Edit window with the full file name
Sleep 200
SetForegroundWindow hwnd
SendMessageByString hwnd, WM_SETTEXT, Len(fullFilename), fullFilename
End If
End Sub
Private Function Get_Window_Text(hwnd As Long) As String
'Returns the text in the specified window
Dim Buffer As String
Dim length As Long
Dim result As Long
SetForegroundWindow hwnd
length = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)
Buffer = Space(length + 1)  '+1 for the null terminator
result = SendMessage(hwnd, WM_GETTEXT, Len(Buffer), ByVal Buffer)
Debug.Print "Edit File name = " & Left(Buffer, length)
Debug.Print "        length = " & length
Get_Window_Text = Left(Buffer, length)
End Function
Private Function Save_As_Click_Save() As Boolean
'Click the Save button in the Save As dialogue, returning True if the ' already exists'
'window appears, otherwise False
Dim hwnd As Long, hWndSaveAs As Long
Dim timeout As Date
Debug.Print "Save_As_Click_Save"
'Find the Save As window, waiting a maximum of 10 seconds for it to appear
timeout = Now + TimeValue("00:00:10")
Do
hwnd = FindWindow(vbNullString, "Save As")
hWndSaveAs = hwnd
DoEvents
Sleep 200
Loop Until hwnd Or Now > timeout
Debug.Print "   Save As window "; Hex(hwnd)
If hwnd Then
SetForegroundWindow hwnd
'Get the child Save button
hwnd = FindWindowEx(hwnd, 0, "Button", "&Save")
Debug.Print "   Save button "; Hex(hwnd)
End If
If hwnd Then
'Click the Save button
'This can cause the ' already exists' popup window to be displayed.  The button click   _
_
is sent with PostMessage
'because SendMessage doesn't return until the window is closed (by clicking Yes or No)
'PostMessage -  _
http://msdn. _
microsoft.com/en-us/library/ms644944%28v=VS.85%29.aspx
'Places (posts) a message in the message queue associated with the thread that created   _
_
the specified window and returns
'without waiting for the thread to process the message.
'SendMessage - http:/ _
_
/msdn.microsoft.com/en-us/library/ms644950%28VS.85%29.aspx
'The SendMessage function calls the window procedure for the specified window and does   _
_
not return until the
'window procedure has processed the message.
'SendMessage hWnd, BM_CLICK, 0, 0
Sleep 100
SetForegroundWindow hwnd
PostMessage hwnd, BM_CLICK, 0, 0
Debug.Print "   Clicked Save button"
End If
If hwnd Then
'Set function return value depending on whether or not the ' already exists' popup  _
window exists
'Note - the popup window is a modal dialogue box and a child of the main Save As window. _
_
Both windows have the
'same class (#32770) and caption (Save As).  This may present a problem in finding the   _
_
popup window, however in all tests
'FindWindow has correctly found the popup window.  Therefore the additional  _
FindWindowEx, which looks for the Yes button
'as a child of the popup window is not necessary
Sleep 500
hwnd = FindWindow("#32770", "Save As")
'This alternative FindWindowEx call, which looks for the popup window as a child of the  _
_
main Save As window, doesn't find it,
'returning 0 for hWnd
'hWnd = FindWindowEx(hWndSaveAs, 0, "#32770", "Save As")
Debug.Print "   Save As -  already exists popup window "; Hex(hwnd)
'hWnd = FindWindowEx(hWnd, 0, "Button", "&Yes")
If hwnd Then
Save_As_Click_Save = True
Else
Save_As_Click_Save = False
End If
End If
End Function
Private Sub File_Already_Exists(Replace As Boolean)
'Click Yes or No in the ' already exists. Do you want to replace it?' window
Dim hwnd As Long
Debug.Print "File_Already_Exists("; Replace; ")"
hwnd = FindWindow("#32770", "Save As")
Debug.Print "   Save As popup window "; Hex(hwnd)
If hwnd Then
If Replace Then
hwnd = FindWindowEx(hwnd, 0, "Button", "&Yes")
Debug.Print "   Yes button "; Hex(hwnd)
Else
hwnd = FindWindowEx(hwnd, 0, "Button", "&No")
Debug.Print "   No button "; Hex(hwnd)
End If
End If
If hwnd Then
SendMessage hwnd, BM_CLICK, 0, 0
End If
End Sub
Private Sub Download_complete_Click_Close()
Dim hwnd As Long
Dim timeout As Date
Debug.Print "Download_complete_Click_Close"
'Find the Download complete window, waiting a maximum of 30 seconds for it to appear.   _
Timeout value is
'dependent on the size of the download, so make it longer for larger files.
timeout = Now + TimeValue("00:00:30")
Do
hwnd = FindWindow("#32770", "Download complete")
DoEvents
Sleep 200
Loop Until hwnd Or Now > timeout
Debug.Print "   Download complete window "; Hex(hwnd)
If hwnd Then
'Find the child Close button
hwnd = FindWindowEx(hwnd, 0, "Button", "Close")
Debug.Print "   Close button "; Hex(hwnd)
End If
If hwnd Then
'Click the Close button
SetForegroundWindow hwnd
Sleep 600  'this sleep is required and 600 milliseconds seems to be the minimum that  _
works
SendMessage hwnd, BM_CLICK, 0, 0
End If
End Sub
Private Function Get_IE_Window() As Object
'Look for an IE browser window and, if found, return that browser as an InternetExplorer  _
object.  Otherwise return Nothing
Dim Shell As Object
Dim IE As Object
Dim i As Variant 'Integer
Set Shell = CreateObject("Shell.Application")
i = 0
Set Get_IE_Window = Nothing
While i 

hierzu benötigt man ein zweites Modul modWindowsAPI

Option Explicit
Public Declare PtrSafe Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As LongPtr)
Public Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function FindWindowEx Lib "USER32" Alias "FindWindowExA" _
(ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As  _
String) As LongPtr
Public Declare PtrSafe Function SetForegroundWindow Lib "USER32" _
(ByVal hwnd As LongPtr) As Long
Public Declare PtrSafe Function SendMessage Lib "USER32" Alias "SendMessageA" _
(ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As  _
LongPtr
Public Declare PtrSafe Function SendMessageByString Lib "USER32" Alias "SendMessageA" _
(ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As String) _
_
As LongPtr
Private Declare PtrSafe Function PostMessage Lib "USER32" Alias "PostMessageA" _
(ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As  _
LongPtr) As Long
Public Declare PtrSafe Sub keybd_event Lib "USER32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As LongPtr, ByVal dwExtraInfo As  _
LongPtr)
Public Const BM_CLICK = &HF5
Public Const WM_SETTEXT = &HC
Public Const WM_GETTEXT = &HD
Public Const WM_GETTEXTLENGTH = &HE
Public Const VK_KEYDOWN = &H0
Public Const VK_KEYUP = &H2
Public Const VK_CONTROL = &H11

7
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: VBA Excel Login auf Webseite und Download und Save
13.07.2019 17:26:37
Zwenn
Hallo leon8107,
über 500 Zeilen Code (wenn auch viele Freizeilen dabei sind), aber keine valide URL. Es ist immer etwas mühsam eine Lösung für solche Probleme als "Trockenübung" zu erarbeiten. Ist die URL so geheim oder kann sich da nicht jeder einfach einen Account erstellen?
Wenn ich es richtig verstanden habe, willst Du Downloads über den "Save Download" Button des IE vornehmen, der unten am Fensterrand erscheint, wenn Du auf den Downloadlink klickst. Ich habe mir Deinen gesamten Code nicht angesehen, nehme aber an, das geht wesentlich kürzer. Ich habe dafür vor einiger Zeit mal angefangen etwas auszuarbeiten.
Deshalb weiß ich, dass der Save Button über Application.SendKeys ("%{S}") ausgelöst werden kann, um den Download ohne weitere Abfrage in den Standard Download Ordner durchzuführen. Achte darauf, dass es das große S ist!
Allerdings gibt es zwei unterschiedliche Möglichkeiten für Downloadordner. Einmal den Standard Windows Download Ordner und einmal einen Ordner, den man im IE selbst gesetzt hat. Man kann aus der Regestry auslesen, in welchem man nach dem Download die runtergeladene Datei findet. Dazu stelle ich Dir mal folgende zwei Funktionen zur Verfügung:
(Wie auch in Deinem Code, musst Du Forensoftware bedingte Zeilenumbrüche in der IDE wieder korrigieren.)

Function ieSpeicherPfad() As String
'Diese Funktion liest den Downloadpfad aus, unter dem der Internet Explorer standardmäßig  _
Downloads abspeichert
'Es wird entweder ein selbst hinterlegter Speicherort verwendet
'(Im IE11: Strg + j [Downloads anzeigen] -> Optionen [unten links] -> im Dialog Pfad setzen)
'oder der Windows Standardpfad für Downloads, wenn kein eigener gesetzt wurde
Dim speicherPfadInRegestry As String
'Prüfen ob ein eigener Downloadpfad innerhalb des Internetexplorers gesetzt wurde
'Downloadverzeichnis des IE, wenn anders als Standard System Download Ordner
'Schlüssel liegt in echt in:
'Computer\HKEY_USERS\NUMMER DES BENUTZERKONTOS\Software\Microsoft\Internet Explorer\ _
Main
'Windows interne Nummern für Benutzerkonten gibt es mehrere. Diese Funktion schaut
'über den unten verwendeten RegestryPfad im aktuell angemeldeten Konto nach
If RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Default Download  _
Directory")  "" Then
speicherPfadInRegestry = RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Internet  _
Explorer\Main\Default Download Directory")
Else
'Standard Windows Download Ordner (Bei mir systemweit auf Laufwerk E:\Ablage gelegt)
speicherPfadInRegestry = RegKeyRead("HKCU\Software\Microsoft\Windows\CurrentVersion\ _
Explorer\Shell Folders\{374DE290-123F-4565-9164-39C4925E467B}")
End If
'Prüfen auf abschließenden Backslash und setzen, falls
'nicht vorhanden, um einen ganzen Pfad zu bilden,
'dem einfach ein Dateiname angehängt werden kann
If Right(speicherPfadInRegestry, 1)  "\" Then
speicherPfadInRegestry = speicherPfadInRegestry & "\"
End If
ieSpeicherPfad = speicherPfadInRegestry
'Als Hinweis:
'Erster eingerichteter Download Ordner von Windows (Wird bei mir nicht verwendet, da  _
umgebogen auf Laufwerk E:\Ablage)
'Ist für die Zwecke dieses Makros nicht sinnvoll verwendbar
'MsgBox Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Downloads"
End Function

Function RegKeyRead(i_RegKey As String) As String
'Quelle: https://stackoverflow.com/questions/27487935/determine-where-browser-is-going-to- _
download-files-excel-and-vba
Dim myWS As Object
On Error Resume Next
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'read key from registry
RegKeyRead = myWS.RegRead(i_RegKey)
End Function

An Deiner Stelle würde ich übrigens LateBinding verwenden, statt die ganzen Verweise in Excel auf jedem Rechner sicher zu stellen. Mit LateBinding laufen solche Makros auf jedem beliebigen Rechner, ohne dass man sich noch um Verweise kümmern muss. Dann geht zwar der Luxus von IntelliSense für die entsprechenden Objekte verloren, aber das wiegt den Universalnutzen auf beliebigen Rechnern in diesem Fall mehr als auf finde ich.
Tja, schätze Du solltest Dein Projekt nochmal überarbeiten ;-) Viel Glück dabei,
Zwenn
Anzeige
AW: VBA Excel Login auf Webseite und Download und Save
13.07.2019 20:47:31
leon8107
Hallo Zwenn ich probiere es gleich mal aus und gebe Feedback.
Auf jedenfall erstmal vielen Dank!
AW: VBA Excel Login auf Webseite und Download und Save
13.07.2019 21:13:47
leon8107
Hallo Zwenn ich bins nochmal, kurz nochmal zu deinen Fragen. Ja so einfach kannst du dich auf der betreffenden Webseite nicht anmelden. Da musste schon einen Partnervertrag abschließen ;). Der Teil des Codes funktioniert ja auch ganz gut. Nach dem die Anmeldung erolgt ist wird eiinfachder Downloadlink aufgerufen. Dann muss nur noch die Datei gespeichert werden.
Hast du Tipps wie ich das "Application.SendKeys ("%{S}")" umsetzen kann. Links zu dem Thema oder andere Hinweise? Meine VBA Kenntnisse sind noch ausbaubar ^^
Anzeige
AW: VBA Excel Login auf Webseite und Download und Save
14.07.2019 15:28:59
Zwenn
Hallo Leon,
da gibt es eigentlich nicht viel zu erklären. Mit SendKeys() wird die in den Klammern angegebene Tastenkombination an das Programmfenster gesendet, dass grade den Fokus hat. Du musst also dafür sorgen, dass der IE den Fokus hat, sonst passiert nicht was Du willst. Bei Verwendung von SendKeys() muss der IE übrigens sichtbar sein. Sonst kann er den Fokus nicht bekommen.
Hier mal der Ausschnitt aus meinem Makro, wie Du dem richtigen Fenster den Fokus geben kannst:

'CSV in den Standard-Download-Ordner laden
'Das ist der Download-Ordner des Systems
'Um Sendkeys richtig zu nutzen, muss die
'Application, an die gesendet werden soll
'aktiviert werden.
'Das passiert über den Namen in der Titelzeile
'Diesen kann man finden, indem man über die
'Shell alle offenen Fenster durchgeht und
'zunächst nach der Anwendung sucht, also dem
'Internet Explorer und dann schaut, ob die
'richtige URL darin geöffnet ist
Set objShell = CreateObject("Shell.Application")
For Each win In objShell.Windows
If InStr(1, UCase(win.FullName), "IEXPLORE") > 0 Then
If win.document.Location = url Then
AppActivate win.document.Title
Application.SendKeys ("%{S}")
End If
End If
Next
SendKeys() verwende ich äußerst ungern, in diesem Fall ist das aber die berühmte Ausnahme der Regel. SendKeys() haut die gewünschten Tastenanschläge ohne irgend eine Prüfung raus. Im schlechtesten Fall macht man sich damit etwas kaputt, wenn die falsche Anwendung den Fokus hat:
Was Application.SendKeys ("%{S}") wegschickt ist der Shortcut Alt + S für das direkte Speichern der Datei im IE. Du versuchst über den Download Dialog zu gehen. Ich weiß gar nicht ob das funktioniert, aber Du kannst Dir das mit dieser Methode auf jeden Fall viel einfacher machen. Bevor Du SendKeys() auslöst, musst Du nach dem Anklicken des DownloadLinks sicher stellen, dass der IE den SpeichernButton am unteren Fensterrand eingeblendet hat. Du hast Sleep() eingebunden. Ich mache das immer über Application.Wait(), weil das auch ohne API zur Verfügung steht. Damit gehen aber als kleinste Zeiteinheit nur Sekunden. Was aber egal sein sollte:
Application.Wait (Now + TimeSerial(0, 0, 3))
Hier kannst Du Dich über SendKeys() informieren:
http://docs.microsoft.com/de-de/office/vba/api/excel.application.sendkeys
Um dann festzustellen, wo die Datei nach dem Download liegt, habe ich Dir die beiden Funktionen im letzten Posting zur Verfügung gestellt. Die Kommentare im Quellcode sollten ausreichend erklären, wie die arbeiten.
Folgenden Teil habe ich damals nicht ausgearbeitet, sondern nur als Idee entwickelt:
Du musst vor dem Download feststellen, in welchem Ordner der Download gespeichert wird. Da können nämlich schon andere Dateien drin liegen und Du musst Deine runtergeladene eindeutig identifizieren können.
Also musst Du Dir vor dem Download alle Dateien merken, die schon vorhanden sind. Dann löst Du den Download aus und überwachst den Ordner in einer Schleife. Sobald die Anzahl der Dateien um 1 gestiegen ist, hast Du Deinen Download. Nun kannst Du alle bekannten Dateien aus einer zweiten jetzt erstellten Dateiliste durch Vergleich mit der ersten Liste rauswerfen. Der Name der übrig bleibt ist die runtergeladene Datei. Diese kannst Du nun mit Name() einfach an Deinen Wunschort verschieben und ggf. dabei auch direkt in Deinen Wunschnamen umbenennen.
Viele Grüße,
Zwenn
Anzeige
AW: VBA Excel Login auf Webseite und Download und Save
14.07.2019 19:58:13
leon8107
Hallo Swenn das hatte ich so auch tatsächlich so schon umgesetzt. Mein Problem ist dann tatsächlich noch das die Datei mit dem richtigen Namen im richtigen Ordner landet. Auch soll der Code mit Workbook_Open () starten. Hier gibt es noch ein Fehlermeldung: ungültiges Argument bei dieser Codezeile: AppActivate win.Document.Title
Jetzt aber noch zu deinem Vorschlag die Datei in den richtigen Ordner zu kopieren. Ich glaub das geht an der Stelle spätestens über meinen Horizont hinaus. Ich glaube das wäre dann irgendwann auch unpraktisch wenn ich die Datei mehrfach am Tag herunterlade, spätestens dann wird es schwierig mit der Erkennug der richtigen Datei.
Ich hätte noch eine weitere Idee: Ist es möglich anstatt die Datei via "Application.SendKeys" zu speichern erstmal zu öffnen und wenn sie dann geöffnet ist via Excel Makro zu umzubenennen und in den richtigen Ordner zu speichern. Öffnen kann ich die Datei beim Download Vorgang via Alt (gehalten) und kurz hintereinander zweimal ff drücken. Wie stelle ich das in den Application.SendKeys dar?
Anzeige
AW: VBA Excel Login auf Webseite und Download und Save
14.07.2019 20:36:47
leon8107
Somit kann ich Öffnen "Application.SendKeys ("%({f}{f})")"
AW: VBA Excel Login auf Webseite und Download und Save
20.07.2019 20:05:29
leon8107
Hallo zusammen,
mein Probleme mit dem Code sind leider noch nicht gelöst.
Mit der Workbook_open funktioniert das nicht: AppActivate win.Document.Title
Was muss ich machen damit das auch mit Workbook_open funktiniert.
Ich hatte vermutet das die Seite noch nicht geladen war. Aber irgendwas stimmmt nicht auch wenn die Seite geladen ist funktioniert der Code trotzdem nicht. Nur wenn ich den Code direkt im VBA Projekt starte da passt es.

32 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige