Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1256to1260
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 in userform
DETLEF
Hallo Excel-Profis
Ich habe in "Tabelle1) Zelle A1 ein Bild eingefügt --> Excel nennt das jetzt "Grafik 1"
Dieses Bild wollte ich nun als Hintegrundbild für der Userform verwenden
hier mein Versuch
Private Sub UserForm_Initialize()
UserForm.Picture = LoadPicture(sheets("Tabelle1").Grafik 1)
End Sub
funktioniert so aber nicht | das Bild muss aber aus dem Tabellenblatt sein da ich es nicht auf Festplatte oder Netzwerk ablegen will
Geht mein Vorhaben überhaupt?

10
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: bild in userform
01.04.2012 21:04:14
Josef

Hallo Detlef,
das geht.
In ein allgemeines Modul.
' **********************************************************************
' Modul: Modul3 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Public Function Export_Picture(myShape As Shape, FileName As String) As Long
  'Idea by Nepumuk
  Dim myChart As Chart, myChartObject As ChartObject
  Dim strFilter As String
  
  On Error GoTo ErrExit
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  strFilter = UCase(Right(FileName, 3))
  
  Select Case strFilter
    Case "GIF", "JPG", "PNG"
    Case Else
      MsgBox "Ungültiges Grafikformat!" & vbLf & vbLf & "Export als '" & FileName & "' nicht möglich.", _
        vbInformation, "Export_Picture"
      Err.Raise vbObjectError + 1024
  End Select
  
  Set myChart = Charts.Add
  Set myChartObject = ActiveChart.ChartObjects.Add(0, 0, myShape.Width, myShape.Height)
  
  myShape.Copy
  
  With myChartObject
    With .Chart
      .ChartArea.Border.LineStyle = xlLineStyleNone
      .Paste
      .Export FileName:=FileName, FilterName:=strFilter, Interactive:=False
    End With
  End With
  
  myChart.Delete
  
  DoEvents
  
  ErrExit:
  Export_Picture = Err.Number = 0
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
  Set myChart = Nothing
  Set myChartObject = Nothing
  Set myShape = Nothing
End Function


In das Modul des UF.
' **********************************************************************
' Modul: UserForm1 Typ: Userform
' **********************************************************************

Option Explicit

Private Sub UserForm_Activate()
  Dim strTmp As String
  strTmp = Environ("TEMP") & "\tmp.gif"
  
  Export_Picture Sheets("Tabelle1").Shapes("Grafik 1"), strTmp
  Me.Picture = LoadPicture(strTmp)
  Kill strTmp
End Sub



« Gruß Sepp »

Anzeige
AW: bild in userform
01.04.2012 21:09:56
DETLEF
Hallo Sepp;
das klappt ja Super
vielen Dank
AW: bild in userform
01.04.2012 21:23:20
Nepumuk
Hallo,
oder auch so:
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" ( _
    ByRef PicDesc As PIC_DESC, _
    ByRef RefIID As GUID, _
    ByVal fPictureOwnsHandle As Long, _
    ByRef IPic As IPictureDisp) As Long
Declare Function CopyImage Lib "user32.dll" ( _
    ByVal handle As Long, _
    ByVal un1 As Long, _
    ByVal n1 As Long, _
    ByVal n2 As Long, _
    ByVal un2 As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32.dll" ( _
    ByVal wFormat As Integer) As Long
Private Declare Function OpenClipboard Lib "user32.dll" ( _
    ByVal hWnd As Long) As Long
Private Declare Function GetClipboardData Lib "user32.dll" ( _
    ByVal wFormat As Integer) As Long
Private Declare Function EmptyClipboard Lib "user32.dll" () As Long
Private Declare 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 PIC_DESC
    lngSize As Long
    lngType As Long
    lnghPic As Long
    lnghPal As Long
End Type

Private Const PICTYPE_BITMAP = 1
Private Const CF_BITMAP = 2
Private Const IMAGE_BITMAP = 0
Private Const LR_COPYRETURNORG = &H4

