Solved

Updating IMAPI code for CD Burning

Posted on 2014-04-14
9
1,180 Views
Last Modified: 2014-05-03
Hi,

After a lot of net searching I finally found some code which looks promising and to my eyes looks as though it could be just what I have been looking for for my project.

I have been trying to automate the CD Burning process from within my MS Access database and have been playing around with a number of options, but nothing has quite worked yet.

In my database, every Batch_Reference also has a link to a Dir which contains a number of scanned .tiff files.  Once the batch has been completed the user clicks to burn batch to CD, inserts and CD and the Dir is burned straight onto the CD-R or DVD-R.  The CD is also given the name of the Batch_Reference.

The Following code I found, successfully burns the CD, however, it burns a multi-sessions CD and does not Finalize the CD.  Also, I can't seem to find how to manage the naming of the CD from within the code.  

It seems that this example is oh so close to what I have been searching for and yet so far.  I really do someone can help me out with this project.

Option Compare Database
Option Explicit

Sub TestCDWrite()
  Dim objDiscMaster As IMAPI2.MsftDiscMaster2
  Dim objRecorder As IMAPI2.MsftDiscRecorder2
  Dim DataWriter As IMAPI2.MsftDiscFormat2Data
  Dim intDrvIndex As Integer
  'The Object browser, but not intellisense, presents types for these, but they cannot be used in VBA
  Dim stream As Variant
  Dim FS As Variant
  Dim Result As Variant
  Dim FSI As Object
  Dim strBurnPath As String
  Dim strUniqueID As String
  
  ' *** CD/DVD disc file system types
  Const FsiFileSystemISO9660 = 1
  Const FsiFileSystemJoliet = 2
  Const FsiFileSystemUDF102 = 4
  
  'On Error GoTo TestCDWrite_Error
  
  intDrvIndex = 0
  strBurnPath = "C:\TempPDF"  'I will eventually set this via a variable
  ' Create a DiscMaster2 object to connect to optical drives.
  Set objDiscMaster = New IMAPI2.MsftDiscMaster2
  
  ' Create a DiscRecorder2 object for the specified burning device.
  Set objRecorder = New IMAPI2.MsftDiscRecorder2
  
  strUniqueID = objDiscMaster.Item(intDrvIndex)
  objRecorder.InitializeDiscRecorder (strUniqueID)

  ' Create a DiscFormat2Data object and set the recorder
  Set DataWriter = New IMAPI2.MsftDiscFormat2Data
  DataWriter.recorder = objRecorder
  DataWriter.ClientName = "IMAPIv2 TEST"

  ' Create a new file system image object
  Set FSI = New IMAPI2FS.MsftFileSystemImage
    
  ' Import the last session, if the disc is not empty, or initialize
  ' the file system, if the disc is empty
  If Not DataWriter.MediaHeuristicallyBlank Then
    On Error Resume Next
    FSI.MultisessionInterfaces = DataWriter.MultisessionInterfaces
    If Err.Number <> 0 Then
      Call MsgBox("Multisession is not supported on this disc", vbExclamation, "Data Archiving")
      GoTo ExitHere
    End If
    On Error GoTo 0
    Call MsgBox("Importing data from previous session ...", vbInformation, "Data Archiving")
    FS = FSI.ImportFileSystem()
  Else
    FS = FSI.ChooseImageDefaults(objRecorder)
  End If

  ' Add the directory and its contents to the file system
  Call MsgBox("Adding " & strBurnPath & " folder to the disc...", vbInformation, "Data Archiving")
  FSI.Root.AddTree strBurnPath, False

  ' Create an image from the file system image object
  
  Set Result = FSI.CreateResultImage()
  Set stream = Result.ImageStream
  
  ' Write stream to disc using the specified recorder
  Call MsgBox("Writing content to the disc...", vbInformation, "Data Archiving")
  DataWriter.Write (stream)

  Call MsgBox("Completed writing Archive data to disk ", vbInformation, "Data Archiving")
  
ExitHere:
  Exit Sub
'Error handling block
TestCDWrite_Error:
  Select Case Err.Number
   Case Else
   MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "TestCode.TestCDWrite"
  End Select
  Resume ExitHere
'End Error handling block

End Sub

Open in new window

0
Comment
Question by:anthonytr
  • 5
  • 4
9 Comments
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40001867
LOL

