Cheekie B
01-31-2017, 12:34 PM
Some background...We have a thick client we use for document management. We are able to customize the functionality of the client by writting C/C++ dll's. This code was written over a decade ago for Win XP and Word 2003. We have only recently seen odd problems after being forced to move to Win 7 and Word 2010.
One of the functions in these dll's creates a new thread and launches Word within that new thread. We also pass Word some command line parameters, that being the name of the word template to use (master.dotm) and a function within the template to execute (autonew) when Word starts.
Autonew adds a new document based on the master.dotm template then it closes the template and calls another function called InsertLetter.
Within this function we insert another Word document, the actual content for the letter as Master.dotm doesnt really have any content other than a header and footer. Its within InsertLetter that users very occasionally get the message box "4248 this command is not available because no document is open".
I do not know exactly where in this function this error occurs but I think the actual problem is within the AutoNew function when the document is being created and for some random, un-reproduceable reason this error pops up.
I have not been able to reproduce this on my PC. The version of word is apparently the same and the PC's should be built the same. The only think I can think of is that its a timing issue between master.dotm creating the new document and then getting closed and the InsertLetter function getting called.
Here is the autonew function:
Public Sub AutoNew()
On Error Resume Next
'Create autotextMenu
Call CreateAutoTextMenu
'Open a new document based on the Master.dotm template
If ActiveDocument.Type = wdTypeTemplate Then
Documents.Add ActiveDocument.Path & "\" & ActiveDocument.Name <---this value is always "Master.dotm"
Exit Sub
End If
Documents("master.dotm").Close
'Enable "Letters" toolbar
CommandBars("Letters").Enabled = True
DoEvents
Application.ScreenUpdating = False
System.Cursor = wdCursorWait
GetData 'Function that populates user data
HeaderFooter (gsLtrCode)
If goValues Is Nothing Then
Set goValues = New clsValues
End If
InsertLetter
Application.ScreenRefresh
'Display driver info
Call DriverInfo
System.Cursor = wdCursorNormal
Exit Sub
The code for InsertLetter:
Private Sub InsertLetter()
Dim sLtrName As String
Dim sTemp As String
Dim oRng As Range
Dim tmpStart
sLtrName = gsPath & "Letters\" & gsLtrCode & ".doc"
With ActiveDocument.Bookmarks
'Insert selected letter
If .Exists("Letter") = True Then
Selection.GoTo what:=wdGoToBookmark, Name:="Letter"
Selection.InsertFile FileName:=sLtrName, Range:="",
ConfirmConversions:=False, link:=False, attachment:=False
'Define gender
sTemp = Left$(sTemp, 2)
If .Exists("Sir_Madam") Then
If CInt(sTemp) > 50 Then
.Item("Sir_Madam").Range.Text = "Madam"
Else
.Item("Sir_Madam").Range.Text = "Sir"
End If
End If
If .Exists("FDate") Then
gbFDate = True
Set oRng = ActiveDocument.Bookmarks("FDate").Range
oRng.Text = Format(Date, "mmmm d, yyyy")
'default Today's Date
.Add Name:="FDate", Range:=oRng
End If
If .Exists("DDate") Then
gbDDate = True
Set oRng = ActiveDocument.Bookmarks("DDate").Range
oRng.Text = Format(DateAdd("ww", 8, Date), "mmmm d, yyyy")
'default +8 weeks
.Add Name:="DDate", Range:=oRng
End If
If .Exists("CaseID") Then
Set oRng = ActiveDocument.Bookmarks("CaseID").Range
oRng.Text = CaseID
.Add Name:="CaseID", Range:=oRng
End If
If .Exists("SeqNum") Then
Set oRng = ActiveDocument.Bookmarks("SeqNum").Range
oRng.Text = LetterSeqNum
.Add Name:="LetterSeqNum", Range:=oRng
End If
If .Exists("SeqNumP1") Then
Set oRng = ActiveDocument.Bookmarks("SeqNumP1").Range
oRng.Text = LetterSeqNum
.Add Name:="LetterSeqNum", Range:=oRng
End If
If .Exists("VJ") Then
.Item("VJ").Range.InsertAfter Trim(gsUserID)
'The next 2 lines added on Feb 23, 2004
ActiveDocument.Bookmarks("VJ").Range.Font.Name = "Arial"
ActiveDocument.Bookmarks("VJ").Range.Font.Size = 10
End If
If Left$(gsLtrCode, 5) = "USREC" Then
If .Exists("DatePlus6w") Then
Set oRng = ActiveDocument.Bookmarks("DatePlus6w").Range
oRng.Text = Format(DateAdd("ww", 6, Date), "mmmm d, yyyy")
'default + 6 weeks
sTemp = oRng
.Add Name:="DatePlus6w", Range:=oRng
End If
If .Exists("DatePlus6wF") Then
Select Case Month(CDate(sTemp))
Case 1
sTemp = Day(CDate(sTemp)) & " janvier, " & Year(CDate(sTemp))
Case 2
sTemp = Day(CDate(sTemp)) & " février, " & Year(CDate(sTemp))
Case 3
sTemp = Day(CDate(sTemp)) & " mars, " & Year(CDate(sTemp))
Case 4
sTemp = Day(CDate(sTemp)) & " avril, " & Year(CDate(sTemp))
Case 5
sTemp = Day(CDate(sTemp)) & " mai, " & Year(CDate(sTemp))
Case 6
sTemp = Day(CDate(sTemp)) & " juin, " & Year(CDate(sTemp))
Case 7
sTemp = Day(CDate(sTemp)) & " juillet, " & Year(CDate(sTemp))
Case 8
sTemp = Day(CDate(sTemp)) & " août, " & Year(CDate(sTemp))
Case 9
sTemp = Day(CDate(sTemp)) & " septembre, " & Year(CDate(sTemp))
Case 10
sTemp = Day(CDate(sTemp)) & " octobre, " & Year(CDate(sTemp))
Case 11
sTemp = Day(CDate(sTemp)) & " novembre, " & Year(CDate(sTemp))
Case 12
sTemp = Day(CDate(sTemp)) & " décembre, " & Year(CDate(sTemp))
End Select
Set oRng = ActiveDocument.Bookmarks("DatePlus6wF").Range
oRng.Text = sTemp
oRng.Font.Italic = True
.Add Name:="DatePlus6wF", Range:=oRng
End If
End If
Else
MsgBox "Letter doesn't exist"
End If
End With
PopulateFields
'Apr 15 begins
With ActiveDocument.Bookmarks
If .Exists("Encl") = True Then
Set oRng = ActiveDocument.Range( _
Start:=ActiveDocument.Bookmarks("Letter").Range.Start, _
End:=ActiveDocument.Bookmarks("Encl").Range.End)
Else
Set oRng = ActiveDocument.Range( _
Start:=ActiveDocument.Bookmarks("Letter").Range.Start, _
End:=ActiveDocument.Bookmarks("VJ").Range.End)
End If
End With
With oRng
.Font.Name = "Arial"
.Font.Size = 11
End With
Exit Sub
End Sub
One of the functions in these dll's creates a new thread and launches Word within that new thread. We also pass Word some command line parameters, that being the name of the word template to use (master.dotm) and a function within the template to execute (autonew) when Word starts.
Autonew adds a new document based on the master.dotm template then it closes the template and calls another function called InsertLetter.
Within this function we insert another Word document, the actual content for the letter as Master.dotm doesnt really have any content other than a header and footer. Its within InsertLetter that users very occasionally get the message box "4248 this command is not available because no document is open".
I do not know exactly where in this function this error occurs but I think the actual problem is within the AutoNew function when the document is being created and for some random, un-reproduceable reason this error pops up.
I have not been able to reproduce this on my PC. The version of word is apparently the same and the PC's should be built the same. The only think I can think of is that its a timing issue between master.dotm creating the new document and then getting closed and the InsertLetter function getting called.
Here is the autonew function:
Public Sub AutoNew()
On Error Resume Next
'Create autotextMenu
Call CreateAutoTextMenu
'Open a new document based on the Master.dotm template
If ActiveDocument.Type = wdTypeTemplate Then
Documents.Add ActiveDocument.Path & "\" & ActiveDocument.Name <---this value is always "Master.dotm"
Exit Sub
End If
Documents("master.dotm").Close
'Enable "Letters" toolbar
CommandBars("Letters").Enabled = True
DoEvents
Application.ScreenUpdating = False
System.Cursor = wdCursorWait
GetData 'Function that populates user data
HeaderFooter (gsLtrCode)
If goValues Is Nothing Then
Set goValues = New clsValues
End If
InsertLetter
Application.ScreenRefresh
'Display driver info
Call DriverInfo
System.Cursor = wdCursorNormal
Exit Sub
The code for InsertLetter:
Private Sub InsertLetter()
Dim sLtrName As String
Dim sTemp As String
Dim oRng As Range
Dim tmpStart
sLtrName = gsPath & "Letters\" & gsLtrCode & ".doc"
With ActiveDocument.Bookmarks
'Insert selected letter
If .Exists("Letter") = True Then
Selection.GoTo what:=wdGoToBookmark, Name:="Letter"
Selection.InsertFile FileName:=sLtrName, Range:="",
ConfirmConversions:=False, link:=False, attachment:=False
'Define gender
sTemp = Left$(sTemp, 2)
If .Exists("Sir_Madam") Then
If CInt(sTemp) > 50 Then
.Item("Sir_Madam").Range.Text = "Madam"
Else
.Item("Sir_Madam").Range.Text = "Sir"
End If
End If
If .Exists("FDate") Then
gbFDate = True
Set oRng = ActiveDocument.Bookmarks("FDate").Range
oRng.Text = Format(Date, "mmmm d, yyyy")
'default Today's Date
.Add Name:="FDate", Range:=oRng
End If
If .Exists("DDate") Then
gbDDate = True
Set oRng = ActiveDocument.Bookmarks("DDate").Range
oRng.Text = Format(DateAdd("ww", 8, Date), "mmmm d, yyyy")
'default +8 weeks
.Add Name:="DDate", Range:=oRng
End If
If .Exists("CaseID") Then
Set oRng = ActiveDocument.Bookmarks("CaseID").Range
oRng.Text = CaseID
.Add Name:="CaseID", Range:=oRng
End If
If .Exists("SeqNum") Then
Set oRng = ActiveDocument.Bookmarks("SeqNum").Range
oRng.Text = LetterSeqNum
.Add Name:="LetterSeqNum", Range:=oRng
End If
If .Exists("SeqNumP1") Then
Set oRng = ActiveDocument.Bookmarks("SeqNumP1").Range
oRng.Text = LetterSeqNum
.Add Name:="LetterSeqNum", Range:=oRng
End If
If .Exists("VJ") Then
.Item("VJ").Range.InsertAfter Trim(gsUserID)
'The next 2 lines added on Feb 23, 2004
ActiveDocument.Bookmarks("VJ").Range.Font.Name = "Arial"
ActiveDocument.Bookmarks("VJ").Range.Font.Size = 10
End If
If Left$(gsLtrCode, 5) = "USREC" Then
If .Exists("DatePlus6w") Then
Set oRng = ActiveDocument.Bookmarks("DatePlus6w").Range
oRng.Text = Format(DateAdd("ww", 6, Date), "mmmm d, yyyy")
'default + 6 weeks
sTemp = oRng
.Add Name:="DatePlus6w", Range:=oRng
End If
If .Exists("DatePlus6wF") Then
Select Case Month(CDate(sTemp))
Case 1
sTemp = Day(CDate(sTemp)) & " janvier, " & Year(CDate(sTemp))
Case 2
sTemp = Day(CDate(sTemp)) & " février, " & Year(CDate(sTemp))
Case 3
sTemp = Day(CDate(sTemp)) & " mars, " & Year(CDate(sTemp))
Case 4
sTemp = Day(CDate(sTemp)) & " avril, " & Year(CDate(sTemp))
Case 5
sTemp = Day(CDate(sTemp)) & " mai, " & Year(CDate(sTemp))
Case 6
sTemp = Day(CDate(sTemp)) & " juin, " & Year(CDate(sTemp))
Case 7
sTemp = Day(CDate(sTemp)) & " juillet, " & Year(CDate(sTemp))
Case 8
sTemp = Day(CDate(sTemp)) & " août, " & Year(CDate(sTemp))
Case 9
sTemp = Day(CDate(sTemp)) & " septembre, " & Year(CDate(sTemp))
Case 10
sTemp = Day(CDate(sTemp)) & " octobre, " & Year(CDate(sTemp))
Case 11
sTemp = Day(CDate(sTemp)) & " novembre, " & Year(CDate(sTemp))
Case 12
sTemp = Day(CDate(sTemp)) & " décembre, " & Year(CDate(sTemp))
End Select
Set oRng = ActiveDocument.Bookmarks("DatePlus6wF").Range
oRng.Text = sTemp
oRng.Font.Italic = True
.Add Name:="DatePlus6wF", Range:=oRng
End If
End If
Else
MsgBox "Letter doesn't exist"
End If
End With
PopulateFields
'Apr 15 begins
With ActiveDocument.Bookmarks
If .Exists("Encl") = True Then
Set oRng = ActiveDocument.Range( _
Start:=ActiveDocument.Bookmarks("Letter").Range.Start, _
End:=ActiveDocument.Bookmarks("Encl").Range.End)
Else
Set oRng = ActiveDocument.Range( _
Start:=ActiveDocument.Bookmarks("Letter").Range.Start, _
End:=ActiveDocument.Bookmarks("VJ").Range.End)
End If
End With
With oRng
.Font.Name = "Arial"
.Font.Size = 11
End With
Exit Sub
End Sub