Word 2010 VBA:奇怪的随机间歇性,错误。 4248此命令不可用,因为没有文档打开

时间:2017-01-30 19:13:36

标签: vba word-2010

一些背景......我们有一个用于文档管理的胖客户端。我们可以通过编写C / C ++ dll来自定义客户端的功能。这段代码是十多年前为Win XP和Word 2003编写的。我们在被迫迁移到Win 7和Word 2010后才出现奇怪的问题。这些dll中的一个函数创建了一个新的线程并在其中启动Word新线程。我们还传递Word一些命令行参数,这些参数是要使用的单词模板的名称(master.dotm),以及模板中要在Word启动时执行(autonew)的函数。

Autonew基于master.dotm模板添加一个新文档,然后关闭模板并调用另一个名为InsertLetter的函数。在这个函数中,我们插入另一个Word文档,作为Master.dotm的字母的实际内容实际上没有除页眉和页脚之外的任何内容。 它在InsertLetter中,用户偶尔会收到消息框“4248此命令不可用,因为没有文档打开”。我不知道这个函数在这个错误发生的确切位置,但我认为实际问题是在创建文档时的AutoNew函数内,并且由于某些随机,不可重现的原因弹出此错误。我无法在我的电脑上重现这一点。单词的版本显然是相同的,PC应该是相同的。我能想到的唯一想法是,master.dotm创建新文档然后关闭并调用InsertLetter函数之间的时间问题。

这是autonew功能:

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

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

0 个答案:

没有答案