Anzeige
Archiv - Navigation
1868to1872
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

Probleme mit VBA und OneDrive

Probleme mit VBA und OneDrive
16.02.2022 14:55:04
Stefan
Hallo zusammen,
ich hoffe ihr könnt helfen. Unser VBA Excel Arbeitsmappe hat seither funktioniert. Ich bin der Meinung, seit unsere Firma unsere Dokumente mit OneDrive sichert, funktioniert die Mappe nicht mehr. Nun zum Problem.
1) Die Arbeitsmappe wird durch Drücken eines Buttons gespeichert und dann eine pdf auf dem User Desktop erstellt.
Mittlerweile kommt der Fehler:
Error No 1004. Das dokument wurde nicht gespeichert. Das Dokument ist möglicherweise geöffnet, oder beim Speichern ist ein Fehler aufgetreten.
Der Desktop wird durch OneDrive gesichert/synchronisiert.
Kann ich den Code entsprechend OneDrive tauglich gestalten?

Sub create_pdf()
Dim c As Comment
For Each c In ActiveSheet.Comments
c.Visible = False
Next
ActiveWorkbook.Save 'letzten Stand speichern
On Error GoTo Fehler 'siehe Fehlerbehandlung unten
Dim pfad As String
Dim name As String
If Sheets("Tabelle3").Visible = xlSheetVisible Then
Sheets(Array("Tabelle1", "Tabelle2", "Tabelle3")).Select
Else
Sheets(Array("Tabelle1", "Tabelle2")).Select
End If
name = Range("E3").Value & Range("J3").Value
pfad = Environ("UserProfile") & "\Desktop\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
pfad & name & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox Sheets("Packaging Instruction").Range("E3").Value & Sheets("Packaging Instruction").Range("J3").Value & " " & "saved on desktop.", vbInformation, "Information"
End Sub

14
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Probleme mit VBA und OneDrive
16.02.2022 15:00:40
Nepumuk
Hallo Stefan,
so:

pfad = Environ("OneDrive") & "\Desktop\"
Gruß
Nepumuk
AW: Probleme mit VBA und OneDrive
16.02.2022 15:28:08
Stefan
Danke Nepumuk. Da mehrere User diese Arbeitsmappe verwenden und (noch) nicht alle den Desktop mit OneDrive sichern; funktioniert das dann trotzdem?
AW: Probleme mit VBA und OneDrive
16.02.2022 16:10:07
Nepumuk
Hallo Stefan,
dann so:

Option Explicit
Private Declare PtrSafe Function SHGetPathFromIDListA Lib "shell32.dll" ( _
ByVal pidl As LongPtr, _
ByVal pszPath As String) As Long
Private Declare PtrSafe Function SHGetSpecialFolderLocation Lib "shell32.dll" ( _
ByVal hwndOwner As LongPtr, _
ByVal nFolder As Long, _
pidl As ITEMIDLIST) As Long
Private Type ITEMID
cb As LongPtr
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As ITEMID
End Type
Private Const R_DESKTOP As Long = &H10
Private Const NO_ERROR As Long = 0&
Private Const MAX_PATH As Long = 260&
Private Function GetPath(ByVal pvlngFolderType As Long) As String
Dim lngResult As Long
Dim strBuffer As String
Dim udtIDL As ITEMIDLIST
lngResult = SHGetSpecialFolderLocation(Application.Hwnd, pvlngFolderType, udtIDL)
If lngResult = NO_ERROR Then
strBuffer = Space$(MAX_PATH)
lngResult = SHGetPathFromIDListA(ByVal udtIDL.mkid.cb, ByVal strBuffer)
If lngResult = 1 Then GetPath = Left$(strBuffer, InStr(1, strBuffer, vbNullChar) - 1)
End If
End Function
Public Sub test()
Dim strPath As String
strPath = GetPath(R_DESKTOP)
MsgBox strPath & "\"
End Sub
Gruß
Nepumuk
Anzeige
AW: Probleme mit VBA und OneDrive
16.02.2022 16:40:53
Nepumuk
Hallo Stefan,
ich bin mal die Funktion mit F8 durchgegangen und habe dabei festgestellt, dass der Type ITEMID einen LongLong enthält. Daher:

