Word VBA Problem

  • Thread starter Peter L Kessler
  • Start date
P

Peter L Kessler

Hello

I've been maintaining a set of Word VBA macros for a few years at the
office, and have a routine which prints the current document to a pre-set
printer and tray(s). The pre-sets have been saved in the Registry, so the
routine pulls these out and uses them to reset the ActivePrinter, print the
document and then reset the ActivePrinter from saved data.

This works fine for Word 97 (on Win 98 & 2000) and Word 2000 (Win2000 & XP).
Word 2002 (Win XP) crashes completely (not just a VBA error, but Word
itself) when it reaches the line which alters the DefaultTray to the saved
setting. I have pasted the essential elements of the routine below. I'm
hoping someone will be able to tell me why this is happening with the "new
and improved" Word, when the old versions work quite happily with it. Any
help is greatly appreciated.

Best wishes
Peter Kessler

Kessler Associates
E: (e-mail address removed)
W: http://www.digital.kessler-web.co.uk





Public PrintDocument()

Dim SelectedComboBox As String

'Get the saved printer and tray settings from the Registry
cboCInvPrint1 = System.PrivateProfileString("", lsection,
"CInvPrint1")
cboCInvPrint2 = System.PrivateProfileString("", lsection,
"CInvPrint2")
cboCInvTray1 = System.PrivateProfileString("", lsection,
"CInvTray1")
cboCInvTray2 = System.PrivateProfileString("", lsection,
"CInvTray2")

SelectedComboBox = cboInvPrint1
ComboBoxChange

' Set Tray 1 for Page 1
If cboInvTray1 <> "" Then
With Options
.DefaultTray = cboInvTray1 '******************** Word
crashes here
End With

'Routine then prints page 1...

End Sub

Public ComboBoxChange()
With Dialogs(wdDialogFilePrintSetup)
.Printer = SelectedComboBox
.DoNotSetAsSysDefault = True
.Execute
End With
End Sub
 
Z

z

I'm a little confused what exactly the registry entries
contain. You didn't specify if the OS was changed also,
but try inserting some error checking and verify that the
registry setting came in correctly:

On Error Goto Problem
' Set Tray 1 for Page 1
If cboInvTray1 <> "" Then
With Options
.DefaultTray = cboInvTray1
'crashes here
End With

Problem:

Msgbox cboInvTray1
Exit Sub
-----Original Message-----
Hello

I've been maintaining a set of Word VBA macros for a few years at the
office, and have a routine which prints the current document to a pre-set
printer and tray(s). The pre-sets have been saved in the Registry, so the
routine pulls these out and uses them to reset the ActivePrinter, print the
document and then reset the ActivePrinter from saved data.

This works fine for Word 97 (on Win 98 & 2000) and Word 2000 (Win2000 & XP).
Word 2002 (Win XP) crashes completely (not just a VBA error, but Word
itself) when it reaches the line which alters the DefaultTray to the saved
setting. I have pasted the essential elements of the routine below. I'm
hoping someone will be able to tell me why this is happening with the "new
and improved" Word, when the old versions work quite happily with it. Any
help is greatly appreciated.

Best wishes
Peter Kessler

Kessler Associates
E: (e-mail address removed)
W: http://www.digital.kessler-web.co.uk





Public PrintDocument()

Dim SelectedComboBox As String

'Get the saved printer and tray settings from the Registry
cboCInvPrint1 =
System.PrivateProfileString("", lsection,
"CInvPrint1")
cboCInvPrint2 =
System.PrivateProfileString("", lsection,
"CInvPrint2")
cboCInvTray1 = System.PrivateProfileString ("", lsection,
"CInvTray1")
cboCInvTray2 = System.PrivateProfileString ("", lsection,
"CInvTray2")

SelectedComboBox = cboInvPrint1
ComboBoxChange

' Set Tray 1 for Page 1
If cboInvTray1 <> "" Then
With Options
.DefaultTray =
cboInvTray1 '******************** Word
 
P

Peter L Kessler

Hi "z"

I'd almost given up on getting an answer! The routine's registry entries
contain previously save printer and tray settings. These are read back in so
that the user only has to set these once.

The OS that's installed on the problem PCs is Win XP (running Word 2002).
The routine always worked on Win 2000, with Word 97 or 2000.

The routine already has error checking, but when Word 2002 crashes the whole
programme goes, not the routine. Word itself crashes and prompts to send
Microsoft an error report each time. The error checking doesn't seem to get
a chance to run.

Best wishes
Peter Kessler

Kessler Associates
E: (e-mail address removed)
W: http://www.kessler-web.co.uk
 
J

Jonathan West

Hi Peter,

Its possible that there is some corruption in the template. Try copying the
code out of the template into a new one, and running it from there.
 
P

Peter L Kessler

Hello Jonathan

You were right. I transferred enough of the routine, including the element
which always causes the crash, to a new module in a new document, and
stepped through it. It worked just fine, which leaves me with a bit of a
headache regarding the original module. It is one of a dozen in a template
which also has about a dozen user forms. Do I have to recreate them all, or
will a simple export/re-import work to solve the problem. I can't see
anything wrong with the code, and it still works in Word 97 on Win 20000, so
I'm hoping you'll say "export and import."

Best wishes
Peter Kessler

Kessler Associates
E: (e-mail address removed)
W: http://www.kessler-web.co.uk
 
P

Peter L Kessler

Hi Again

Unfortunately, I've answered my own question and it's not looking good.

I exported all the forms and modules from my template, created a new
document and imported them all into it. Then I spent half an hour recreating
the attached toolbar, and working out which References needed to be added.
After that, the templates, re-installed on my own Word 97 system, work as
before. But, installed on a Word 2002 machine, they still crash.

Thinking I might have imported the corruption with the problem-specific
module, I copied the script, created a new module and pasted this in. Once
again, it works in Word 97 but not in Word 2002. It also used to work in
Word 2000, but I don't have any Word 2000 systems available to me any more.

I'm scratching my head over this. Do you have any ideas to help me?

Best wishes
Peter Kessler

Kessler Associates
E: (e-mail address removed)
W: http://www.kessler-web.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top