Link to home
Start Free TrialLog in
Avatar of anthonytr
anthonytrFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Burn a CD from VBA

Hi,

I have asked a question regarding this code previously, however, I seem to have another issue with it.  I don't remember where I sourced the code from - sorry.

Private Sub cmd_burn_cd_Click()

     'Provide the drive letter of your CD burner
strDriveLetter = InputBox("Driveletter: ", "Driveletter", "D") & ":\"
     'Provide the source directory. We made one on the c drive
     'and everything we put in this directory will be put on a cd
strSourceDirectory = Me.batch_output_location
     'Provide a volume name for your CD (16 characters max)
strCDName = Me.batch_reference
Const MY_COMPUTER = &H11
Set WShshell = CreateObject("WScript.Shell")
Set objShell = CreateObject("Shell.Application")
strBurnDirectory = WShshell.RegRead( _
"HKCU\Software\Microsoft\Windows\CurrentVersion\" _
& "Explorer\Shell Folders\CD Burning")
Set objFolder = objShell.Namespace(strSourceDirectory)
objShell.Namespace(strBurnDirectory).CopyHere objFolder.Items
objShell.Namespace(&H11&).ParseName(strDriveLetter).InvokeVerbEx ( _
"Write &these files to CD")
End Sub

Open in new window


I am trying to set the CD name which will be created/burned.  I have set:

strCDName = Me.batch_reference

When I finalise the CD to burn the CD is given the current date as the Name and asks the user to name the CD.  How can I pass my reference field to the Burn process accordingly?  I can't actually see where strCDName is being used in the code (apart from being set).

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of anthonytr

ASKER

Thank you for your response.  I'll check it out.

I also thank your for your advise regarding "Sendkeys".  I had been looking at using IMAPI2 to control the burn process, but could never get it to work, however, I then realised I had not referenced the IMAPI2.dll in my project.  Would you suggest pursuing the IMAPI route for this code or keep the one I already have?  All that is being burned is a folder full of .tiff files.

I can show you what code I have been playing with re IMAPI if it helps.

Thanks again.
First check this code out, if it doesn't work we can try the IMAPI route.
Hi,

I ran the code and it returns:

"Run-time error 438" Object does not support this property

and debugs to:

strBurnDirectory = WshShell.Namespace(59).Self.Path

Open in new window

Then try this line instead:
strBurnDirectory = WShshell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\CD Burning")

Open in new window

Hi,

That got the process going, without the error, however, the CD name referenced a:

 strCDName = Me.batch_reference

does not get used and the default "Current Date" is appended.  When I cancel the process without burning the CD it stall MS Access.
Increase the sleep time on line 25, try 5000 (5 seconds), you can lower it if it works.
Nope.  It still does not populate the CD name correctly and MS access freezes.
Add WShshell. before all occurrences of SendKeys. i.e. WshShell.SendKeys
Still no joy...
Perhaps I do need to look into using IMAPI2 control to burn the CD or something.

Simply put, I need to burn the contents of a DIR to CD.  In my system there is a button which when pressed, checks to see if there is DIR saved in the database and for the reference ID and then burns the contents to CD.  I'm trying to to make it as automatic as possible so as to reduce the input from the user.

Having been messing with the built in Burn Process and the above code, there are a number of moments when the user has to take control (clicking the balloon popup to activate the burn, re entering the CD reference (CDName) and selecting whether the CD will be finalised or remain open (like a USB Stick).  In my project, this could cause problems if a user clicks the wrong thing or names the CD incorrectly.

Any thoughts as to where I should start looking?  Any suggestions most welcome.
This is the code I have been looking at, but I'm not sure if it's a no go:

' This script burns data files to disc in a single session
' using files from a single directory tree.
 
' Copyright (C) Microsoft Corp. 2006

Option Explicit

' *** CD/DVD disc file system types
Const FsiFileSystemISO9660 = 1
Const FsiFileSystemJoliet = 2
Const FsiFileSystemUDF102 = 4

WScript.Quit (Main)

Function Main()
    Dim Index                ' Index to recording drive.
    Dim Recorder             ' Recorder object
    Dim Path                 ' Directory of files to burn
    Dim Stream               ' Data stream for burning device
    
    Index = 1                ' Second drive on the system
    Path = "C:\TOBURN"      ' Files to transfer to disc

    ' Create a DiscMaster2 object to connect to optical drives.
    Dim g_DiscMaster
    Set g_DiscMaster = WScript.CreateObject("IMAPI2.MsftDiscMaster2")

    ' Create a DiscRecorder object for the specified burning device.
    Dim uniqueId
    Set Recorder = WScript.CreateObject("IMAPI2.MsftDiscRecorder2")
    uniqueId = g_DiscMaster.Item(Index)
    Recorder.InitializeDiscRecorder (uniqueId)

    ' Create an image stream for a specified directory.
    Dim FSI                  ' Disc file system
    Dim Dir                  ' Root directory of the disc file system
    Dim dataWriter
        
    ' Create a new file system image and retrieve root directory
    Set FSI = CreateObject("IMAPI2FS.MsftFileSystemImage")
    Set Dir = FSI.Root

    'Create the new disc format and set the recorder
    Set dataWriter = CreateObject("IMAPI2.MsftDiscFormat2Data")
    dataWriter.Recorder = Recorder
    dataWriter.ClientName = "IMAPIv2 TEST"

    FSI.ChooseImageDefaults (Recorder)
        
    ' Add the directory and its contents to the file system
    Dir.AddTree Path, False
        
    ' Create an image from the file system
    Dim result
    Set result = FSI.CreateResultImage()
    Stream = result.ImageStream
    
    ' Write stream to disc using the specified recorder.
    WScript.Echo "Writing content to disc..."
    dataWriter.Write (Stream)

    WScript.Echo "----- Finished writing content -----"
    Main = 0
End Function

Open in new window

As this questions looks as though it might be taking another route, I think it might be well worth closing this thread and opening a new question.
Here is the new question:

ID28412358
:)

You've found my code on Google and I have replied to your new Q

Nick67
I actually requested this to be closed (not deleted - see ID: 39999863).  Not sure if that message was read.  

Not sure who eenookami is, I don't think they participated in this thread at all (i might be wrong).

It's not really a duplicate question per se, as the initial question and example code was using a different method in order to burn the CD.  As the thread developed it became clear that the code I started with wasn't going to fulfil my requirements and that I would need to look elsewhere, in particular the IMAPI2 framework.  The first answer from MacroShadow I'm sure would generate the CD name accordingly with a little more work on my part, but it was apparent that this was not going to be the route I would be taking.

The question, therefore does not need to be deleted, just closed.  Which is what I intend to do
yes it did