Clever-Excel-Forum

Normale Version: Aktuellste PDF öffnen
Du siehst gerade eine vereinfachte Darstellung unserer Inhalte. Normale Ansicht mit richtiger Formatierung.
Hallo,

ich habe einen Code, der die aktuellste Excel Datei in einem angegebenen Ordner öffnet.

Code:
"Dim strTmpFileNew As String
     Dim strTmpFile As String
     Dim strPath As String
     Dim StrTyp As String
     Dim datTime As Date
     On Error GoTo Fin
    
     '---------------------------------------------------------------
     strPath = "DATEIPFAD"
      StrTyp = "*.xlsx"
     
    '----------------------------------------------------------------
    
     strTmpFile = Dir$(strPath & StrTyp)
     strTmpFileNew = strTmpFile
     datTime = FileDateTime(strPath & strTmpFile)
     Do While strTmpFile <> ""
         If datTime < FileDateTime(strPath & strTmpFile) Then
             datTime = FileDateTime(strPath & strTmpFile)
             strTmpFileNew = strTmpFile
         End If
         strTmpFile = Dir$()
     Loop
     If strTmpFileNew <> "" Then Workbooks.Open strPath & strTmpFileNew
Fin:
     If Err.Number <> 0 Then MsgBox "...………….", vbInformation, "......" "

Meine Frage ist nun, ob ich diesen Code auch für PDF's nutzen kann. Ich habe den StrTyp schon angepasst, allerdings weiß ich nicht was ich statt "Workbooks.Open" schreiben soll.
Oder ist das ganze nicht so einfach auf andere Dateien anwendbar.

MfG

Schmerich
Hallo, :19:

ganz oben (direkt nach Option Explicit) in dem Modul: :21:

Code:
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
Private Const SW_MAXIMIZE = 3

Und dann statt:

Code:
If strTmpFileNew <> "" Then Workbooks.Open strPath & strTmpFileNew


Das hier (getestet!):

Code:
If strTmpFileNew <> "" Then ShellExecute 0, "Open", strPath & strTmpFileNew, "", "", SW_MAXIMIZE
Danke erstmal für die Antwort. Was genau meinst du mit dem ersten Teil?

Code sieht jetzt so aus:


Code:
"Sub AKT()
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
Private Const SW_MAXIMIZE = 3
Dim strTmpFileNew As String
     Dim strTmpFile As String
     Dim strPath As String
     Dim StrTyp As String
     Dim datTime As Date
     On Error GoTo Fin
    
     '---------------------------------------------------------------
     strPath = "DATEIPFAD" ' Pfadabgabe anpassen!!!
      StrTyp = "*.pdf" 'Dateiendung anpassen!!!
     
    '----------------------------------------------------------------
    
     strTmpFile = Dir$(strPath & StrTyp)
     strTmpFileNew = strTmpFile
     datTime = FileDateTime(strPath & strTmpFile)
     Do While strTmpFile <> ""
         If datTime < FileDateTime(strPath & strTmpFile) Then
             datTime = FileDateTime(strPath & strTmpFile)
             strTmpFileNew = strTmpFile
         End If
         strTmpFile = Dir$()
     Loop
     If strTmpFileNew <> "" Then ShellExecute 0, "Open", strPath & strTmpFileNew, "", "", SW_MAXIMIZE
Fin:
     If Err.Number <> 0 Then MsgBox "...…………...", vbInformation, "....."
End Sub"
Hallo, :19:

nein, die API - Deklaration muss außerhalb!

Code:
Option Explicit
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
Private Const SW_MAXIMIZE = 3
Sub Akt()
    Dim strPath As String
    Dim StrTyp As String
    Dim datTime As Date
    '.........
    '.........
    '.........
    If strTmpFileNew <> "" Then ShellExecute 0, "Open", strPath & strTmpFileNew, "", "", SW_MAXIMIZE
Fin:
    If Err.Number <> 0 Then MsgBox "Error: " & Err.Number & " " & Err.Description
End Sub

Statt der "'........." eben dein Code.
Oder:

Code:
Sub M_snb()
    Shell "cmd /c " & "G:\OF\" & Split(CreateObject("wscript.shell").exec("cmd /c dir G:\OF\*.pdf /b/o-d").stdout.readall, vbCrLf)(0)
End Sub
Pfad 'G:\OF\ ' anpassen ( 2 x )
Sauber es funktioniert! Danke!!!