Log in

View Full Version : Run-time error 5947 Word 2010 - why?



HelenT
11-07-2016, 05:24 AM
Hi,

I have a start-up template which initialises a custom tab on the ribbon. On this tab is a drop-down menu from which the user can choose to attach one of 4 templates. Irrespective of which template is chosen, the first time a template is chosen from the drop-down menu, the user gets:

"Run-time error 5947. Could not change document template."

However, subsequent attempts to change the template (by choosing a different option on the drop-down menu) are always successful. The error occurs on the first attempt only, irrespective of which template is chosen.

I have included the call back routine which handles the Drop-down menu on the ribbon:

'Callback for DropDown onAction

Sub AttachTemplate(ByVal control As IRibbonControl, selectedID As String, selectedIndex As Integer)
Dim DummyVar As Integer

Select Case selectedIndex

Case 0 '--
'do nothing
Exit Sub

Case 1 'Giant Print
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\bookgp.dot"
End With
MsgBox ("Giant Print template attached!")
Exit Sub

Case 2 'bookx
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\bookx.dotm"
End With
MsgBox ("bookx template attached!")
Exit Sub

Case 3 'magx
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\magx.dotm"
End With
MsgBox ("magx template attached!")
Exit Sub

Case 4 'chess
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\chess.dotm"
End With
MsgBox ("chess template attached!")
Exit Sub

End Select

End Sub



Can anyone tell me why the first attempt to change template fails and how I can fix this?

Many thanks, Helen.

gmaxey
11-07-2016, 06:23 AM
What happens if you change to:

Sub AttachTemplate(selectedIndex As Integer)

and call with

Sub Testing
AttachTemplate 1
End Sub

gmayor
11-07-2016, 06:55 AM
I guess I should ask why you are adding templates to documents instead of creating new documents from the templates? It seems you are putting the cart before the horse.

HelenT
11-08-2016, 03:39 AM
Hi Greg,

The same behaviour occurs, I'm afraid :-(

Thanks, Helen.

HelenT
11-08-2016, 03:53 AM
Hi Graham,

I work in a braille and giant print production environment where Word documents are automatically generated from a scan and OCR package. These Word documents are then processed by a user. Different custom styles and and macros are applied to these files depending on what "type" of documents they are (general magazine, general book, giant print book, chess magazine). The scan and OCR package does not have enough intelligence to know what type of file it is dealing with so just creates a generic file based on the normal template. Therefore it is up to the user to attach the relevant template (and then style up the file). The file is then either exported to a PDF (giant print files) or converted to XML. This XML file is then used by an in-house tool to create braille documents or synthetic audio.

As you can see from the above, with this particular workflow, it is not possible to create the document from its' relevant template.

Thanks, Helen.

gmaxey
11-08-2016, 05:15 AM
While I don't have your specific templates, I am unable to duplicate or create your error. You might try an error handler:


Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Test()
AttachTemplate 1
End Sub
Sub AttachTemplate(selectedIndex As Integer)
Dim DummyVar As Integer
On Error GoTo Err_Handler
Select Case selectedIndex

Case 0 '--
'do nothing
Exit Sub
Case 1 'Giant Print
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "D:\sgmlutil\template\word\bookgp.dot"
End With
MsgBox ("Giant Print template attached!")
Exit Sub
Case 2 'bookx
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\bookx.dotm"
End With
MsgBox ("bookx template attached!")
Exit Sub
Case 3 'magx
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\magx.dotm"
End With
MsgBox ("magx template attached!")
Exit Sub
Case 4 'chess
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\chess.dotm"
End With
MsgBox ("chess template attached!")
Exit Sub
End Select
lbl_Exit:
Exit Sub
Err_Handler:
Doze 1000
Resume
End Sub
Sub Doze(ByVal lngPeriod As Long)
DoEvents
Sleep lngPeriod
' Call it in desired location to sleep for 10 seconds like this:
' Doze 10000
End Sub

HelenT
11-14-2016, 04:47 AM
Hi Greg,

Thanks, that's a neat workaround. Still haven't solved the original problem, but at least I can now get around it without causing concern to the user!

Cheers, Helen.


While I don't have your specific templates, I am unable to duplicate or create your error. You might try an error handler:


Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Test()
AttachTemplate 1
End Sub
Sub AttachTemplate(selectedIndex As Integer)
Dim DummyVar As Integer
On Error GoTo Err_Handler
Select Case selectedIndex

Case 0 '--
'do nothing
Exit Sub
Case 1 'Giant Print
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "D:\sgmlutil\template\word\bookgp.dot"
End With
MsgBox ("Giant Print template attached!")
Exit Sub
Case 2 'bookx
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\bookx.dotm"
End With
MsgBox ("bookx template attached!")
Exit Sub
Case 3 'magx
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\magx.dotm"
End With
MsgBox ("magx template attached!")
Exit Sub
Case 4 'chess
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "C:\sgmlutil\template\word\chess.dotm"
End With
MsgBox ("chess template attached!")
Exit Sub
End Select
lbl_Exit:
Exit Sub
Err_Handler:
Doze 1000
Resume
End Sub
Sub Doze(ByVal lngPeriod As Long)
DoEvents
Sleep lngPeriod
' Call it in desired location to sleep for 10 seconds like this:
' Doze 10000
End Sub