Solved

Burn a CD from VBA

Posted on 2014-04-13
20
572 Views
Last Modified: 2014-04-28
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
0
Comment
Question by:anthonytr
  • 11
  • 5
20 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39998452
The code you posted is only partial. You have to use SendKeys to fill out the CD Name.

This a adapted version of what you are trying to do, and hopefully will work.
Option Explicit

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

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.Namespace(59).Self.Path
    Set objFolder = objShell.Namespace(strSourceDirectory)
    objShell.Namespace(strBurnDirectory).CopyHere objFolder.Items
    objShell.Namespace(&H11&).ParseName(strDriveLetter).InvokeVerbEx ("Write &these files to CD")
        Do Until WshShell.AppActivate("CD Writing Wizard")
        Sleep 200
    Loop
    WshShell.AppActivate ("CD Writing Wizard")
    Sleep 1000
    SendKeys strCDName
    WshShell.AppActivate ("CD Writing Wizard")
    SendKeys "{Enter}"
    Do Until Not WshShell.AppActivate("CD Writing Wizard")
        Sleep 200
    Loop
    
End Sub

Open in new window


Note: Be forewarned SendKeys is not very reliable.
0
 

Author Comment

by:anthonytr
ID: 39998467
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.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39998491
First check this code out, if it doesn't work we can try the IMAPI route.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:anthonytr
ID: 39998633
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

0
 
LVL 27

Expert Comment

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

Open in new window

0
 

Author Comment

by:anthonytr
ID: 39998864
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.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39998936
Increase the sleep time on line 25, try 5000 (5 seconds), you can lower it if it works.
0
 

Author Comment

by:anthonytr
ID: 39998955
Nope.  It still does not populate the CD name correctly and MS access freezes.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39999022
Add WShshell. before all occurrences of SendKeys. i.e. WshShell.SendKeys
0
 

Author Comment

by:anthonytr
ID: 39999031
Still no joy...
0
 

Author Comment

by:anthonytr
ID: 39999466
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.
0
 

Author Comment

by:anthonytr
ID: 39999539
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

0
 

Author Comment

by:anthonytr
ID: 39999863
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.
0
 

Author Comment

by:anthonytr
ID: 39999914
Here is the new question:

ID28412358
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40001907
:)

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

Nick67
0
 

Author Comment

by:anthonytr
ID: 40006888
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
0
 

Author Comment

by:anthonytr
ID: 40012862
yes it did
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

679 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