none
Problem with IDataObject.GetData RRS feed

  • Question

  • I'm trying to extract a snapshot of formated data in excel and save it to a separate file.  The following works.

    MyExcel = CreateObject("Excel.Application")

    MyExcel.Workbooks.Open(MyInputPath & "\" & MyInputFilename)

    myWrkBk = MyExcel.Workbooks(MyInputFilename)

    For Each MyWrkSheet In myWrkBk.Worksheets

    MyWrkSheet.Range("C3:L9").CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap)

    SaveToBmp(OutputPath & "\tmp.bmp"

    Next MywrkSheet

    Private Sub SaveToBmp(ByVal Filename As String)

    Dim img As Image

    Dim data As IDataObject

    data = Clipboard.GetDataObject

    If data.GetDataPresent(DataFormats.Bitmap) Then

    img = CType(data.GetData(DataFormats.Bitmap), Image)

    img.Save(Filename)

    End If

    End Sub

    But if I change the format to do it as a picture, and printer format the img object never get set

    The img.save(Filename) give "Object reference not set to an instance of an object"

     

    For Each MyWrkSheet In myWrkBk.Worksheets

    MyWrkSheet.Range("C3:L9").CopyPicture(Excel.XlPictureAppearance.xlPrinter, Excel.XlCopyPictureFormat.xlPicture)

    SaveToWMF(OutputPath & "\tmp.wmf")    <- I had this line wrong, I just corrected it.

    Next MywrkSheet

    Private Sub SaveToWMF(ByVal Filename As String)

    Dim img As Image

    Dim data As IDataObject

    data = Clipboard.GetDataObject

    If data.GetDataPresent(DataFormats.Bitmap) Then

    img = CType(data.GetData(DataFormats.MetafilePict), Image)

    img.Save(Filename)

    End If

    Wednesday, December 6, 2006 8:01 PM

Answers

  • That's not going to fly.  You ask the clipboard if it has image data available in the .bmp format.  It says "sure", then you ask for a MetaFile.  It says "no way" == null.  A bitmap can't be converted into a .wmf (or .emf), it is a fundamentally different format.  Bitmap graphics vs. vector graphics.   Excel might be a little too helpful here by converting the metafile into a bitmap for you.  Debug it by calling Clipboard.GetDataObject().GetFormats().  One of them might be metafile compatible...
    Wednesday, December 6, 2006 10:27 PM
    Moderator

All replies

  • That's not going to fly.  You ask the clipboard if it has image data available in the .bmp format.  It says "sure", then you ask for a MetaFile.  It says "no way" == null.  A bitmap can't be converted into a .wmf (or .emf), it is a fundamentally different format.  Bitmap graphics vs. vector graphics.   Excel might be a little too helpful here by converting the metafile into a bitmap for you.  Debug it by calling Clipboard.GetDataObject().GetFormats().  One of them might be metafile compatible...
    Wednesday, December 6, 2006 10:27 PM
    Moderator
  • I did try the GetFormats and found that the

    MyWrkSheet.Range("C3:L9").CopyPicture(Excel.XlPictureAppearance.xlPrinter, Excel.XlCopyPictureFormat.xlPicture)

    does put a "EnhancedMetafile", "MetafilePict" format on the clipboard.  So I changed the code to be

    MyMeta = data.GetData(DataFormats.EnhancedMetafile)

    MyMeta.Save(Filename)

    But it still gives me the same error.

    Thursday, December 7, 2006 2:27 PM