The odds that you haven't come across my stuff before are pretty low.
"C:\TempPDF" is very familiar :)
But I never bother to put all the disclaimer stuff in the samples I post

I didn't originate the code, but I can see that you have altered what I've got.
Now, where you have
 ' Create a new file system image object
  Set FSI = New IMAPI2FS.MsftFileSystemImage


I have more:
 ' Create a new file system image object
  'Set FSI = New IMAPI2FS.MsftFileSystemImage
  Set FSI = CreateObject("IMAPI2FS.MsftFileSystemImage")
  FSI.VolumeName = "WO " & Forms!frmJobID![WO#] & " " & Format(Forms!frmJobID![Date], "mmm yyyy")


So, to name the disk, you need
FSI.VolumeName = "SomeStringYou'dLikeToNameIt"

Mine finishes the disks and spits them out but where you have
 Call MsgBox("Completed writing Archive data to disk ", vbInformation, "Data Archiving")
 


I have:
 Call MsgBox("Completed writing Archive data to disk ", vbInformation, "Data Archiving")
  objRecorder.EjectMedia


I didn't write the code we are using, and I don't pretend to fully understand it.  But spitting the disk out is part-and-parcel of finalizing it, and that may do it.

This is the guts of the code that chooses the disk type
 ' Import the last session, if the disc is not empty, or initialize
  ' the file system, if the disc is empty
  If Not DataWriter.MediaHeuristicallyBlank Then
    On Error Resume Next
    FSI.MultisessionInterfaces = DataWriter.MultisessionInterfaces
    If Err.Number <> 0 Then
      Call MsgBox("Multisession is not supported on this disc", vbExclamation, "Data Archiving")
      GoTo ExitHere
    End If
    On Error GoTo 0
    Call MsgBox("Importing data from previous session ...", vbInformation, "Data Archiving")
    fs = FSI.ImportFileSystem()
  Else
    fs = FSI.ChooseImageDefaults(objRecorder)
   
  End If


If you are putting in a blank CD then the operative bit is
fs = FSI.ChooseImageDefaults(objRecorder)
If that is not giving you what you want, we'll have to dig deeper into what the code actually means and does.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40001913
Here is a verbose explanation of the code we are using
http://www.aspfree.com/c/a/windows-scripting/burning-cds-with-the-imapi2-control/

That site explains how to substitute ChooseImageDefaults with FileSystemsToCreate  to force a particular filesystem -- but I don't know if that is needful.  My reading seems to indicate that, if the burner supports UDF, it will put UDF on the disk -- and that by default Joliet and ISO9660 get put on at the same time for maximal backward compatibility.  The failure to finalize the disk comes from failing to eject it.

Ejecting the disk alone may be the ticket.
0
 

Author Comment

by:anthonytr
ID: 40001981
Hi,

It's great to get a response from you - thanks!

I have changed a few more sections of the code:

Sub TestCDWrite()
  Dim objDiscMaster As IMAPI2.MsftDiscMaster2
  Dim objRecorder As IMAPI2.MsftDiscRecorder2
  Dim DataWriter As IMAPI2.MsftDiscFormat2Data
  Dim intDrvIndex As Integer
  'The Object browser, but not intellisense, presents types for these, but they cannot be used in VBA
  Dim stream As Variant
  Dim FS As Variant
  Dim Result As Variant
  Dim FSI As Object
  Dim strBurnPath As String
  Dim strUniqueID As String
  
  ' *** CD/DVD disc file system types
  Const FsiFileSystemISO9660 = 1
  Const FsiFileSystemJoliet = 2
  Const FsiFileSystemUDF102 = 4
  
  'On Error GoTo TestCDWrite_Error
  
  intDrvIndex = 0
  strBurnPath = "C:\Toburn"
  ' Create a DiscMaster2 object to connect to optical drives.
  Set objDiscMaster = New IMAPI2.MsftDiscMaster2
  
  ' Create a DiscRecorder2 object for the specified burning device.
  Set objRecorder = New IMAPI2.MsftDiscRecorder2
  
  strUniqueID = objDiscMaster.Item(intDrvIndex)
  objRecorder.InitializeDiscRecorder (strUniqueID)

  ' Create a DiscFormat2Data object and set the recorder
  Set DataWriter = New IMAPI2.MsftDiscFormat2Data
  DataWriter.Recorder = objRecorder
  DataWriter.ClientName = "IMAPIv2 TEST"
  DataWriter.ForceMediaToBeClosed = True
  
  
  ' Create a new file system image object
  Set FSI = New IMAPI2FS.MsftFileSystemImage
    
  
    FS = FSI.ChooseImageDefaults(objRecorder)
  

  ' Add the directory and its contents to the file system
  Call MsgBox("Adding " & strBurnPath & " folder to the disc.  Press OK to continue.", vbInformation, "Burn Batch to CD")
  FSI.Root.AddTree strBurnPath, False

  ' Create an image from the file system image object
  
  Set Result = FSI.CreateResultImage()
  Set stream = Result.ImageStream
  
  ' Write stream to disc using the specified recorder
  Call MsgBox("Burn Batch to disc.  Press OK to continue.", vbInformation, "Burn Batch to CD")
  DataWriter.Write (stream)
    
  Call MsgBox("Burn process completed.", vbInformation, "Burn Batch to CD")
  
ExitHere:
  Exit Sub
'Error handling block
TestCDWrite_Error:
  Select Case Err.Number
   Case Else
   MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "TestCode.TestCDWrite"
  End Select
  Resume ExitHere
'End Error handling block

End Sub

Open in new window


What I found was I needed to include:

DataWriter.ForceMediaToBeClosed = True

in order to Finalize the CD.  I like the "Spitting Out" of the CD line in your code and will include this in my code too.

I thought that:

 DataWriter.ClientName = "IMAPIv2 TEST"

was where the CD was being named.  Thank you for your suggestion, I'll put that into my code too.

Have you been able to catch the error if a CD is not in the drive when a user runs the Burn Process?  At the moment i get a Run-Time error and it debugs.

From what I am seeing, my test Burn CD's are in UDF format.  As I'm burning CD-R or DVD-R's only this isn't an issue, is it?

Thanks again fro your response.  I'll get back to you regarding my alterations and finds.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40002013
We've got the error handling block, although I see you've got it commented out at the moment. So do I.  I prefer that the end-user has to come running to me when things go BANG!
'On Error GoTo TestCDWrite_Error

There's at least five different errors that can get thrown

Full disk
No disk
Incompatible disk
Upside down disk
Too small a disk

If you really want to handle them gracefully, you'll need a

Select Case Err.Number
    Case WhateverNumberIsNoDisk
       Msgbox "Put in a disk, dummy!"
    Case WhateverNumberIsFullDisk
       Msgbox "

....
End Select


And you'll have to test each of the five to get the error number (they may all be the same!) that gets thrown.

Myself, I expect the user to be smart enough to throw in a fresh disk when they are burning one :)

