What is going wrong here?

D

David

Hi and thanks,
This code works fine in Word97/Win98, and Word2000/XP, but when I try to run
it on Word2002/XP or Word2003/XP it crashes Word totally with a 'problem' in
MSO.dll. According to MSKB, it means corrupted user account, but creating
and running under a new user gives same results. Here's the code:

'1. Get the startup folder
Dim vStartupPath As String
With Dialogs(wdDialogToolsOptionsFileLocations)
.Path = "STARTUP-PATH"
.Update
vStartupPath = .Setting
If Not Right$(vStartupPath, 1) = "\" Then
vStartupPath = vStartupPath + "\"
End If
End With
'-------------------------------------------------
'2. Check if template is loaded and unload it if so.
If Len(Dir(vStartupPath & "Job Finisher.dot")) > 0 Then
AddIns(vStartupPath & "Job Finisher.dot").Installed = False
With ActiveDocument
.UpdateStylesOnOpen = False
.AttachedTemplate = "Normal.dot"
End With
End If
'-------------------------------------------------
'3. Check if file is in C:\Documed\blanks and Copy it to the startup folder
If Len(Dir("C:\DocuMed\Blanks\Job Finisher.dot")) > 0 Then
FileCopy "C:\DocuMed\Blanks\Job Finisher.dot", vStartupPath & "Job
Finisher.dot"
MsgBox "'Job Finisher.dot' has been copied to the Startup folder. Click
OK to continue."
Else
MsgBox "'Job Finisher.dot' is not in the C:\Documed\Blanks folder.
Please put it there and reclick the Setup Button."
Exit Sub
End If
'-------------------------------------------------
AddIns.Add FileName:=vStartupPath & "Job Finisher.dot", Install:=True
With ActiveDocument
.UpdateStylesOnOpen = False
.AttachedTemplate = "Normal.dot"
End With
'-------------------------------------------------
'5. Create Toolbar button
CustomizationContext = NormalTemplate
Set isThere = CommandBars("Menu Bar").FindControl _
(Type:=msoControlButton, Tag:="Job Finisher")
If isThere Is Nothing Then
With CommandBars("Menu Bar").Controls
Set myButton = .Add(Type:=msoControlButton)
With myButton
.Style = msoButtonCaption
.Caption = "&Job Finisher"
.OnAction = "JobFinisher"
.Tag = "Job Finisher"
End With
End With
MsgBox "'Job Finisher' button added to menu bar."
Else
MsgBox "Update complete."
Exit Sub
End If
MsgBox "Job Finisher shortcut key set to Alt+J."
End Sub
 
C

Charles Kenyon

Did you try stepping through it? F8 in the vba Editor.

Notes on your code. You should not have the customization context for your
changes as normal.dot. Make it your job finisher template or another
Add-In. If it is loaded as an Add-In the button, keyboard shortcut, and
macro will all be available. Once you have made a change through code, you
should save the template in your code or mark its saved state as True.
Otherwise your user is going to be reading a warning about changes being
made to some template and get annoyed. (If the changes are being made to
normal.dot, the user should be annoyed.)

Generally AddIns are distributed using the user network login script rather
than through vba.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

David

Thanks for valuable code comments.
Stepping resulted in crash at:
vStartupPath = .Setting
Does this help?
 
C

Charles Kenyon

I'm not sure what this does:

With Dialogs(wdDialogToolsOptionsFileLocations)
.Path = "STARTUP-PATH"
.Update
vStartupPath = .Setting

Are you trying to get the setting of the Startup Path or change it?
The following can be put into a string and will have the path.
Application.Options.DefaultFilePath(wdStartupPath)

What follows is a function that I wrote a while back. It may be a useful
starting point for you.

Function WorkGroupPath() As String
' Written by Charles Kenyon
' February 28, 2003
'
' Used by templates menus to set location of templates.
' Returns workgroup tempates path with "\" at the end.
'
' This is needed because if the folder is a network drive rather
' than a folder, it will have the "\" already. If it is a folder,
' it will not have the backslash. This function gives a string
' with the backslash in either case.
'
WorkGroupPath =
Application.Options.DefaultFilePath(wdWorkgroupTemplatesPath)
If Right(WorkGroupPath, 1) <> "\" Then
WorkGroupPath = WorkGroupPath & "\"
End If
End Function

Try using Application.Options.DefaultFilePath(wdStartupPath) instead.

Note, I am a novice programmer, a lawyer. Generally, though, I use the
dialogs to change settings if needed but not to get information. See the
links under "Working with built-in dialogs" at
http://word.mvps.org/FAQs/MacrosVBA/index.htm.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
D

David

To get the startup path, I was getting the setting from the dialog.
It worked in earlier versions, but not after Word 2000.
'Application.StarupPath' gets me the same info, and seems to work in all
versions.

Thank you!
 
C

Charles Kenyon

Glad I could help.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 

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