B
Bruce McCormick
I hate to post this again. Malcolm Smith graciously attempted to provide
some help, but my code is still blowing up.
I am running the code below in Word XP. It moves through a couple of the
document templates, successfully updates them, then, after a couple of
seconds, crashes. I have tried adding and removing various templates from
the folder, always with the same result.
Why would this get through some templates and then crash?
I am cleaning out my temp files after each crash. Could the templates be
corrupt? What am I doing wrong?
I'd be grateful for any help.
'===========================================================================
========
' Procedure : UpdateUserInfo
' Date : 1/6/2004 06:02
' Purpose : Allows the user to update their document templates in one
pass.
' Shortcut : None
' Modified : 1/10/2004 15:08
'===========================================================================
========
Public Sub UpdateUserInfo()
' Declare the variables:
Dim MyUserForm As frmUserInformationForm
Dim MyAlertForm As frmBePatient
Dim wdDot As Document
Dim sUserName As String
Dim sUserJobTitle As String
Dim sUserDirectPhone As String
Dim sUserFaxPhone As String
Dim sUserEmailAddress As String
Dim sUserDivision As String
Dim sTemplatesPath As String
Dim x As Integer
'Collect the user's information via a custom form:
Set MyUserForm = New frmUserInformationForm
MyUserForm.Tag = "Cancel"
'Populate the fields with the current information"
On Error Resume Next
MyUserForm.txtUserName = Application.UserName
MyUserForm.txtUserJobTitle = _
ActiveDocument.CustomDocumentProperties("UserJobTitle")
MyUserForm.txtUserDirectPhone = _
ActiveDocument.CustomDocumentProperties("UserDirectPhone")
MyUserForm.txtUserFaxPhone = _
ActiveDocument.CustomDocumentProperties("UserFaxPhone")
MyUserForm.txtUserEmailAddress = _
ActiveDocument.CustomDocumentProperties("UserEmailAddress")
MyUserForm.cboDivision = _
ActiveDocument.CustomDocumentProperties("UserDivision")
'Show the form:
MyUserForm.Show vbModal
'Save the user information to variables:
If MyUserForm.Tag = "OK" Then 'User pressed OK
sUserName = MyUserForm.txtUserName
sUserJobTitle = MyUserForm.txtUserJobTitle
sUserDirectPhone = MyUserForm.txtUserDirectPhone
sUserFaxPhone = MyUserForm.txtUserFaxPhone
sUserEmailAddress = MyUserForm.txtUserEmailAddress
sUserDivision = MyUserForm.cboDivision.Value
Else
GoTo Shutdown 'User pressed Cancel
End If
'Display a message to the user to be patient:
'Application.ScreenRefresh
Set MyAlertForm = New frmBePatient
MyAlertForm.Show vbModeless
MyAlertForm.Repaint
'Insert the variables into each template and update
sTemplatesPath = Options.DefaultFilePath(wdUserTemplatesPath) + "\" + _
sUserDivision + "\"
'Set the user's name in the Tools | Options | User Information dialog
Application.UserName = sUserName
' Save the variables to the DDC template:
SetCustomProperty "UserJobTitle", sUserJobTitle
SetCustomProperty "UserDirectPhone", sUserDirectPhone
SetCustomProperty "UserFaxPhone", sUserFaxPhone
SetCustomProperty "UserEmailAddress", sUserEmailAddress
SetCustomProperty "UserDivision", sUserDivision
ActiveDocument.Save
'Set the other variables as custom document properties:
'Display a message to the user to be patient:
With Application.FileSearch
.NewSearch
.FileName = "*.dot"
.LookIn = sTemplatesPath
.Execute
For x = 1 To .FoundFiles.Count
Set wdDot = Documents.Open(.FoundFiles(x))
MyAlertForm.Caption = "Updating files ..."
MyAlertForm.lblMessage = "Please wait while we update the " + _
wdDot.Name + " files with your user information."
MyAlertForm.Repaint
'Add User Job Title:
SetCustomProperty "UserJobTitle", sUserJobTitle
'Add User Direct Phone Number:
SetCustomProperty "UserDirectPhone", sUserDirectPhone
'Add User Fax Phone Number:
SetCustomProperty "UserFaxPhone", sUserFaxPhone
'Add User E-mail Address:
SetCustomProperty "UserEmailAddress", sUserEmailAddress
'Add User Division:
SetCustomProperty "UserDivision", sUserDivision
'Update the document with the new values:
Selection.WholeStory
Selection.Fields.Update
Selection.HomeKey Unit:=wdStory
'Save the document:
ActiveDocument.Close SaveChanges:=wdSaveChanges
Next
End With
Shutdown:
' Release the variables:
Unload MyUserForm
Unload MyAlertForm
Set MyUserForm = Nothing
Set MyAlertForm = Nothing
Set wdDot = Nothing
sUserName = vbNull
sUserJobTitle = vbNull
sUserDirectPhone = vbNull
sUserFaxPhone = vbNull
sUserEmailAddress = vbNull
sUserDivision = vbNull
sTemplatesPath = vbNull
x = vbNull
Application.ScreenUpdating = True
End Sub
'===========================================================================
========
' Procedure : SetCustomProperty
' Date : 1/6/2004 06:38
' Purpose : Checks to see if the CustomDocumentProperty exists in the
Active
' : Document. If yes, and the value is unchanged, it exists the
routine
' : without doing anything. If the value has changed, it updates
the
' : the property to the new value. If the property didn't
previously
' : exits, it creates a property with the new value.
' Shortcut : None
' Modified : 1/10/2004 15:45
'===========================================================================
========
Private Sub SetCustomProperty(sPropertyName As String, sPropertyValue As
String)
' Declare the variables:
Dim oProperty As DocumentProperty
Dim bPropertyExists As Boolean
' See if the CustomDocument Property Exists:
For Each oProperty In ActiveDocument.CustomDocumentProperties
If oProperty.Name = sPropertyName Then
' If yes, set a flag and exit the loop:
' Check to see if the value changed:
If oProperty.Value <> sPropertyValue Then
' If true, delete the value and set the flag to false:
oProperty.Delete
bPropertyExists = False
Else
' Otherwise, set the flag to true
bPropertyExists = True
End If
Exit For
End If
' If no, loop back through:
Next oProperty
' If the CustomDocument Property Does not Exist, add it:
If bPropertyExists = False Then
ActiveDocument.CustomDocumentProperties.Add _
Name:=sPropertyName, _
Type:=msoPropertyTypeString, _
Value:=sPropertyValue, _
LinkToContent:=False
End If
' Release the variables:
bPropertyExists = False
Set oProperty = Nothing
End Sub
some help, but my code is still blowing up.
I am running the code below in Word XP. It moves through a couple of the
document templates, successfully updates them, then, after a couple of
seconds, crashes. I have tried adding and removing various templates from
the folder, always with the same result.
Why would this get through some templates and then crash?
I am cleaning out my temp files after each crash. Could the templates be
corrupt? What am I doing wrong?
I'd be grateful for any help.
'===========================================================================
========
' Procedure : UpdateUserInfo
' Date : 1/6/2004 06:02
' Purpose : Allows the user to update their document templates in one
pass.
' Shortcut : None
' Modified : 1/10/2004 15:08
'===========================================================================
========
Public Sub UpdateUserInfo()
' Declare the variables:
Dim MyUserForm As frmUserInformationForm
Dim MyAlertForm As frmBePatient
Dim wdDot As Document
Dim sUserName As String
Dim sUserJobTitle As String
Dim sUserDirectPhone As String
Dim sUserFaxPhone As String
Dim sUserEmailAddress As String
Dim sUserDivision As String
Dim sTemplatesPath As String
Dim x As Integer
'Collect the user's information via a custom form:
Set MyUserForm = New frmUserInformationForm
MyUserForm.Tag = "Cancel"
'Populate the fields with the current information"
On Error Resume Next
MyUserForm.txtUserName = Application.UserName
MyUserForm.txtUserJobTitle = _
ActiveDocument.CustomDocumentProperties("UserJobTitle")
MyUserForm.txtUserDirectPhone = _
ActiveDocument.CustomDocumentProperties("UserDirectPhone")
MyUserForm.txtUserFaxPhone = _
ActiveDocument.CustomDocumentProperties("UserFaxPhone")
MyUserForm.txtUserEmailAddress = _
ActiveDocument.CustomDocumentProperties("UserEmailAddress")
MyUserForm.cboDivision = _
ActiveDocument.CustomDocumentProperties("UserDivision")
'Show the form:
MyUserForm.Show vbModal
'Save the user information to variables:
If MyUserForm.Tag = "OK" Then 'User pressed OK
sUserName = MyUserForm.txtUserName
sUserJobTitle = MyUserForm.txtUserJobTitle
sUserDirectPhone = MyUserForm.txtUserDirectPhone
sUserFaxPhone = MyUserForm.txtUserFaxPhone
sUserEmailAddress = MyUserForm.txtUserEmailAddress
sUserDivision = MyUserForm.cboDivision.Value
Else
GoTo Shutdown 'User pressed Cancel
End If
'Display a message to the user to be patient:
'Application.ScreenRefresh
Set MyAlertForm = New frmBePatient
MyAlertForm.Show vbModeless
MyAlertForm.Repaint
'Insert the variables into each template and update
sTemplatesPath = Options.DefaultFilePath(wdUserTemplatesPath) + "\" + _
sUserDivision + "\"
'Set the user's name in the Tools | Options | User Information dialog
Application.UserName = sUserName
' Save the variables to the DDC template:
SetCustomProperty "UserJobTitle", sUserJobTitle
SetCustomProperty "UserDirectPhone", sUserDirectPhone
SetCustomProperty "UserFaxPhone", sUserFaxPhone
SetCustomProperty "UserEmailAddress", sUserEmailAddress
SetCustomProperty "UserDivision", sUserDivision
ActiveDocument.Save
'Set the other variables as custom document properties:
'Display a message to the user to be patient:
With Application.FileSearch
.NewSearch
.FileName = "*.dot"
.LookIn = sTemplatesPath
.Execute
For x = 1 To .FoundFiles.Count
Set wdDot = Documents.Open(.FoundFiles(x))
MyAlertForm.Caption = "Updating files ..."
MyAlertForm.lblMessage = "Please wait while we update the " + _
wdDot.Name + " files with your user information."
MyAlertForm.Repaint
'Add User Job Title:
SetCustomProperty "UserJobTitle", sUserJobTitle
'Add User Direct Phone Number:
SetCustomProperty "UserDirectPhone", sUserDirectPhone
'Add User Fax Phone Number:
SetCustomProperty "UserFaxPhone", sUserFaxPhone
'Add User E-mail Address:
SetCustomProperty "UserEmailAddress", sUserEmailAddress
'Add User Division:
SetCustomProperty "UserDivision", sUserDivision
'Update the document with the new values:
Selection.WholeStory
Selection.Fields.Update
Selection.HomeKey Unit:=wdStory
'Save the document:
ActiveDocument.Close SaveChanges:=wdSaveChanges
Next
End With
Shutdown:
' Release the variables:
Unload MyUserForm
Unload MyAlertForm
Set MyUserForm = Nothing
Set MyAlertForm = Nothing
Set wdDot = Nothing
sUserName = vbNull
sUserJobTitle = vbNull
sUserDirectPhone = vbNull
sUserFaxPhone = vbNull
sUserEmailAddress = vbNull
sUserDivision = vbNull
sTemplatesPath = vbNull
x = vbNull
Application.ScreenUpdating = True
End Sub
'===========================================================================
========
' Procedure : SetCustomProperty
' Date : 1/6/2004 06:38
' Purpose : Checks to see if the CustomDocumentProperty exists in the
Active
' : Document. If yes, and the value is unchanged, it exists the
routine
' : without doing anything. If the value has changed, it updates
the
' : the property to the new value. If the property didn't
previously
' : exits, it creates a property with the new value.
' Shortcut : None
' Modified : 1/10/2004 15:45
'===========================================================================
========
Private Sub SetCustomProperty(sPropertyName As String, sPropertyValue As
String)
' Declare the variables:
Dim oProperty As DocumentProperty
Dim bPropertyExists As Boolean
' See if the CustomDocument Property Exists:
For Each oProperty In ActiveDocument.CustomDocumentProperties
If oProperty.Name = sPropertyName Then
' If yes, set a flag and exit the loop:
' Check to see if the value changed:
If oProperty.Value <> sPropertyValue Then
' If true, delete the value and set the flag to false:
oProperty.Delete
bPropertyExists = False
Else
' Otherwise, set the flag to true
bPropertyExists = True
End If
Exit For
End If
' If no, loop back through:
Next oProperty
' If the CustomDocument Property Does not Exist, add it:
If bPropertyExists = False Then
ActiveDocument.CustomDocumentProperties.Add _
Name:=sPropertyName, _
Type:=msoPropertyTypeString, _
Value:=sPropertyValue, _
LinkToContent:=False
End If
' Release the variables:
bPropertyExists = False
Set oProperty = Nothing
End Sub