Word Project Fails -- No MS Explanation

R

Ridge Kennedy

Dear All,

I have a template project that was under development and was wrking
reasonabley well, that went on hiatus for a couple of months. It is in
Office 2003, running on XP workstations.

I returned to work on it and now, when I run code for the form that takes
all the information from an office form and puts it into the document
created by my template, I get an MS Office error "Microsoft Office Word has
encountered a problem and needs to close. We are sorry for the
inconvenience."

It offers to do its best to save my work and asks if it's OK to send a
report back to the mother ship.

*However* -- when I step through the code in the VBA editor -- no error --
it still works.

Arrrgh.

Can anyone offer words of advice on what to do or where to look or whatever.
The MS error message offers no clue and the "attached" data references
modules and .dlls and contains lots of cryptic data -- but nothing to give
me any idea what's wrong.

Sincerely,

Ridge (in New Joisey)
 
D

Doug Robbins - Word MVP

If you do not post the code, there is no chance of any assistance. The only
crystal balls around here are the ones on the Christmas Trees.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Ridge Kennedy

Doug said:
If you do not post the code, there is no chance of any assistance. The
only crystal balls around here are the ones on the Christmas Trees.


Point well taken. I was thinking there might be something that MS has done
re an update or something, but . . .

Problem is that the code produces a MS error and closes the application with
no indication of why it is doing this.
Note also that I am able to step through this in the VBA environment and it
works OK. The procedure calls another procedure and I have included that
after the main chunk of code.

Here is the code that is initiated when the user clicks OK:

Public Sub OnFormattingGuideOK()
' code if user clicks OK button on formatting guide form

Dim oDoc As Document
' Dim strDocVarName As String
' Dim strDocVarText As String
Dim MyRange As Range
Dim strDate As String
Dim strTitle As String
Dim strName As String
Dim strCompany As String
Dim strAddress As String
Dim strSalutation As String
Dim strLetterText As String
Dim strClosing As String
Dim strAuthor As String
Dim strAuthorTitle As String
Dim strCopies As String
Dim strAssistant As String
Dim strAttachment As String
Dim oFrm As UserForm
Set oFrm = frmLthdFormattingGuide
Dim strAuthorInitials As String

' Declare listbox variables
Dim varItem As Variant
Dim intIndex As Integer
Dim intRow As Integer
Dim intRows As Integer
Dim intColumn As Integer
Dim intColumns As Integer
Dim lst As MSForms.ListBox
Set lst = oFrm.lstbxAuthors

' keep screen from flashing
Application.ScreenUpdating = False

'set initial letter text
strLetterText = "Letter text goes here . . . "

' set intRows variable
Set lst = oFrm.lstbxAuthors
intRows = lst.ListCount - 1

' go through the rows of the names in the listbox on the form
' find the one that is selected and set data equal to variables

For varItem = 0 To intRows
If lst.Selected(varItem) = True Then
strAuthor = (lst.Column(0, varItem))
strAuthorTitle = (lst.Column(1, varItem))
strAuthorInitials = (lst.Column(2, varItem))
End If

Next varItem

' now go through peole who are frequent cc's and
' concatenate data into a variable
strCopies = ""
Set lst = oFrm.lstbxCopies
intRows = lst.ListCount - 1

For varItem = 0 To intRows

If lst.Selected(varItem) = True Then
strCopies = strCopies & (lst.Column(0, varItem)) & ", " _
& (lst.Column(1, varItem)) & Chr$(11)

End If

Next varItem

Set oDoc = ActiveDocument

' strDate = frmLthdFormattingGuide.txtbxDate

strDate = IIf(frmLthdFormattingGuide.ckbxDate = -1, _
(Format((Date), "mmmm d, yyyy")), "Insert Date Here")
Call SaveDocVar("NJDate", (strDate))

strAddress = oFrm.txtbxAddress
Call SaveDocVar("NJAddress", (strAddress))

strName = oFrm.txtbxName
Call SaveDocVar("NJName", (strName))

strTitle = oFrm.txtbxTitle
Call SaveDocVar("NJTitle", (strTitle))

strCompany = oFrm.txtbxCompany
Call SaveDocVar("NJCompany", (strCompany))

strSalutation = oFrm.txtbxSalutation
Call SaveDocVar("NJSalutation", (strSalutation))

strClosing = oFrm.txtbxClosing
Call SaveDocVar("NJClosing", (strClosing))

Call SaveDocVar("NJAuthor", (strAuthor))

Call SaveDocVar("NJAuthorTitle", (strAuthorTitle))

Call SaveDocVar("NJAuthorInitials", (strAuthorInitials))

Call SaveDocVar("NJCopies", (strCopies))

strAssistant = oFrm.txtbxAssistant
Call SaveDocVar("NJAssistant", (strAssistant))

strAttachment = oFrm.txtbxAttachment
Call SaveDocVar("njattachment", (strAttachment))




' frmLthdFormattingGuide.Hide
' Unload frmLthdFormattingGuide
'
' Selection.InsertAfter
(Documents(strOrigDocName).Variables("Headline").Value)

Set MyRange = oDoc.Range
MyRange.Collapse wdCollapseStart

If strDate <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJDate").Value) & vbCrLf
MyRange.Style = ("NJ LT Date")
MyRange.Collapse (wdCollapseEnd)
End If

If strName <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJName").Value) & Chr$(11)
MyRange.Style = ("NJ LT Address")
MyRange.Collapse (wdCollapseEnd)
End If

If strTitle <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJTitle").Value) & Chr$(11)
MyRange.Style = ("NJ LT Address")
MyRange.Collapse (wdCollapseEnd)
End If

If strCompany <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJCompany").Value) & Chr$(11)
MyRange.Style = ("NJ LT Address")
MyRange.Collapse (wdCollapseEnd)
End If

If strAddress <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJAddress").Value) & vbCrLf
MyRange.Style = ("NJ LT Address")
MyRange.End = MyRange.End - 1
With MyRange.Find
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
MyRange.Find.Execute Replace:=wdReplaceAll

MyRange.End = MyRange.End + 2
MyRange.Collapse (wdCollapseEnd)

End If

If strSalutation <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJSalutation").Value) & vbCrLf
MyRange.Style = ("NJ LT Salutation")
MyRange.Collapse (wdCollapseEnd)
End If

MyRange.InsertAfter (strLetterText) & vbCrLf
MyRange.Style = ("NJ LT Text")
MyRange.Collapse (wdCollapseEnd)

If strClosing <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJClosing").Value) & vbCrLf
MyRange.Style = ("NJ LT Closing")
MyRange.Collapse (wdCollapseEnd)
End If

If strAuthor <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJAuthor").Value) & vbCrLf
MyRange.Style = ("NJ LT Author")
MyRange.Collapse (wdCollapseEnd)
End If

If strAuthorTitle <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJAuthorTitle").Value) & vbCrLf
MyRange.Style = ("NJ LT title")
MyRange.Collapse (wdCollapseEnd)
End If

If strCopies <> "" Then
MyRange.InsertAfter "CC:" & Chr$(9) &
(oDoc.Variables("NJCopies").Value) & vbCrLf
MyRange.Style = ("NJ LT Copies")
MyRange.Collapse (wdCollapseEnd)
End If


If strAssistant <> "" Then
MyRange.InsertAfter (oDoc.Variables("NJAuthorInitials").Value) & _
":" & (oDoc.Variables("NJassistant").Value) & vbCrLf
MyRange.Style = ("NJ LT Assistant")
MyRange.Collapse (wdCollapseEnd)
End If

If strAttachment <> "" Then

MyRange.InsertAfter "Enc.:" & (oDoc.Variables("NJAttachment").Value)
MyRange.Style = ("NJ LT Attachment")
MyRange.Collapse (wdCollapseEnd)
End If

Call ChangeLTHDFont


frmLthdFormattingGuide.hide
Unload frmLthdFormattingGuide
Unload frmOLContacts

Set MyRange = ActiveDocument.Range
MyRange.Collapse (wdCollapseStart)

Call DoFindReplace("^l^p", "^p")


Set MyRange = ActiveDocument.Range
MyRange.Collapse (wdCollapseStart)
With MyRange.Find
.ClearFormatting
.Text = (strLetterText)
.Forward = True
.MatchWholeWord = True
End With
MyRange.Find.Execute
MyRange.Select

Application.ScreenUpdating = True


End Sub

SaveDocVar function

Public Sub SaveDocVar(strDocVarName As String, strDocVarText As String)
'procedure to copy text from form to document variables

Dim oDoc As Object

' Dim strFileName As String
' Dim strNewFileName As String
' Dim MyRange As Range
Dim oVar As Object
Dim lNum As Long


Set oDoc = ActiveDocument

' check to see if the variable name exists. If not, create it
' otherwise, change the value of the existing variable to the one you
have chosen

For Each oVar In oDoc.Variables
If oVar.Name = (strDocVarName) Then lNum = oVar.Index
Next oVar

If lNum = 0 Then

oDoc.Variables.Add Name:=(strDocVarName), Value:=(strDocVarText)
Else
oDoc.Variables(strDocVarName).Value = (strDocVarText)

End If

End Sub
 
R

Ridge Kennedy

Ooops. OK. I found the problem.

I had another function called doFindReplace in my AddIn template.

I had moved it to a different module *after* I had reached the stopping
point on this letterhead template. I was making an effort to keep all the
highly reusable pieces in one place.

I changed the line in the template that was crashing Word from:

call doFindReplace . . .

to

call NJAddIn.WordTools.DoFindReplace . . .

And it ran from the form as expected.

Thanks for your attention. Sorry for the inconvenience.

Curious though, that I was able to step through it, and that VBA didn't give
me an error -- instead of the MS "closing the application" error.

Sincerely,

R.
 

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