MS has code here about checking for media support
http://msdn.microsoft.com/en-us/library/aa364821(v=vs.85).aspx
It's vbscript like all this code was originally (am I the only person who's ever bothered to write CD's with MS Access VBA and you are now the second?) But you may be able to adapt it to VBA

It appears that
If dataWriter.CurrentPhysicalMediaType = 0 Then
        Msgbox "Put in a disk, dummy!"
        exit sub
end if


might also do it without allowing an error to occur.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:anthonytr
ID: 40002910
I'm working through my error trapping - thanks for all the information.

I have been looking at the 'Monitoring Progress with Events' section on the MSDN site,  Are you  able to help convert this to fit into my VBA project?  I really don't know how to.  

I would just like to inform the user as to what is currently happening during the burn process, just so they know it's doing something and has not just stalled.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40004627
That's past my skill level.
It looks like the datawriter object has events, but whether and how you can hook those events to VBA is an open question.
Instead of declaring
Dim DataWriter As IMAPI2.MsftDiscFormat2Data
in the sub, you may be able to declare as a public module variable
Public DataWriter As IMAPI2.MsftDiscFormat2Data
You may then be able to write
Public Sub DataWriter_Update()

But like I said, that's way past me, man
0
 

Author Comment

by:anthonytr
ID: 40004651
Don't worry about it :)  

You have been VERY helpful already and I very much appreciate your help.  The code I now have works perfectly well (with event notification, etc).  I'll continue working on it - slowly.

Thank you again for your help.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40004787
This site is in German
http://www.office-loesung.de/ftopic553388_0_0_asc.php

But the upshot is that if you create the datawriter as a class, WithEvents, you can then access the events of the datawriter.  Good luck.
0
 

Author Comment

by:anthonytr
ID: 40004800
Not only do I have to start learning how to convert VB to VBA, but I now have to learn German too!

...only joking.  I will look into it - thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now