Get error "Word has encountered a problem..." when using File...New macro to toggle on/off Windows i

P

Pat Sullivan

Hi,
I get the error "Word has encountered a problem and needs to close. We are
sorry for the inconvenience." when I create a document from a template using
File...New. The error occurs after the AutoNew subroutine ends.

If I double-click the template file in Windows Explorer, the AutoNew
subroutine works fine. Opening a doucment that is attached to the template
(running the AutoOpen subroutine using the same code) works fine also.

I am using Word 2003, and the macro checks if the "Windows in Taskbar" is
checked. If so, I want to uncheck it so that a Userform that opens multiple
documents will remain in view. At the end, I want to re-check "Windows in
Taksbar", if that was the original user setting.

Does anyone know what is causing this behaviour & if there is a way around
it?

This is the code used in the template file .....

Public ShowTasks As Boolean
Dim WordVer As String
Dim VerNum As Double
Dim VerInt As Integer

Sub AutoOpen()
Call TurnOffSDI ' so Userform remains in view
'
' Code to ... Load Userform & open multiple documents
'
Call TurnOnSDI ' return to original user setting
End Sub

Sub AutoNew()
Call TurnOffSDI ' so Userform remains in view
'
' Code to ... Load Userform & open multiple documents
'
Call TurnOnSDI ' return to original user setting
End Sub

Sub TurnOffSDI()
'' If later than W2000 (or version 9.0)- turn off the SDI feature
' ---This allows Userform to remain in view while documents are opened under
Userform control
ShowTasks = False
WordVer = Application.Version ' get Word version#
VerNum = Val(WordVer) ' convert to decimal number & remove
possible alpha suffix(i.e. for SRs prior to W2000)
VerInt = Int(VerNum) ' truncate to get version# preceding
decimal (11=W2003,10=W2003,9=W2000,8=W97,7=W95)
If VerInt > 9 Then
Call SDIOffWord10 ' call if version later than Word2000
End If
End Sub

Sub TurnOnSDI()
' If later than W2000 (or version 9.0)- turn on the SDI feature
WordVer = Application.Version ' get Word version#
VerNum = Val(WordVer) ' convert to decimal number & remove
possible alpha suffix(i.e. for SRs prior to W2000)
VerInt = Int(VerNum) ' truncate to get version# preceding
decimal (11=W2003,10=W2003,9=W2000,8=W97,7=W95)
If VerInt > 9 Then
Call SDIOnWord10 ' call if version later than Word2000
End If
End Sub

Sub SDIOffWord10()
' ShowTasks is true if user setting at Tools|Options|View...Windows in
Taskbar is checked
If Application.ShowWindowsInTaskbar Then ShowTasks = True
Application.ShowWindowsInTaskbar = False
End Sub

Sub SDIOnWord10()
' ShowTasks is true if user setting at Tools|Options|View...Windows in
Taskbar is checked
If ShowTasks Then Application.ShowWindowsInTaskbar = True
End Sub
 
B

Beth Melton

First I'd shorten your code. :)

It looks like all you really need is:

'To retain current settings pass the current value of the option to
your variable
ShowTasks = Application.ShowWindowsInTaskbar

'To turn off option for versions greater than 9 (no need to convert to
an integer)
If Val(Application.Version) > 9 Then
Application.ShowWindowsInTaskbar = False
end if

' Set the option back to the user's preference using the previously
stored True/False value
Application.ShowWindowsInTaskbar=ShowTasks

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
P

Pat Sullivan

Thanks Beth,
Part of the reason for the extra code is that I want it to run on both W2000
and W2003. Since the Application.ShowWindowsInTaskbar command is not in
W2000, I'm trying to isolate these commands in subroutines that will only be
run in W2000+.

The reason why this code only causes me a problem in the AutoNew subroutine
is still baffling to me.

Pat Sullivan
 
B

Beth Melton

Right - only turn it off for versions greater than 9 which are Word
2002 (10) and Word 2003 (11). The code I provided removes the need
for the additional subroutines. I've used similar code in several
add-ins which run on multiple versions of Word.

I can't reproduce the error on my end so I was wondering if it could
be due to your calls to your subroutines and reducing your code could
rectify the issue. Otherwise without seeing all of the code I don't
know what to suggest.

It could also be a timing issue, although usually in these cases you
encounter the reverse: it errors when opening outside the application
but runs fine when using File/New.

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
P

Pat Sullivan

OK, I'll try that. Thanks again for your help.

Beth Melton said:
Right - only turn it off for versions greater than 9 which are Word 2002
(10) and Word 2003 (11). The code I provided removes the need for the
additional subroutines. I've used similar code in several add-ins which
run on multiple versions of Word.

I can't reproduce the error on my end so I was wondering if it could be
due to your calls to your subroutines and reducing your code could rectify
the issue. Otherwise without seeing all of the code I don't know what to
suggest.

It could also be a timing issue, although usually in these cases you
encounter the reverse: it errors when opening outside the application but
runs fine when using File/New.

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 

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