Consulting

Results 1 to 18 of 18

Thread: Whats wrong with this code??

  1. #1
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location

    Whats wrong with this code??

    Dim fsBLOBFile As System.IO.FileStream
    fsBLOBFile = System.IO.File.Open(Pict, FileMode.Open, FileAccess.Read)
    Dim br As System.IO.BinaryReader
    br = System.IO.BinaryReader(fsBLOBFile.Length)
    Dim bytBlobdata() As Byte
    byblobdata = br.ReadBytes(fsBLOBFile.Lenght)
    br.Close
    fsBLOBFile.Close
    I am getting a compiler error at Dim fsBLOBFile As System.IO.FileStream


    Thanks,
    Karen

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    What reference did you set in the VBE's Tools menu? When you did System., intellisense should have helped if your reference was set and proper.

    That looks like vb.net code. You will need to use another method. The file scripting method would work for this most likely.

    Some other parts are missing.

    Were you wanting to do this in vba or vb.net?

  3. #3
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    I havent set any reference i am a newbie in Vba... i want to do this in vba for excel.. What reference should i set and how??

    Thanks.

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Are you a vb.net coder then?

    It is better to explain what you need. We can then code a method to do it.

  5. #5
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    Yeah I am a VB.Net programmer and this is my vb.net code

    'Read jpg into file stream, and from there into Byte array. Dim fsBLOBFile As New FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read) Dim bytBLOBData As Byte() = New Byte(fsBLOBFile.Length - 1) {} fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length) fsBLOBFile.Close()


    Basically I am trying to convert a image as a blob.

    Thanks,
    Karen

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    It might be easier for you to just write the code as an EXE in vb.net. You can then use Shell() in VBA to execute it. For others to use your EXE, they will have to have the vb.net framework files of course.

    If you want to stick with an all VBA method, here is an example using the filescripting object. You will have to set the Reference first as I used an early binding method to allow intellisense to work. You can probably modify it from there.
    [vba]Sub text_Parse()
    Rem Needs Reference: MicroSoft Script Runtime, scrrun.dll
    Rem Instructions: http://support.microsoft.com/default...b;en-us;186118
    Dim FSO As New FileSystemObject
    Dim fstr As Scripting.TextStream
    Dim sData As String

    Set fstr = FSO.OpenTextFile("C:\your file.txt")

    Do Until fstr.AtEndOfStream
    sData = fstr.ReadLine

    If Mid(sData, 10, 9) = "Market " Then
    sNumber = Trim(Mid(sData, 1, 2))

    End If

    Loop

    End Sub[/vba]

  7. #7
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    Yeah I like the idea of writing it VB.net and then using Shell to call it... Can you give me some examples using shell in VBA..

  8. #8
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Shell is in vb.net. Like vb.net, in the VBE, type Shell and press F1 to browse the help for it.
    e.g.
    [vba]Sub Test()
    ChDir "c:\myfiles\Excel"
    Shell "cmd /c dir *.* > c:\temp\test.txt", vbNormal
    End Sub[/vba]
    You would probably use it something like:
    [vba]Sub RunMyEXEProgram()
    Shell ThisWorkbook.Path & "\VBNETProgramName.exe", vbNormal
    End Sub[/vba]

  9. #9
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    Well I tried putting that code in a asp.net program and then ran and it worked good and after inserting it into sql server and retreiveing the binary data from sql server it came up to 19 pages in word... and i found out that so much data cannot fit in a cell of a spread sheet. So is there any other work around???

  10. #10
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I am not sure how that relates to your question or what data if any that you are importing? Given that it seems that you want to poke alot of data into a cell, then I see three methods. Two methods involve using MSWord. If you need it in Excel, import the data to an MSWord object. Or, just import to MSWord and skip Excel.

    For the third method, you would need to parse the data. vbCrLF should be a good character to Split() data. Once the data is in an array, you can easily add it to multiple cells.

    There might be other methods but I would have to know the nature of the data and how you want it shown in Excel. I don't do sqlserver but I have done other database query's by various means such as ADO and Data > Import External Data.

  11. #11
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    Sorry for the confusion.. My ultimate goal is to import a picture from excel to sql server..so i thought if i could get the binary value of the picture and then import the binary value to sql server varbinary datatype and then display it in it a report

  12. #12
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Can you not just use the filename for the object in sqlserver? Here's how to save a picture object in Excel to a file.
    [VBA]SavePicture Sheet1.Image1.Picture, ThisWorkbook.Path & "\Image1.emf"
    [/VBA]

  13. #13
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    can u explain more about ur last comment.. kinda confused me.

  14. #14
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    Kenneth,
    I am trying to use this code in my spread but getting an error that says Expected function or variable

    folderName = SavePicture(ActiveSheet.filename, filename)

    filename is a string.. which is path for the File.. and ActiveSheet.filename is the Picture.

    any help will be appreciated.

    Thanks,
    Karen

  15. #15
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    SavePicture is a Sub so it returns no value.

    I would advise against naming objects the same as some other variable.

    So, you should probably do:
    [VBA]SavePicture ActiveSheet.filename.Picture, filename[/VBA]

    You can attach a short example xls if needed.

  16. #16
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    Thanks for the getting answer..

    I am getting an error that says runtime error 438 Object doesnt support this property or Method and its pointing to

    SavePicture ActiveSheet.filename.Picture, folderName

  17. #17
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    What is the value of folderName? Are you sure that the object name for the Picture object is named filename?

    A posted xls would show us what is going on.

  18. #18
    VBAX Regular
    Joined
    Nov 2008
    Posts
    21
    Location
    This is my code
    Dim Pict
    Dim ImgFileFormat As String
    Dim PictCell As Range
    Dim Ans As Integer
    Dim nextcell
    Dim IPict As Variant
    'ActiveSheet.Protect True, True, True, True, True
    ImgFileFormat = "Image Files (*.jpg),others, tif (*.tif),*.tif, jpg (*.jpg),*.jpg"
    GetPict:
    Pict = Application.GetOpenFilename(ImgFileFormat)
    'Note you can load in any nearly file format
    If Pict = False Then End
    'Ans = MsgBox("Open : " & Pict, vbYesNo, "Insert Picture")
    'If Ans = vbNo Then GoTo GetPict
    'Now paste to userselected cell
    GetCell:
    Set PictCell = Application.ActiveCell  'Application.InputBox("Select the cell to insert into", Type:=8)
    If PictCell.Count > 1 Then MsgBox "Select ONE cell only": GoTo GetCell
    PictCell.Select
    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim filename As String
    Dim folderName As String
    filename = FSO.GetBaseName(Pict)
     ActiveSheet.Pictures.Insert(Pict).Select
      
      Selection.Name = filename
      Selection.Height = Application.InchesToPoints(1)
      Selection.Width = Application.InchesToPoints(1.75)
      folderName = "c:\Temp\" & filename
      SavePicture Selection.Name.Picture, folderName

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •