Anzeige
Archiv - Navigation
1896to1900
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
Bild aus Sheet in Userform laden
30.08.2022 09:18:42
Finn64
Hallo zusammen,
Ich sitze gerade daran, ein Bild, das vorher über meine UserForm in die Excel Datei geladen wurde, auch in der Userform anzuzeigen. Ich hatte das ganze probiert mit LoadPicture(). Das Problem ist nur, dass das Bild auch angezeigt werden soll, wenn die Excel Datei verschickt wird. Das Bild wird dafür schon mit der Shapes.AddPicture Funktion mit der Datei zusammen gespeichert. D.h. nach dem Verschicken wird das Bild auch immernoch in der Excel Datei angezeigt, unabhängig davon, dass auf den Pfad zum Bild nicht mehr zugegriffen werden kann. Das schaffe ich allerdings nicht beim Anzeigen in der Userform, dafür muss der Pfad vom Bild noch bekannt sein und ich kann nicht auf das mit dem Dokument zusammen gespeicherten Bild zugreifen.
Falls das zu kryptisch war, hier nochmal mein code:

'Bild wird in Zelle gespeichert unabhängig vom Dateipfad
Dim objPicture As Shape
Dim pfad$
pfad = dateiauswahl()
If pfad  "" Then
On Error Resume Next
ActiveSheet.Shapes("Bild1").Delete
On Error GoTo 0
Set objPicture = ActiveSheet.Shapes.AddPicture(Filename:=pfad, LinktoFile:=False, SaveWithDocument:=True, Left:=Cells(18, 2).Left, Top:=Cells(18, 2).Top, Width:=Range("B18:C27").Width, Height:=Range("B18:C27").Height)
objPicture.Name = "Bild1"
Set objPicture = Nothing
'Bild in der Userform wird erzeugt
Dim cnt As MSForms.Control
Set cnt = Me.Controls.Add("Forms.Image.1", "Img" & t, True)
cnt.Left = 120
cnt.Top = 15
cnt.Width = 80
cnt.Height = 80
cnt.PictureSizeMode = fmPictureSizeModeZoom
cnt.Picture = LoadPicture(Range("B18:C27"))            '==> hier hatte ich erst den Pfad, aber genau das möchte ich ja nicht
End If
Vielen Dank schonmal,
schöne Grüße,
Finn

2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Bild aus Sheet in Userform laden
30.08.2022 09:54:12
Nepumuk
Hallo Finn,
so:

    'Bild wird in Zelle gespeichert unabhängig vom Dateipfad
Dim objPicture As Shape
Dim pfad$
pfad = dateiauswahl()
If pfad  "" Then
On Error Resume Next
ActiveSheet.Shapes("Bild1").Delete
On Error GoTo 0
Set objPicture = ActiveSheet.Shapes.AddPicture(Filename:=pfad, LinktoFile:=False, SaveWithDocument:=True, Left:=Cells(18, 2).Left, Top:=Cells(18, 2).Top, Width:=Range("B18:C27").Width, Height:=Range("B18:C27").Height)
objPicture.Name = "Bild1"
Set objPicture = Nothing
'Bild in der Userform wird erzeugt
Dim cnt As MSForms.Image
Set cnt = Me.Controls.Add("Forms.Image.1", "Img" & t, True)
cnt.Left = 120
cnt.Top = 15
cnt.Width = 80
cnt.Height = 80
cnt.PictureSizeMode = fmPictureSizeModeZoom
Set cnt.Picture = GetPicture(ActiveSheet, "Bild1")
End If
und in einem Standardmodul:

Option Explicit
Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32.dll" ( _
ByRef PicDesc As PICT_DESC, _
ByRef RefIID As GUID, _
ByVal fPictureOwnsHandle As LongPtr, _
ByRef IPic As IPicture) As LongPtr
Private Declare PtrSafe Function CopyImage Lib "user32.dll" ( _
ByVal handle As LongPtr, _
ByVal un1 As Long, _
ByVal n1 As Long, _
ByVal n2 As Long, _
ByVal un2 As Long) As LongPtr
Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32.dll" ( _
ByVal wFormat As Long) As Long
Private Declare PtrSafe Function OpenClipboard Lib "user32.dll" ( _
ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function GetClipboardData Lib "user32.dll" ( _
ByVal wFormat As Long) As LongPtr
Private Declare PtrSafe Function DeleteObject Lib "gdi32.dll" ( _
ByVal hObject As LongPtr) As Long
Private Declare PtrSafe Function CLSIDFromString Lib "ole32.dll" ( _
ByVal lpsz As Any, _
ByRef pCLSID As GUID) As Long
Private Declare PtrSafe Function EmptyClipboard Lib "user32.dll" () As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32.dll" () As Long
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type PICT_DESC
lSize As Long
lType As Long
hPic As LongPtr
hPal As LongPtr
End Type
Private Const PICTYPE_BITMAP As Long = 1
Private Const CF_BITMAP As Long = 2
Private Const IMAGE_BITMAP As Long = 0
Private Const LR_COPYRETURNORG As Long = &H4
Private Const GUID_IPICTUREDISP As String = "{7BF80981-BF32-101A-8BBB-00AA00300CAB}"
Private Function PastePicture(ByRef prlngptrCopy As LongPtr) As IPictureDisp
Dim lngReturn As Long, lngptrPointer As LongPtr
If CBool(IsClipboardFormatAvailable(CF_BITMAP)) Then
lngReturn = OpenClipboard(Application.hwnd)
If lngReturn > 0 Then
lngptrPointer = GetClipboardData(CF_BITMAP)
prlngptrCopy = CopyImage(lngptrPointer, _
IMAGE_BITMAP, 0&, 0&, LR_COPYRETURNORG)
Call CloseClipboard
If lngptrPointer  0 Then Set PastePicture = _
CreatePicture(prlngptrCopy, 0)
End If
End If
End Function
Private Function CreatePicture( _
ByVal lngptrhPic As LongPtr, _
ByVal lngptrhPal As LongPtr) As IPictureDisp
Dim udtPicInfo As PICT_DESC, udtID_IDispatch As GUID
Dim objPicture As IPictureDisp
Call CLSIDFromString(StrPtr( _
GUID_IPICTUREDISP), udtID_IDispatch)
With udtPicInfo
.lSize = LenB(udtPicInfo)
.lType = PICTYPE_BITMAP
.hPic = lngptrhPic
.hPal = lngptrhPal
End With
Call OleCreatePictureIndirect(udtPicInfo, _
udtID_IDispatch, 0&, objPicture)
Set CreatePicture = objPicture
Set objPicture = Nothing
End Function
Public Function GetPicture( _
ByRef probjWorksheet As Worksheet, _
ByVal pvstrShapeName As String) As IPictureDisp
Dim lngptrCopy As LongPtr
Dim objTempPicture As IPictureDisp
Call OpenClipboard(0)
Call EmptyClipboard
Call CloseClipboard
On Error Resume Next
Do
Call probjWorksheet.Shapes(pvstrShapeName).CopyPicture( _
Appearance:=xlScreen, Format:=xlBitmap)
If Err.Number = 0 Then Exit Do
Call Err.Clear
Loop
On Error GoTo 0
Do
Set objTempPicture = PastePicture(lngptrCopy)
Loop While objTempPicture Is Nothing
Set GetPicture = objTempPicture
Set objTempPicture = Nothing
End Function
Gruß
Nepumuk
Anzeige
AW: Bild aus Sheet in Userform laden
30.08.2022 10:13:53
Finn64
Super vielen Dank, hat sofort geklappt :)

309 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige