MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Feb 13th, 2005, 02:26 AM   #1
Wil Moosa
 
Wil Moosa's Avatar
 
Join Date: Aug 2002
Location: Maasbree, the Netherlands
Posts: 820
Default Selection.Copy and save as bitmap

I was wondering looking at the code down below. Is it possible by vba to save the bitmap straight away -without the interference of Word or even PaintShopPro or any other programme- as a bmp file? How would the code look like then?

Sub Bitmap_export()
Dim WrdApp As New Word.Application
Dim wrdDoc As New Word.Document
'copy the required data from the Excel File
Selection.Copy
' Create a new Word Document
Set wrdDoc = WrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)
WrdApp.Visible = True
'Paste the copied data as Picture
wrdDoc.SaveAs Filename:=ThisWorkbook.path & "\Exporteren\" & "FNE Bitmap " & Format(Date, "dd-mm-yy") + "_" + Format(Time, "hh-mm") & ".doc "
WrdApp.Selection.PasteSpecial Placement:=wdInLine, _
DataType:=wdPasteBitmap

End Sub
__________________
Where there is a Wil, there is a way....
Wil Moosa is offline   Reply With Quote
Old Feb 13th, 2005, 02:48 AM   #2
daniels012
 
daniels012's Avatar
 
Join Date: Jan 2005
Location: Greenville, SC
Posts: 4,559
Default

Not sure what you are looking to do but maybe this will help...
If you are trying to copy a section of the excel sheet and save it as a picture, here is how you do that.

Select area to save as bitmap ie: A1:G12.
Then hold down shift... On your menu bar hit Edit-> You will see CopyPicture click it...then choose as shown when printed... Then you can paste into the Microsoft Paint program or paste into word or paste into another excell worksheet.

Hope this helps,
Michael
__________________
"It is so nice, to see so many, that know so much"

Using Excel 2007
daniels012 is offline   Reply With Quote
Old Feb 13th, 2005, 02:57 PM   #3
Wil Moosa
 
Wil Moosa's Avatar
 
Join Date: Aug 2002
Location: Maasbree, the Netherlands
Posts: 820
Default

I understand that but the idea is to use vba code to save the data staraight away as a .bmp file. So I want te avoid to open any editor.
__________________
Where there is a Wil, there is a way....
Wil Moosa is offline   Reply With Quote
Old Feb 13th, 2005, 11:50 PM   #4
Wil Moosa
 
Wil Moosa's Avatar
 
Join Date: Aug 2002
Location: Maasbree, the Netherlands
Posts: 820
Default

I solved the problem by adjusting code from Stephen Bullen. the original code can be found at... http://www.oaltd.co.uk/DLCount/DLCo...astePicture.zip
__________________
Where there is a Wil, there is a way....
Wil Moosa is offline   Reply With Quote
Old Feb 13th, 2005, 11:58 PM   #5
HOTPEPPER
MrExcel MVP
 
HOTPEPPER's Avatar
 
Join Date: Mar 2004
Location: Oregon
Posts: 12,576
Default

That link gave me a Page Not Found error.

Any possibility you could post your code, so we can see how you did it?
__________________
Office 2007
HOTPEPPER is offline   Reply With Quote
Old Feb 14th, 2005, 12:24 AM   #6
Wil Moosa
 
Wil Moosa's Avatar
 
Join Date: Aug 2002
Location: Maasbree, the Netherlands
Posts: 820
Default

The following link should work: http://www.bmsltd.ie/Excel/Default.htm The name of the file is PastePicture.Zip

The code from Stephen Bullen comes with a userform. I got rid of the userform and used the code from the "Save" button for non-userform related code. Before you run the code you have to select "the area to be pictured" first.

I guess that the code from module 1 can be made more efficient for my purpose.

It turned out that the code has to be placed in two seperate modules as in one module it did not work...

Module 1:

Option Explicit
Option Compare Text

''' User-Defined Types for API Calls

'Declare a UDT to store a GUID for the IPicture OLE Interface
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

And module 2:

Sub Bitmap_Exporteren2()

Dim vFile As Variant, sFilter As String, lPicType As Long, oPic As IPictureDisp

'Create an appropriate filter, based on the bitmap/picture option in the dialog
sFilter = IIf(obMetafile, "Windows Metafile (*.emf),*.emf", "Windows Bitmap (*.bmp),*.bmp")

'Get the filename to save the bitmap to
vFile = ThisWorkbook.path & "\Exporteren\" & "FNE Bitmap2 " & Format(Date, "dd-mm-yy") + "_" + Format(Time, "hh-mm") & ".bmp "
If vFile <> False Then
'Get the type of bitmap
lPicType = IIf(obMetafile, xlPicture, xlBitmap)

'Copy a picture to the clipboard
Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
'Retrieve the picture from the clipboard...
Set oPic = PastePicture(lPicType)

'... and save it to the file
SavePicture oPic, vFile

End If

End Sub
__________________
Where there is a Wil, there is a way....
Wil Moosa is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 04:25 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes