|
30.11.2005, 14:25 | #1 |
MOF Meister |
Acc2002/XP - Grafik aus Zwischenablage in Datei speichern
Hallo Leute,
wie kann ich mit VBA ein Bitmap (Graphik), was sich in der Zwischenablage befindet als Datei speichern. Ich hab so was schon mal für Word gemacht, der Beitrag ist aber nicht mehr im Forum (vermutlich der Sommerabsturz). __________________ Gruß Steffen |
01.12.2005, 08:37 | #2 |
Threadstarter
MOF Meister |
Hi,
Code: SavePicture Ich hab mir jetzt aus den verschiedensten Quellen folgende Lösung zusammengebastelt: Code: Option Compare Database Option Explicit ' ACHTUNG : Einbindung der Excel-Objektbibliotek erforderlich (Extras-Verweise) ' Einbindung der OLE-Automation erforderlich (Extras-Verweise) ' getestet unter AC97 und AC2000 Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type 'Declare a UDT to store the bitmap information Private Type uPicDesc Size As Long Type As Long hPic As Long hPal As Long End Type '''Windows API Function Declarations 'Does the clipboard contain a bitmap/metafile? Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long 'Open the clipboard to read Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long 'Get a pointer to the bitmap/metafile Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long 'Close the clipboard Private Declare Function CloseClipboard Lib "user32" () As Long 'Convert the handle into an OLE IPicture interface. Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long 'Create our own copy of the metafile, so it doesn't get wiped out by subsequent clipboard updates. Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long 'Create our own copy of the bitmap, so it doesn't get wiped out by subsequent clipboard updates. Declare Function CopyImage Lib "user32" (ByVal handle As Long, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long 'The API format types we're interested in Const CF_BITMAP = 2 Const CF_PALETTE = 9 Const CF_ENHMETAFILE = 14 Const IMAGE_BITMAP = 0 Const LR_COPYRETURNORG = &H4 Function PastePicture(Optional lXlPicType As Long = xlPicture) As IPicture 'Some pointers Dim h As Long, hPicAvail As Long, hPtr As Long, hPal As Long, lPicType As Long, hCopy As Long 'Convert the type of picture requested from the xl constant to the API constant lPicType = IIf(lXlPicType = xlBitmap, CF_BITMAP, CF_ENHMETAFILE) 'Check if the clipboard contains the required format hPicAvail = IsClipboardFormatAvailable(lPicType) If hPicAvail <> 0 Then 'Get access to the clipboard h = OpenClipboard(0&) If h > 0 Then 'Get a handle to the image data hPtr = GetClipboardData(lPicType) 'Create our own copy of the image on the clipboard, in the appropriate format. If lPicType = CF_BITMAP Then hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG) Else hCopy = CopyEnhMetaFile(hPtr, vbNullString) End If 'Release the clipboard to other programs h = CloseClipboard 'If we got a handle to the image, convert it into a Picture object and return it If hPtr <> 0 Then Set PastePicture = CreatePicture(hCopy, 0, lPicType) End If End If End Function Private Function CreatePicture(ByVal hPic As Long, ByVal hPal As Long, ByVal lPicType) As IPicture ' IPicture requires a reference to "OLE Automation" Dim r As Long, uPicInfo As uPicDesc, IID_IDispatch As GUID, IPic As IPicture 'OLE Picture types Const PICTYPE_BITMAP = 1 Const PICTYPE_ENHMETAFILE = 4 ' Create the Interface GUID (for the IPicture interface) With IID_IDispatch .Data1 = &H7BF80980 .Data2 = &HBF32 .Data3 = &H101A .Data4(0) = &H8B .Data4(1) = &HBB .Data4(2) = &H0 .Data4(3) = &HAA .Data4(4) = &H0 .Data4(5) = &H30 .Data4(6) = &HC .Data4(7) = &HAB End With ' Fill uPicInfo with necessary parts. With uPicInfo .Size = Len(uPicInfo) ' Length of structure. .Type = IIf(lPicType = CF_BITMAP, PICTYPE_BITMAP, PICTYPE_ENHMETAFILE) ' Type of Picture .hPic = hPic ' Handle to image. .hPal = IIf(lPicType = CF_BITMAP, hPal, 0) ' Handle to palette (if bitmap). End With ' Create the Picture object. r = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic) ' If an error occured, show the description If r <> 0 Then Debug.Print "Create Picture: " & fnOLEError(r) ' Return the new Picture object. Set CreatePicture = IPic End Function Private Function fnOLEError(lErrNum As Long) As String 'OLECreatePictureIndirect return values Const E_ABORT = &H80004004 Const E_ACCESSDENIED = &H80070005 Const E_FAIL = &H80004005 Const E_HANDLE = &H80070006 Const E_INVALIDARG = &H80070057 Const E_NOINTERFACE = &H80004002 Const E_NOTIMPL = &H80004001 Const E_OUTOFMEMORY = &H8007000E Const E_POINTER = &H80004003 Const E_UNEXPECTED = &H8000FFFF Const S_OK = &H0 Select Case lErrNum Case E_ABORT fnOLEError = " Aborted" Case E_ACCESSDENIED fnOLEError = " Access Denied" Case E_FAIL fnOLEError = " General Failure" Case E_HANDLE fnOLEError = " Bad/Missing Handle" Case E_INVALIDARG fnOLEError = " Invalid Argument" Case E_NOINTERFACE fnOLEError = " No Interface" Case E_NOTIMPL fnOLEError = " Not Implemented" Case E_OUTOFMEMORY fnOLEError = " Out of Memory" Case E_POINTER fnOLEError = " Invalid Pointer" Case E_UNEXPECTED fnOLEError = " Unknown Error" Case S_OK fnOLEError = " Success!" End Select End Function Function GrafikZwischenablage2Datei(DatName As String) As Boolean Dim lPicType As Long, oPic As Variant lPicType = xlBitmap Set oPic = PastePicture(lPicType) If oPic Is Nothing Then Exit Function SavePicture oPic, DatName GrafikZwischenablage2Datei = True End Function Sub mytest() Dim ZielDat As String ZielDat = "d:\tar\mytest.bmp" If GrafikZwischenablage2Datei(ZielDat) Then MsgBox ZielDat & " erzeugt!" Else MsgBox "kein bild in der zwischenablage" End If End Sub __________________ Gruß Steffen |
01.12.2005, 10:11 | #3 |
MOF Meister |
Kompliment, nette Funktion!
__________________ Gruss aus dem (wilden) Süden ;-) Karl-Heinz<br><FONT Size="2" color="#B22222">PS: Wenn das <b>Thema abgeschlossen</b> ist, bitte den Thread mit einem <b>Feedback</b> als erledigt kennzeichnen, bzw. mit dieser <b><a *****"http://www.ms-office-forum.net/forum/showthread.php?t=102899#erledigen" target="_blank">neuen Funktion</a></b> (anklicken) als <b><a *****"http://www.ms-office-forum.net/forum/showpost.php?p=911701&postcount=1" target="_blank">erledigt</a></b> setzen!</font><br> |
20.12.2005, 12:15 | #4 |
MOF Guru |
Hallo Steffen,
habe heute deine tolle Prozedur entdeckt - Danke! Bei mir wird aber leider immer nur ein Ausschnitt des Screenshots gespeichert und ich kann nicht finden, wie man die Größe der Bitmap beeinflussen könnte. Hast du eine Idee, was man da machen kann? __________________ Liebe GrüßeAnne |
20.12.2005, 12:27 | #5 |
MOF Meister |
Hallo Anne,
PMFJI Komisch, bei mir wird alles gespeichert. Wie erstellst du denn den Screenshot? Ich hab's einfach per Drucktaste in die Zwischenablage geholt. [Edit]: PS: Was mich ein bißchen stört ist die enorme Dateigröße der generierten Bild-Datei. Hat jemand 'ne Ahnung wie man das beeinflussen kann? __________________ Gruss aus dem (wilden) Süden ;-) Karl-Heinz<br><FONT Size="2" color="#B22222">PS: Wenn das <b>Thema abgeschlossen</b> ist, bitte den Thread mit einem <b>Feedback</b> als erledigt kennzeichnen, bzw. mit dieser <b><a *****"http://www.ms-office-forum.net/forum/showthread.php?t=102899#erledigen" target="_blank">neuen Funktion</a></b> (anklicken) als <b><a *****"http://www.ms-office-forum.net/forum/showpost.php?p=911701&postcount=1" target="_blank">erledigt</a></b> setzen!</font><br> Geändert von KHS (20.12.2005 um 12:32 Uhr). |
20.12.2005, 13:27 | #6 |
Threadstarter
MOF Meister |
Hallo Anne & Karl-Heinz,
@Anne: Auch ich kann dein Problem nicht nachvollziehen. Vergleich doch mal das Ergebnis von - Erzeugter Datei - Interner Windowsanzeige (NT:Programme/Zubehör/Zwischenablage) - Einfügen in ein beliebiges Zeichenprogramm @Karl-Heinz: Ich denke das die Prozedur selbst nur BMP kann (Befehl SavePicture) Das erzeugte Bild ist aber verlustfrei und muss somit so groß sein. Eine Umwandlung zu GIF führt zu einer Reduzierung der Farben und JPG "vermatscht" und Größenreduzierung reduziert die Auflösung. Keiner "hindert" dich aber das Bild (automatisch) zu konvertieren. In Access selbst wäre das Einbinden einer DLL möglich. Ich selbst (in meiner Bastelstube) bevorzuge die Nutzung von IrfanView entweder als batch : Code: Sub ConvIt() Dim myCom As String Dim srcFile As String, dstFile As String Const IV As String = "......\i_view32.exe" srcFile = "d:\tar\mytest.bmp" dstFile = "d:\tar\mytest.gif" myCom = IV & " " & srcFile & " /convert=" & dstFile Shell (myCom) End Sub __________________ Gruß Steffen |
20.12.2005, 22:10 | #7 |
MOF Guru |
Also, vielleicht trägt dies ja zur Klärung bei:
Wenn ich (manuell) einen Screenshot in die Zwischenablage bringe und in Paint einfügen möchte, kommt bei mir stets die Frage "Das Bild in der Zwischenablage ist größer als die Bitmap. Soll die Bitmap vergrößert werden?" was dann mit Ja zu beantworten ist. Hier sehe ich die Ursache, denn die Frage kommt ja nicht durch und kann nicht beantwortet werden. __________________ Liebe GrüßeAnne |
21.12.2005, 07:31 | #8 |
MOF Meister |
Hallo Steffen,
die Funktion speichert auch in JPG und GIF (andere Formate hab' ich noch nicht getestet) - zumindest werden die Dateiendungen 'ohne Murren geschluckt', wobei die Dateigröße immer identisch mit einer BMP ist. Aber deine Konvertierungs-Geschichte ist auch interessant, muß ich mal bei Gelegenheit probieren - Danke für den Tipp. Mit SendKeys arbeite ich nun weniger gern. Hallo Anne, das hängt dann aber möglicherweise mit spezifischen Gegebenheiten auf deinem Rechner zusammen (Version?), oder? Bei mir kommt eine solche Frage in Paint nicht. __________________ Gruss aus dem (wilden) Süden ;-) Karl-Heinz<br><FONT Size="2" color="#B22222">PS: Wenn das <b>Thema abgeschlossen</b> ist, bitte den Thread mit einem <b>Feedback</b> als erledigt kennzeichnen, bzw. mit dieser <b><a *****"http://www.ms-office-forum.net/forum/showthread.php?t=102899#erledigen" target="_blank">neuen Funktion</a></b> (anklicken) als <b><a *****"http://www.ms-office-forum.net/forum/showpost.php?p=911701&postcount=1" target="_blank">erledigt</a></b> setzen!</font><br> |
21.12.2005, 10:02 | #9 |
Threadstarter
MOF Meister |
Hallo ihr zwei,
also Paint und der VBA-Code zu Abspeicherung sind meiner Meinung nach 2 völlig unabhängige Sachen. @ Anne: Paint hat beim Aufruf ein leeres "Blatt" mit einer entspechenden Pixelanzahl (Einzustellen unter Bild->Attribute und die letzte Einstellung wird sich "gemerkt"). Ist das einzufügende Bitmap größer (hat entweder in Breite oder Höhe mehr Pixel als das leere Blatt), erfolgt eine Meldung. Wenn bei Karl-Heinz die Meldung nicht kommt, ist sein leeres Blatt aktuell größer eingestellt. Wir haben also somit immer noch keine Klärung für dein Problem. @ Karl-Heinz: Wenn du bei "ZielDat" xxx.jpg angibst, heisst das lediglich, dass die Datei "xxx.jpg" benannt wird, in Wirklichkeit ist es immer noch im Bitmap-Format und deshalb bleibt auch die Dateigröße gleich. In Abhängigkeit von deinem Bildbearbeitungsprogramm, wird es dich darauf hinweisen, dass die Datei eine falsche Endung hat. "SendKeys" ist sicher eine problematische Kiste, aber man kann damit praktisch jedes beliebige Programm an Office ankoppeln, was wiederum 'ne Spitzenanwendung ist. ... da fällt mir noch ein PrtSc : bringt gesamten Bildschirm in Zwischenablage Alt PrtSc : bringt aktuelles Fenster in Zwischenablage __________________ Gruß SteffenGeändert von Steffen0815 (21.12.2005 um 10:11 Uhr). Grund: da fällt mir noch ein |
21.12.2005, 10:31 | #10 |
MOF Meister |
Zitat: Wenn du bei "ZielDat" xxx.jpg angibst, heisst das lediglich, dass die Datei "xxx.jpg" benannt wird, in Wirklichkeit ist es immer noch im Bitmap-Format und deshalb bleibt auch die Dateigröße gleich. "zumindest werden die Dateiendungen 'ohne Murren geschluckt'" PS: Mein leeres Paint-Blatt ist kleiner als das eingefügte Bild!? Geht trotzdem ohne Probleme oder Frage - auch das manuelle Einfügen. So, jetzt geht's aber demnächst zum Feiern __________________ Gruss aus dem (wilden) Süden ;-) Karl-Heinz<br><FONT Size="2" color="#B22222">PS: Wenn das <b>Thema abgeschlossen</b> ist, bitte den Thread mit einem <b>Feedback</b> als erledigt kennzeichnen, bzw. mit dieser <b><a *****"http://www.ms-office-forum.net/forum/showthread.php?t=102899#erledigen" target="_blank">neuen Funktion</a></b> (anklicken) als <b><a *****"http://www.ms-office-forum.net/forum/showpost.php?p=911701&postcount=1" target="_blank">erledigt</a></b> setzen!</font><br> |
21.12.2005, 11:57 | #11 |
MOF Guru |
Hallo an alle: Kurze Entwarnung!
Es liegt an dem Code, mit dem ich den ScreenShot in die Zwischenablage bringe. Der bietet angeblich die Option, das aktive Formular oder den ganzen BS zu kopieren. Witzigerweise kommt beim Formular-Print nun alles und in vollst. Größe (das hatte ich noch nicht getestet, weil ich es nicht zu brauchen glaubte ), beim Komplett-Print aber nur ein kleiner Auszug. Wieso auch immer, damit kann ich leben und so kann ich es nun auch einsetzen. P.S. Karl-Heinz, woher weißt, dass es bei uns jetzt zur Weihnachtsfeier losgeht?! __________________ Liebe GrüßeAnne |
21.12.2005, 14:03 | #12 |
MOF Meister |
Hallo Anne,
tja, da staunst du, was? Viel Spass dabei! __________________ Gruss aus dem (wilden) Süden ;-) Karl-Heinz<br><FONT Size="2" color="#B22222">PS: Wenn das <b>Thema abgeschlossen</b> ist, bitte den Thread mit einem <b>Feedback</b> als erledigt kennzeichnen, bzw. mit dieser <b><a *****"http://www.ms-office-forum.net/forum/showthread.php?t=102899#erledigen" target="_blank">neuen Funktion</a></b> (anklicken) als <b><a *****"http://www.ms-office-forum.net/forum/showpost.php?p=911701&postcount=1" target="_blank">erledigt</a></b> setzen!</font><br> |