views:

13

answers:

1

I need to import PDF and XLS objects and convert them to binary. The following code doesn't work, apparently because the "fd.SelectedItems(1)" is the path of the object instead of the object itself.

If I dim "fileToUpload" as an object, I get a "run time '91' Object variable or With block variable not set".

If I don't specifically Dim "fileToUpload", when I get to the last line below I get "run time '424' Object required".

Anyone know the magic words?

  Dim fd As FileDialog
  Dim ImageToBytes() As Byte
  Dim ImageCode As String

  FilePickerControl = False
  Set fd = Application.FileDialog(msoFileDialogFilePicker)
  fd.AllowMultiSelect = False
  If fd.Show = -1 Then
     FileToUpload = fd.SelectedItems(1)
  End If

  ImageToBytes = System.IO.File.ReadAllBytes(FileToUpload) 
A: 

What is FileToUpload supposed to be? If you dim it as an object, you need to SET it, i.e., SET FileToUpload = fd.SelectedItems(1), but I don't think that's necessarily thecorrect thing, as so far as I can tell, fd.SelectedItems(1) is going to return a string value that has the file name/path, not an actual file object. Looks to me like it should be a string.

Perhaps this will work:

  Dim fd As FileDialog
  Dim strFileToUpload As String
  Dim ImageToBytes() As Byte
  Dim ImageCode As String

  FilePickerControl = False
  Set fd = Application.FileDialog(msoFileDialogFilePicker)
  fd.AllowMultiSelect = False
  If fd.Show = -1 Then
     strFileToUpload = fd.SelectedItems(1)
  End If

  ImageToBytes = System.IO.File.ReadAllBytes(strFileToUpload) 
David-W-Fenton
David, many thanks for the response. When I got to the last line I got "run time '424' Object required". strFileToUpload is the path and I think I need strFileToUpload to be the PDF object itself. Any idea how I can get that?
jrc
Well, I have no idea what System.IO.File.ReadAllBytes is, so you'll have to see what ReadAllBytes expects for its data structure. My suspicion is that you've got File System Object code mixed in here, and you may need a file object from the FSO. But I'm just guessing there.
David-W-Fenton