Option Explicit
Private Declare PtrSafe Function SHGetPathFromIDListA Lib "shell32.dll" ( _
ByVal pidl As LongLong, _
ByVal pszPath As String) As Long
Private Declare PtrSafe Function SHGetSpecialFolderLocation Lib "shell32.dll" ( _
ByVal hwndOwner As LongPtr, _
ByVal nFolder As Long, _
ByRef pidl As ITEMIDLIST) As Long
Private Type ITEMID
cb As LongLong
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As ITEMID
End Type
Private Const CSIDL_DESKTOP As Long = &H0
Private Const NO_ERROR As Long = 0&
Private Const MAX_PATH As Long = 260&
Private Function GetPath(ByVal pvlngFolderType As Long) As String
Dim lngResult As Long
Dim strBuffer As String
Dim udtIDL As ITEMIDLIST
lngResult = SHGetSpecialFolderLocation(Application.Hwnd, pvlngFolderType, udtIDL)
If lngResult = NO_ERROR Then
strBuffer = Space$(MAX_PATH)
lngResult = SHGetPathFromIDListA(ByVal udtIDL.mkid.cb, ByVal strBuffer)
If lngResult = 1 Then GetPath = Left$(strBuffer, InStr(1, strBuffer, vbNullChar) - 1)
End If
End Function
Public Sub test()
Dim strPath As String
strPath = GetPath(CSIDL_DESKTOP)
MsgBox strPath & "\"
End Sub
Gruß
Nepumuk
Anzeige
AW: Probleme mit VBA und OneDrive
17.02.2022 07:16:23
Stefan
Hallo Nepumuk,
vielen Dank. Gibt's da evtl auch die Möglichkeit, den Code so umzuschreiben, dass der User selbst bestimmt wo er das PDF abspeichert?
Also das ein "speichern unter" Fenster aufgeht. Gibt es so evtl keine Probleme ob OneDrive oder nicht ...!?
AW: Probleme mit VBA und OneDrive
17.02.2022 07:39:19
Nepumuk
Hallo Stefan,
dann so:

Option Explicit
Private Declare PtrSafe Function SHGetPathFromIDListA Lib "shell32.dll" ( _
ByVal pidl As LongLong, _
ByVal pszPath As String) As Long
Private Declare PtrSafe Function SHGetSpecialFolderLocation Lib "shell32.dll" ( _
ByVal hwndOwner As LongPtr, _
ByVal nFolder As Long, _
ByRef pidl As ITEMIDLIST) As Long
Private Type ITEMID
cb As LongLong
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As ITEMID
End Type
Private Const CSIDL_DESKTOP As Long = &H0
Private Const NO_ERROR As Long = 0&
Private Const MAX_PATH As Long = 260&
Public Sub Create_PDF()
Dim strPath As String
Dim objComment As Comment
Dim objFileDialog As FileDialog
For Each objComment In ActiveSheet.Comments
objComment.Visible = False
Next
ThisWorkbook.Save 'letzten Stand speichern
If Sheets("Tabelle3").Visible = xlSheetVisible Then
Sheets(Array("Tabelle1", "Tabelle2", "Tabelle3")).Select
Else
Sheets(Array("Tabelle1", "Tabelle2")).Select
End If
strPath = GetPath(CSIDL_DESKTOP) & "\" & Range("E3").Text & Range("J3").Text
Set objFileDialog = Application.FileDialog(msoFileDialogSaveAs)
With objFileDialog
.FilterIndex = 26
.InitialFileName = strPath
If .Show Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=.SelectedItems(1), _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
MsgBox Sheets("Packaging Instruction").Range("E3").Value & _
Sheets("Packaging Instruction").Range("J3").Value & _
" saved on desktop.", vbInformation, "Information"
End If
End With
Set objFileDialog = Nothing
End Sub
Private Function GetPath(ByVal pvlngFolderType As Long) As String
Dim lngResult As Long
Dim strBuffer As String
Dim udtIDL As ITEMIDLIST
lngResult = SHGetSpecialFolderLocation(Application.Hwnd, pvlngFolderType, udtIDL)
If lngResult = NO_ERROR Then
strBuffer = Space$(MAX_PATH)
lngResult = SHGetPathFromIDListA(ByVal udtIDL.mkid.cb, ByVal strBuffer)
If lngResult = 1 Then GetPath = Left$(strBuffer, InStr(1, strBuffer, vbNullChar) - 1)
End If
End Function
Gruß
Nepumuk
Anzeige
AW: Probleme mit VBA und OneDrive
17.02.2022 10:37:21
Stefan
Danke für dein Mühe.
Alles in ein Modul kopieren, richtig?
Leider kommt dann folgende Fehlermeldung: Fehler beim Kompilieren: Benutzerdefinierter Typ nicht definiert.
Markiert ist die Zeile "cb As LongLong" unter Private Type ITEMID
AW: Probleme mit VBA und OneDrive
17.02.2022 16:26:49
Nepumuk
Hallo Stefan,
hast du kein 64Bit Excel?
Gruß
Nepumuk
AW: Probleme mit VBA und OneDrive
18.02.2022 09:51:21
Stefan
Hi Nepumuk,
tatsächlich habe ich 32-bit Versionen. Die Kollegen haben teilw. schon 64-bit.
Dies kompatibel für beiden Versionen zu machen?
AW: Probleme mit VBA und OneDrive
19.02.2022 11:34:46
Nepumuk
Hallo Stefan,
das sollte in beiden Versionen laufen:

Option Explicit
Private Declare PtrSafe Function SHGetPathFromIDListA Lib "shell32.dll" ( _
ByVal pidl As LongPtr, _
ByVal pszPath As String) As Long
Private Declare PtrSafe Function SHGetSpecialFolderLocation Lib "shell32.dll" ( _
ByVal hwndOwner As LongPtr, _
ByVal nFolder As Long, _
ByRef pidl As ITEMIDLIST) As Long
Private Type ITEMID
cb As LongPtr
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As ITEMID
End Type
Private Const CSIDL_DESKTOP As Long = &H0
Private Const NO_ERROR As Long = 0&
Private Const MAX_PATH As Long = 260&
Public Sub Create_PDF()
Dim strPath As String
Dim objComment As Comment
Dim objFileDialog As FileDialog
For Each objComment In ActiveSheet.Comments
objComment.Visible = False
Next
ThisWorkbook.Save 'letzten Stand speichern
If Sheets("Tabelle3").Visible = xlSheetVisible Then
Sheets(Array("Tabelle1", "Tabelle2", "Tabelle3")).Select
Else
Sheets(Array("Tabelle1", "Tabelle2")).Select
End If
strPath = GetPath(CSIDL_DESKTOP) & "\" & Range("E3").Text & Range("J3").Text
Set objFileDialog = Application.FileDialog(msoFileDialogSaveAs)
With objFileDialog
.FilterIndex = 26
.InitialFileName = strPath
If .Show Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=.SelectedItems(1), _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
MsgBox Sheets("Packaging Instruction").Range("E3").Value & _
Sheets("Packaging Instruction").Range("J3").Value & _
" saved on desktop.", vbInformation, "Information"
End If
End With
Set objFileDialog = Nothing
End Sub
Private Function GetPath(ByVal pvlngFolderType As Long) As String
Dim lngResult As Long
Dim strBuffer As String
Dim udtIDL As ITEMIDLIST
lngResult = SHGetSpecialFolderLocation(Application.Hwnd, pvlngFolderType, udtIDL)
If lngResult = NO_ERROR Then
strBuffer = Space$(MAX_PATH)
lngResult = SHGetPathFromIDListA(ByVal udtIDL.mkid.cb, ByVal strBuffer)
If lngResult = 1 Then GetPath = Left$(strBuffer, InStr(1, strBuffer, vbNullChar) - 1)
End If
End Function
Gruß
Nepumuk
Anzeige
AW: Probleme mit VBA und OneDrive
22.02.2022 14:45:19
Stefan
Hi Nepumuk,
funktioniert. Besten Dank.
Leider habe ich noch ein weiteres Problem in der gleichen Arbeitsmappe. Durch klicken eines Buttons wird die Excel gespeichert und dann ein PDF erstellt, welches an eine Email angehängt wird. Bevor OneDrive wurde durch den Befehl "kill pdf" die PDF gelöscht. Mittlerweile kommt ein "Laufzeitfehler 53 - Datei nicht gefunden" und die pdf ist auch auf dem Desktop zu finden. Kannst du helfen? Hier der Code:

Sub Save_and_Send()
Dim c As Comment
For Each c In ActiveSheet.Comments
c.Visible = False
Next
ActiveWorkbook.Save 'letzten Stand speichern
Dim pdf As String
pdf = pdf_erstellen
Call permail(pdf)
Kill (pdf) 'pdf wieder löschen
End Sub

Function pdf_erstellen()
Dim pdf As String
Dim sep As String
sep = Application.PathSeparator
pdf = ThisWorkbook.Path & sep & ActiveSheet.Range("E3").Value & ActiveSheet.Range("J3").Value & ".pdf"
'ThisWorkbook.Path & sep & ThisWorkbook.name & ".pdf" 'Speicherpfad
If Sheets("Tabelle3").Visible = xlSheetVisible Then
Sheets(Array("Tabelle1", "Tabelle2", "Tabelle3")).Select
Else
Sheets(Array("Tabelle1", "Tabelle2")).Select
End If
On Error Resume Next
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdf, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
On Error GoTo 0
pdf_erstellen = pdf
Sheets("Packaging Instruction").Select 'Gruppierung aufheben
End Function

Anzeige
AW: Probleme mit VBA und OneDrive
22.02.2022 15:36:53
Nepumuk
Hallo Stefan,
so?

Public Sub Save_and_Send()
Dim c As Comment
Dim pdf As String
For Each c In ActiveSheet.Comments
c.Visible = False
Next
Thisworkbook.Save 'letzten Stand speichern
pdf = pdf_erstellen
Call permail(pdf)
Kill pdf 'pdf wieder löschen
End Sub
Private Function pdf_erstellen() As String
Dim pdf As String
pdf = GetPath(CSIDL_DESKTOP) & "\" & Range("E3").Text & Range("J3").Text & ".pdf"
If Sheets("Tabelle3").Visible = xlSheetVisible Then
Sheets(Array("Tabelle1", "Tabelle2", "Tabelle3")).Select
Else
Sheets(Array("Tabelle1", "Tabelle2")).Select
End If
On Error Resume Next
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdf, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
On Error GoTo 0
pdf_erstellen = pdf
Sheets("Packaging Instruction").Select 'Gruppierung aufheben
End Function
Eventuell musst du die Funktion "GetPath" als Public deklarieren.
Gruß
Nepumuk
Anzeige
AW: Probleme mit VBA und OneDrive
23.02.2022 10:47:48
Stefan
Hallo Nepumuk,
leider kommt "Fehler beim Kompilieren: Variable nicht definiert". Dabei ist "Private Function pdf_erstellen () As String" gelb markiert und "(CSIDL_DESKTOP)" blau hinterlegt.
GetPath als Public zu deklarieren, kenne ich mich zu wenig aus, wie das funktioniert. Kann es daran liegen?
AW: Probleme mit VBA und OneDrive
25.02.2022 10:29:32
Stefan
@Nepumuk, du bist meine letzte Rettung :)
Kannst du nochmal drüber schauen?

7 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige