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