MS Word的语法检查程序启动,VBA删除MS Access表单中的所有换行符

时间:2014-06-17 16:40:02

标签: vba ms-access spell-checking

我在VBA中有这个代码,在退出ms Access窗体的文本框后启动MS Word拼写和语法检查。 运行检查后,文本发送回表单,我的文本的所有换行符都消失了。我的数据看起来像一个段落,而不是很好的格式。

这种情况发生在Access端,因为我在拼写检查结束时看到的.doc仍然有换行符。

非常感谢你的帮助!

Option Compare Database

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
   wdApp.Visible = True
   wdApp.Activate


      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
            ctrl = 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
    ctrl.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)

看起来MS Word使用的是与Access文本框控件不同的换行符。您可以通过替换此行将字符恢复为拼写检查后在Access中使用的原始字符:

ctrl = wdApp.Selection.Text

使用:

ctrl = Replace(wdApp.Selection.Text, vbCr, vbCrLf)