Coool
Feb 22nd, 2006, 02:02 AM
Hi i got the following code from VB.NET section.
It works fine in vb.net but i want it in C#. I have also tried using line
http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx
but all in vain,.. so plz
Public Class clsSpellMe
Friend moApp As New Word.Application
Private mbKillMe As Boolean
Friend Property KillMe() As Boolean
Get
InitializeMe()
KillMe = mbKillMe
End Get
Set(ByVal Value As Boolean)
mbKillMe = Value
End Set
End Property
Friend Sub InitializeMe()
Try
'<INITIALIZE WORD>
moApp = DirectCast(GetObject(, "Word.Application"), Word.Application)
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Word.Application"), Word.Application)
mbKillMe = True
Else
MessageBox.Show(ex.Message, "SpellChecker™.NET", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
End Sub
Friend Function SpellMe(ByVal msSpell As String) As String
Dim oDoc As Word.Document
Dim iWSE As Integer
Dim iWGE As Integer
Dim iResp As Integer
Dim sReplace As String
If msSpell = String.Empty Then
Else
Try
InitializeMe()
Select Case moApp.Version
Case "9.0", "10.0", "11.0"
oDoc = moApp.Documents.Add(, , 1, True)
Case "8.0"
oDoc = moApp.Documents.Add
Case Else
MessageBox.Show("Unsupported Version of Word.", "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Function
End Select
oDoc.Words.First.InsertBefore(msSpell)
iWSE = oDoc.SpellingErrors.Count
iWGE = oDoc.GrammaticalErrors.Count
'<CHECK SPELLING AND GRAMMER DIALOG BOX>
If iWSE > 0 Or iWGE > 0 Then
'<HIDE MAIN WORD WINDOW>
moApp.Visible = False
If (moApp.WindowState = Word.WdWindowState.wdWindowStateNormal) Or _
(moApp.WindowState = Word.WdWindowState.wdWindowStateMaximize) Then
moApp.WindowState = Word.WdWindowState.wdWindowStateMinimize
Else
moApp.WindowState = Word.WdWindowState.wdWindowStateMinimize
End If
moApp.Quit()
'</HIDE MAIN WORD WINDOW>
'<PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.CheckGrammarWit hSpelling = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.SuggestSpelling Corrections = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreUppercase = False
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreInternetA ndFileAddresses = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreMixedDigi ts = False
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.ShowReadability Statistics = False
'</PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
'<DO ACTUAL SPELL CHECKING>
moApp.Visible = True
moApp.Activate()
iResp = moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Display
'</DO ACTUAL SPELL CHECKING>
If iResp < 0 Then
moApp.Visible = True
MessageBox.Show("Corrections Being Updated!", "VB/Office Guru™ SpellChecker™", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
oDoc.Select()
oDoc.Range.Copy()
sReplace = DirectCast(Clipboard.GetDataObject.GetData("System.String", True), String)
'<FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
If (InStrRev(sReplace, Chr(13) & Chr(10))) = (Len(sReplace) - 1) Then
sReplace = Mid$(sReplace, 1, Len(sReplace) - 2)
End If
'</FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
SpellMe = sReplace
ElseIf iResp = 0 Then
'MessageBox.Show("Spelling Corrections Have Been Canceled!", "VB/Office Guru™ SpellChecker™.NET", _
'MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
SpellMe = msSpell
End If
Else
'MessageBox.Show("No Spelling Errors Found" & Environment.NewLine & "Or No Suggestions Available!", _
'"VB/Office Guru™ SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
SpellMe = msSpell
End If
'</CHECK SPELLING AND GRAMMER DIALOG BOX>
oDoc.Close(False)
oDoc = Nothing
'<HIDE WORD IF THERE ARE NO OTHER INSTANCES>
If KillMe = True Then
moApp.Visible = False
End If
'</HIDE WORD IF THERE ARE NO OTHER INSTANCES>
Catch ex As Exception
MessageBox.Show(ex.Message, "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
End Function
End Class
It works fine in vb.net but i want it in C#. I have also tried using line
http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx
but all in vain,.. so plz
Public Class clsSpellMe
Friend moApp As New Word.Application
Private mbKillMe As Boolean
Friend Property KillMe() As Boolean
Get
InitializeMe()
KillMe = mbKillMe
End Get
Set(ByVal Value As Boolean)
mbKillMe = Value
End Set
End Property
Friend Sub InitializeMe()
Try
'<INITIALIZE WORD>
moApp = DirectCast(GetObject(, "Word.Application"), Word.Application)
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Word.Application"), Word.Application)
mbKillMe = True
Else
MessageBox.Show(ex.Message, "SpellChecker™.NET", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
End Sub
Friend Function SpellMe(ByVal msSpell As String) As String
Dim oDoc As Word.Document
Dim iWSE As Integer
Dim iWGE As Integer
Dim iResp As Integer
Dim sReplace As String
If msSpell = String.Empty Then
Else
Try
InitializeMe()
Select Case moApp.Version
Case "9.0", "10.0", "11.0"
oDoc = moApp.Documents.Add(, , 1, True)
Case "8.0"
oDoc = moApp.Documents.Add
Case Else
MessageBox.Show("Unsupported Version of Word.", "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Function
End Select
oDoc.Words.First.InsertBefore(msSpell)
iWSE = oDoc.SpellingErrors.Count
iWGE = oDoc.GrammaticalErrors.Count
'<CHECK SPELLING AND GRAMMER DIALOG BOX>
If iWSE > 0 Or iWGE > 0 Then
'<HIDE MAIN WORD WINDOW>
moApp.Visible = False
If (moApp.WindowState = Word.WdWindowState.wdWindowStateNormal) Or _
(moApp.WindowState = Word.WdWindowState.wdWindowStateMaximize) Then
moApp.WindowState = Word.WdWindowState.wdWindowStateMinimize
Else
moApp.WindowState = Word.WdWindowState.wdWindowStateMinimize
End If
moApp.Quit()
'</HIDE MAIN WORD WINDOW>
'<PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.CheckGrammarWit hSpelling = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.SuggestSpelling Corrections = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreUppercase = False
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreInternetA ndFileAddresses = True
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreMixedDigi ts = False
moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.ShowReadability Statistics = False
'</PREP CHECK SPELLING OPTIONS DIALOG BOX (MODIFY TO YOUR PREFERENCES)>
'<DO ACTUAL SPELL CHECKING>
moApp.Visible = True
moApp.Activate()
iResp = moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Display
'</DO ACTUAL SPELL CHECKING>
If iResp < 0 Then
moApp.Visible = True
MessageBox.Show("Corrections Being Updated!", "VB/Office Guru™ SpellChecker™", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
oDoc.Select()
oDoc.Range.Copy()
sReplace = DirectCast(Clipboard.GetDataObject.GetData("System.String", True), String)
'<FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
If (InStrRev(sReplace, Chr(13) & Chr(10))) = (Len(sReplace) - 1) Then
sReplace = Mid$(sReplace, 1, Len(sReplace) - 2)
End If
'</FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
SpellMe = sReplace
ElseIf iResp = 0 Then
'MessageBox.Show("Spelling Corrections Have Been Canceled!", "VB/Office Guru™ SpellChecker™.NET", _
'MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
SpellMe = msSpell
End If
Else
'MessageBox.Show("No Spelling Errors Found" & Environment.NewLine & "Or No Suggestions Available!", _
'"VB/Office Guru™ SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
SpellMe = msSpell
End If
'</CHECK SPELLING AND GRAMMER DIALOG BOX>
oDoc.Close(False)
oDoc = Nothing
'<HIDE WORD IF THERE ARE NO OTHER INSTANCES>
If KillMe = True Then
moApp.Visible = False
End If
'</HIDE WORD IF THERE ARE NO OTHER INSTANCES>
Catch ex As Exception
MessageBox.Show(ex.Message, "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
End Function
End Class