Results 1 to 5 of 5

Thread: URGENT : VB.NET code covertion in C#.Net

  1. #1

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Exclamation URGENT : VB.NET code covertion in C#.Net

    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/uti...btocsharp.aspx

    but all in vain,.. so plz
    Code:
    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.CheckGrammarWithSpelling = True
                        moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.SuggestSpellingCorrections = True
                        moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreUppercase = False
                        moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreInternetAndFileAddresses = True
                        moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.IgnoreMixedDigits = False
                        moApp.Dialogs(Word.WdWordDialog.wdDialogToolsSpellingAndGrammar).Application.Options.ShowReadabilityStatistics = 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
    Last edited by Hack; Feb 22nd, 2006 at 06:52 AM. Reason: Added [code] [/code] tags for more clarity.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: URGENT : VB.NET code covertion in C#.Net

    You can compile it to a DLL as is and use it in your C# apps if need be. If not, what issues did you come across when using the converter. It's probably easier to deal with them than to convert the whole lot. Also, please use [Highlight=VB] tags for VB code and [code] tags for other code. It makes things much easier to read.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: URGENT : VB.NET code covertion in C#.Net

    In fact, it's easier to convert the whole thing by hand than figuring out what part of the code is causing the converter to error out!

    The rules are quite simple... once your'e familiar with C# syntax. For example, you don't declare variables like

    Dim x as myclass

    Instead,

    myclass x

    And my pet peeve... If is spelt if. With a small i.

    That fact right there used to waste hours of my time when I was coding for the first time.

    The horror, the horror...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: URGENT : VB.NET code covertion in C#.Net

    Quote Originally Posted by mendhak
    In fact, it's easier to convert the whole thing by hand than figuring out what part of the code is causing the converter to error out!

    The rules are quite simple... once your'e familiar with C# syntax. For example, you don't declare variables like

    Dim x as myclass

    Instead,

    myclass x

    And my pet peeve... If is spelt if. With a small i.

    That fact right there used to waste hours of my time when I was coding for the first time.

    The horror, the horror...
    All key words in C# are lower case, just like they are all upper case by default in VB. The converter will sort things like that without issue though. It's more likely to be something like the Select Case that's the issue, because a C# switch statement can be used for numerical values only. The Run-time functions are likely to be a sticking point too, as the converter may be able to handle standard Framework stuff only. Isn't that one of the reasons we often give for not using Runtime functions in VB? At a glance I can't see anything else that's likely to cause a problem.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: URGENT : VB.NET code covertion in C#.Net

    Our Instant C# VB to C# converter produces (note that there are a couple of things that have no direct .NET equivalent, like InStrRev & GetObject - these are left as calls to the VB namespace):

    Code:
    public class clsSpellMe
    {
    
    	internal Word.Application moApp = new Word.Application();
    	private bool mbKillMe;
    
    	internal bool KillMe
    	{
    		get
    		{
    			InitializeMe();
    			return mbKillMe;
    		}
    		set
    		{
    			mbKillMe = value;
    		}
    	}
    
    	internal void InitializeMe()
    	{
    		try
    		{
    			//<INITIALIZE WORD>
    			moApp = (Word.Application)(Microsoft.VisualBasic.Interaction.GetObject(null, "Word.Application"));
    		}
    		catch (Exception ex)
    		{
    			if (Microsoft.VisualBasic.Information.TypeName(moApp) == "Nothing")
    			{
    				moApp = (Word.Application)(Microsoft.VisualBasic.Interaction.CreateObject("Word.Application", ""));
    				mbKillMe = true;
    			}
    			else
    				MessageBox.Show(ex.Message, "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    		}
    	}
    
    	internal string SpellMe(string msSpell)
    	{
    		string tempSpellMe = null;
    
    		Word.Document oDoc = null;
    		int iWSE = 0;
    		int iWGE = 0;
    		int iResp = 0;
    		string sReplace = null;
    
    		if (msSpell == string.Empty)
    		{
    
    		}
    		else
    		{
    			try
    			{
    				InitializeMe();
    				switch (moApp.Version)
    				{
    					case "9.0":
    					case "10.0":
    					case "11.0":
    						oDoc = moApp.Documents.Add(,, 1, true);
    						break;
    					case "8.0":
    						oDoc = moApp.Documents.Add();
    						break;
    					default:
    						MessageBox.Show("Unsupported Version of Word.", "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    						return null;
    				}
    				oDoc.Words.First.InsertBefore(msSpell);
    				iWSE = oDoc.SpellingErrors.Count;
    				iWGE = oDoc.GrammaticalErrors.Count;
    				//<CHECK SPELLING AND GRAMMER DIALOG BOX>
    				if (iWSE > 0 | iWGE > 0)
    				{
    					//<HIDE MAIN WORD WINDOW>
    					moApp.Visible = false;
    					if ((moApp.WindowState == Word.WdWindowState.wdWindowStateNormal) | (moApp.WindowState == Word.WdWindowState.wdWindowStateMaximize))
    						moApp.WindowState = Word.WdWindowState.wdWindowStateMinimize;
    					else
    						moApp.WindowState = Word.WdWindowState.wdWindowStateMinimize;
    					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)
    					{
    						moApp.Visible = true;
    						MessageBox.Show("Corrections Being Updated!", "VB/Office Guru™ SpellChecker™", MessageBoxButtons.OK, MessageBoxIcon.Information);
    						oDoc.Select();
    						oDoc.Range.Copy();
    						sReplace = (string)(Clipboard.GetDataObject().GetData("System.String", true));
    						//<FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
    						if ((Microsoft.VisualBasic.Strings.InStrRev(sReplace, '\r' + '\n', -1, Microsoft.VisualBasic.CompareMethod.Binary)) == (sReplace.Length - 1))
    							sReplace = sReplace.Substring(0, sReplace.Length - 2);
    						//</FIX FOR POSSIBLE EXTRA LINE BREAK AT END OF TEXT>
    						tempSpellMe = sReplace;
    					}
    					else if (iResp == 0)
    					{
    						//MessageBox.Show("Spelling Corrections Have Been Canceled!", "VB/Office Guru™ SpellChecker™.NET", _
    						//MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    						tempSpellMe = msSpell;
    					}
    				}
    				else
    				{
    					//MessageBox.Show("No Spelling Errors Found" & Environment.NewLine & "Or No Suggestions Available!", _
    					//"VB/Office Guru™ SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    					tempSpellMe = msSpell;
    				}
    				//</CHECK SPELLING AND GRAMMER DIALOG BOX>
    				oDoc.Close(false);
    				oDoc = null;
    				//<HIDE WORD IF THERE ARE NO OTHER INSTANCES>
    				if (KillMe == true)
    					moApp.Visible = false;
    				//</HIDE WORD IF THERE ARE NO OTHER INSTANCES>
    			}
    			catch (Exception ex)
    			{
    				MessageBox.Show(ex.Message, "SpellChecker™.NET", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    			}
    		}
    		return tempSpellMe;
    	}
    }
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width