MS Access 2013:通过VBA使用MS Word的语法检查

时间:2014-06-16 07:26:44

标签: vba ms-access spell-checking

(问题下面的解决方案)

我有一个访问表单,允许用户在文本框(MYFIELD)中输入文本。我已经在用户退出字段时自动设置了拼写检查选项

Private Sub MYFIELD_Exit(Cancel As Integer)
Dim strSpell
    strSpell = MYFIELD
    If IsNull(Len(strSpell)) Or Len(strSpell) = 0 Then
       Exit Sub
    End If
    With MYFIELD
        .SetFocus
        .SelStart = 0
        .SelLength = Len(strSpell)
    End With
    DoCmd.SetWarnings False
    DoCmd.RunCommand acCmdSpelling
    DoCmd.SetWarnings True

End Sub

我想启用语法检查。由于它不是在Access中构建的,我需要使用单词,但我无法弄清楚如何做到这一点。

我在forum

上找到了此代码

1)发布此内容的人说他的用户"将文本输入大型控件。然后将该控件合并到word文档"中。我的字段只是一个很长的文本字段,所以我仍然可以使用(或改编)下面的代码吗?

2)如果是,我不知道如何将它与我的代码混合(尽管我所有的搜索 - 我不熟悉VBA-,我不明白如何从我的表单中调用此公共函数):

Public Function SpellIt(ctl As Control)
   Dim wdApp As Word.Application
   Dim wdDoc As Word.Document


    On Error GoTo SpellIt_Err

   Set wdApp = New Word.Application

      If Not IsNull(ctl) Then
         Set wdDoc = wdApp.Documents.Add
         wdApp.Selection.Text = ctl
         wdApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show
         If Len(wdApp.Selection.Text) <> 1 Then
            ctl = wdApp.Selection.Text
         Else
            wdDoc.Close wdDoNotSaveChanges
            wdApp.Quit
            Set wdApp = Nothing
            Exit Function
         End If
         wdDoc.Close wdDoNotSaveChanges
      End If

   wdApp.Quit
   Set wdApp = Nothing

   MsgBox "Spelling and Grammar Check Complete.", vbInformation, "Microsoft Word Spelling And Grammar:"
   Exit Function

SpellIt_Err:
    Err.Clear
    ctl.Undo
    MsgBox "We encountered an error in it's conversation with Microsoft Word regarding your comment." & vbCrLf & _
        "As a precaution, any changes made within the grammar and spelling dialog box have not been retained.", _
        vbCritical, "Spelling and Grammar Check NOT Complete:"
End Function

非常感谢你的帮助!

这是解决方案:(我需要将ctl更改为ctrl)

Private Sub Description_Exit(Cancel As Integer)
Call SpellIt(Description)
End Sub
Public Function SpellIt(ctrl As Control)
   Dim wdApp As Word.Application
   Dim wdDoc As Word.Document


    On Error GoTo SpellIt_Err

   Set wdApp = New Word.Application

      If Not IsNull(ctrl) Then
         Set wdDoc = wdApp.Documents.Add
         wdApp.Selection.Text = ctrl
         wdApp.Dialogs(wdDialogToolsSpellingAndGrammar).Show
         If Len(wdApp.Selection.Text) <> 1 Then
            ctl = wdApp.Selection.Text
         Else
            wdDoc.Close wdDoNotSaveChanges
            wdApp.Quit
            Set wdApp = Nothing
            Exit Function
         End If
         wdDoc.Close wdDoNotSaveChanges
      End If

   wdApp.Quit
   Set wdApp = Nothing

   MsgBox "Spelling and Grammar Check Complete.", vbInformation, "Microsoft Word Spelling And Grammar:"
   Exit Function

SpellIt_Err:
    Err.Clear
    ctl.Undo
    MsgBox "We encountered an error in it's conversation with Microsoft Word regarding your comment." & vbCrLf & _
        "As a precaution, any changes made within the grammar and spelling dialog box have not been retained.", _
        vbCritical, "Spelling and Grammar Check NOT Complete:"
End Function

1 个答案:

答案 0 :(得分:1)

好吧,你似乎对功能实际上是什么感到困惑。函数的格式为Function(Arguments),并在Sub中调用。它不是独立运行的,不应该这样对待。已有的一个示例是MsgBox()。您调用MsgBox函数,该函数接受一个参数(或本例中的参数)并将其放入函数中,将其转换为您需要的函数。

首先要做的事情。您拥有的文本框称为MYFIELD。那是你的控制。最好不要让函数具有相同的名称。将其重命名为Public Function ButChng(ctrl as Control)。函数本身不需要触及它原来的样式。

MYFIELD_Exit子目录中,您需要做的就是调用该函数。所以它会这样读:

Private Sub MYFIELD_Exit(Cancel As Integer)
Call ButChng(MYFIELD) 'this puts the MYFIELD control as your variable to go through ButChng
Exit Sub

然后应按预期工作。