Private Function Paste_Picture() As IPictureDisp
    
    Dim lngReturn As Long, lngCopy As Long, lngPointer As Long
    
    If IsClipboardFormatAvailable(CF_BITMAP) <> 0 Then
        
        lngReturn = OpenClipboard(Application.hWnd)
        
        If lngReturn > 0 Then
            
            lngPointer = GetClipboardData(CF_BITMAP)
            lngCopy = CopyImage(lngPointer, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
            Call CloseClipboard
            If lngPointer <> 0 Then Set Paste_Picture = Create_Picture(lngCopy)
            
        End If
    End If
End Function

Private Function Create_Picture( _
        ByVal pvlnghPic As Long) As IPictureDisp

    
    Dim udtPicInfo As PIC_DESC, udtID_IDispatch As GUID
    Dim objPicture As IPictureDisp
    
    With udtID_IDispatch
        .Data1 = &H20400
        .Data4(0) = &HC0
        .Data4(7) = &H46
    End With
    
    With udtPicInfo
        .lngSize = Len(udtPicInfo)
        .lngType = PICTYPE_BITMAP
        .lnghPic = pvlnghPic
        .lnghPal = 0
    End With
    
    Call OleCreatePictureIndirect(udtPicInfo, udtID_IDispatch, 0&, objPicture)
    
    Set Create_Picture = objPicture
    
End Function

Public Sub Show_Picture( _
        ByRef probjTable As Worksheet, _
        ByRef probjImage As MSForms.Image, _
        ByVal pvstrPictureName As String)

    
    Dim objPicture As IPictureDisp
    
    Call OpenClipboard(Application.hWnd)
    Call EmptyClipboard
    Call CloseClipboard
    
    probjTable.Shapes(pvstrPictureName).CopyPicture _
        Appearance:=xlScreen, Format:=xlBitmap
    
    Set objPicture = Paste_Picture
    
    If Not objPicture Is Nothing Then
        Set probjImage.Picture = objPicture
    Else
        MsgBox "Picture can't show in Userform", vbCritical, "Error"
    End If
End Sub

' **********************************************************************
' Modul: UserForm1 Typ: Userform
' **********************************************************************

Option Explicit

Private Sub UserForm_Activate()
    Call Show_Picture(Tabelle1, Image1, "Grafik 1")
End Sub

Gruß
Nepumuk
Anzeige
Na das sichere ich mir auch mal ab,...
01.04.2012 21:39:02
Matthias
Hallo Leutz,
mächtig kompliziert wie das als Oberlaie so sehe.
Aber ich habe einfach eine Userform erstellt.
Das Anzeigewerkzeug gewählt in passender größe.
Dann die Eigenschaften aufgerufen.
Die Bilder die man doch in Excel einfügt liegen ja in irgendeinerform als Daten ja vor, somit lad ein Bild von deiner Platte oder wo auch immer in dieses Anzeigentool.
Einstellen Picture Center u. Scretch - für zentriertes Bild.
Die Userform jetzt nur noch laden - wie immer du es möchtest.
Speichern - fertig.
Das Bild / Bilder kannst du nun von der Platte entfernen .
Excel speichert die Anzeige irgendwo ab.
hier eine Beispieldatei : https://www.herber.de/bbs/user/79628.xlsm
Gruß Matze.
Anzeige
AW: bild in userform
02.04.2012 10:16:38
JoWE
Hallo,
ich habe Deinen Code ausprobieren wollen, den Code entsprechend Deiner Vorgabe teilweise ins Modul und in die neue UF importiert und die UF gestartet. Der Code bleibt aber hängen: (1) Fehler: Image1 ist nicht definiert: Habe ich nachgeholt! Dann aber trotzdem beim Aktivieren: (2) Fehler in der Prozedur Show_Picture: "Objektvariable oder With-Block-Variable nicht festgelegt". Ein Bild (Bild1.bmp) befindet sich als "Picture 4" auf der Tabelle1. Habe es auch als Bild1.jpg erfolglos probiert.
Was mache ich falsch?
Meine Arbeitsmappe: https://www.herber.de/bbs/user/79631.xls
Gruß
Jochen
Anzeige
AW: bild in userform
02.04.2012 12:28:47
Nepumuk
Hallo,
du musst auf deinem Userform ein Image-Control einfügen. Oder willst du das Bild direkt auf dem Userform anzeigen?
Gruß
Nepumuk
AW: bild in userform
02.04.2012 14:18:23
JoWE
...ah ja, so klappt's.
Aber eigentlich wollte ich es schon direkt auf der UF anzeigen, sofern möglich.
Gruß
Jochen
AW: bild in userform
02.04.2012 15:09:29
Nepumuk
Hallo,
so:
Private Sub UserForm_Activate()
    Call Show_Picture(Tabelle1, Me, "Picture 4")
End Sub

Public Sub Show_Picture( _
        ByRef probjTable As Worksheet, _
        ByRef probjForm As Object, _
        ByVal pvstrPictureName As String)

    
    Dim objPicture As IPictureDisp
    
    Call OpenClipboard(Application.hWnd)
    Call EmptyClipboard
    Call CloseClipboard
    
    probjTable.Shapes(pvstrPictureName).CopyPicture _
        Appearance:=xlScreen, Format:=xlBitmap
    
    Set objPicture = Paste_Picture
    
    If Not objPicture Is Nothing Then
        Set probjForm.Picture = objPicture
    Else
        MsgBox "Picture can't show in Userform", vbCritical, "Error"
    End If
End Sub

Gruß
Nepumuk
Anzeige
AW: bild in userform
02.04.2012 15:30:58
JoWE
Hi Nepumuk,
super, Danke!
Gruß
Jochen
OT: Ob die Idee ursprgl von ...
03.04.2012 00:42:48
...
…Nepumuk stammt, weiß ich nicht, Sepp,
aber schon im allerersten Archivjahr 1999 (kein Zugriff mehr) hatte ein Norbert Köhler so etwas kreiert, was von HWH damals als gute Idee gelobt wurde; war allerdings etwas fehlerhaft… ;-)
Gruß Luc :-?

303 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige