I have been experiencing exactly the same problem, but it only occurs on certain machines. I managed to recreate the problem with the following steps:
- Using Microsoft Virtual PC 2007, install a clean build of XP Pro.
- Without installing anything else, other than the VM Addons, apply all of the MS Updates.
- Run the code as described above. I have provided an alternative below too.
It would seem that the problem has occurred through an MS update. Using the KB240797 knowledge base article (http://support.microsoft.com/default.aspx/kb/240797?p=1), I was able to determine that a kill bit had been issued for the Common Dialog ActiveX control, and an alternative class ID provided (8F0F480A-4366-4737-8265-2AD6FDAC8C31), suggesting that the control had been superseded. However, after checking other machines on which the control worked, specifically development machines (XP Pro x64, XP Pro x32 & Vista x32), each with a copy of Visual Studio 2008 installed, there was no kill bit entry nor was there an alternative class ID.
To get the dialog working, I simply renamed the registry key (I could have deleted it as well), meaning that there was no longer a kill bit. Hey presto, it worked!
The registry key is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet
Explorer\ActiveX
Compatibility{F9043C85-F6F2-101A-A3C9-08002B2F49FB}
I guess that there is a chance that this will be re-applied with a future Microsoft cumulative update, and it may also open up some security vulnerability.
I hope that that helps you. Thanks for your original post as it seems to have set me on the right track.
Kaine
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Common Dialog Test</title>
<script language="vbscript" type="text/vbscript">
Sub AlertErr()
On Error Resume Next
document.objComDlg.Copies = 1
document.objComDlg.FromPage = 1
document.objComDlg.ToPage = 1
document.objComDlg.Min = 1
document.objComDlg.Max = 1
document.objComDlg.Flags = cdlPDHidePrintToFile Or cdlPDNoSelection
document.objComDlg.CancelError = True
printerDialog = document.objComDlg.ShowPrinter
If Err.Number = 0 Then
Call MsgBox("No Error. The print simulation worked as expected.")
ElseIf Err.Number = 32755 Then
Call MsgBox("You clicked the 'Cancel' button.")
Else
Call MsgBox("The following error occurred: " & Err.Description & " (" & Err.Number & ")")
End If
End Sub
</script>
</head>
<body>
<div>
<button id="btnAlertErr" onclick="AlertErr()">Print Me</button>
<object classid="clsid:5220cb21-c88d-11cf-b347-00aa00a28331">
<param name="LPKPath" value="CommonDialog.lpk">
</object>
<object id="objComDlg" codebase="http://activex.microsoft.com/controls/vb6/comdlg32.cab" classid="clsid:F9043C85-F6F2-101A-A3C9-08002B2F49FB" viewastext>
<param name="_ExtentX" value="847" />
<param name="_ExtentY" value="847" />
<param name="_Version" value="393216" />
<param name="_Version" value="393216" />
<param name="CancelError" value="0" />
<param name="Color" value="0" />
<param name="Copies" value="1" />
<param name="DefaultExt" value="" />
<param name="DialogTitle" value="" />
<param name="FileName" value="" />
<param name="Filter" value="" />
<param name="FilterIndex" value="0" />
<param name="Flags" value="0" />
<param name="FontBold" value="0" />
<param name="FontItalic" value="0" />
<param name="FontName" value="" />
<param name="FontSize" value="8" />
<param name="FontStrikeThru" value="0" />
<param name="FontUnderLine" value="0" />
<param name="FromPage" value="0" />
<param name="HelpCommand" value="0" />
<param name="HelpContext" value="0" />
<param name="HelpFile" value="" />
<param name="HelpKey" value="" />
<param name="InitDir" value="" />
<param name="Max" value="0" />
<param name="Min" value="0" />
<param name="MaxFileSize" value="260" />
<param name="PrinterDefault" value="1" />
<param name="ToPage" value="0" />
<param name="Orientation" value="1" />
</object>
</div>
</body>
</html>