none
Spellcheck in VB error RRS feed

  • Question

  • I have the below code, but in the line range = doc.Range(), there is an error saying it can't convert COM to string.

    Imports Microsoft.Office.Interop
    
    Private Sub ButtonClicked() Handles ButtonSpellCheck.Click
          Dim wordApp As New Word.Application
                wordApp.Visible = False
                Dim doc As Word.Document = wordApp.Documents.Add()
                Dim range As Word.Range
                range = doc.Range()
                range.Text = TextBox1.Text
                doc.Activate()
                doc.CheckSpelling()
                Dim chars() As Char = {CType(vbCr, Char), CType(vbLf, Char)}
                TextBox1.Text = doc.Range().Text.Trim(chars)
                doc.Close(SaveChanges:=False)
                wordApp.Quit()
    End Sub

    Basically, when a button is clicked, it is converted to a Word document and checked for spelling that way.

    Could you please help to fix my code so the error doesn't occur?


    JDS404 Check out my blog at www.interlinkjds.wordpress.com!

    • Moved by Amanda Zhu Monday, July 14, 2014 8:36 AM
    Saturday, July 12, 2014 12:00 PM

Answers

  • Hi JDS

    Try using = doc.Content instead and see if that gives you a better result.

    doc.Content should give you the entire main document "Story" as a RAnge. The term you're using, .Range(), is a METHOD with two parameters. VBA will manage to return a Range object, but .NET doesn't always. So try it with .Content and see if that helps :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, July 14, 2014 2:13 PM
    Moderator

All replies

  • Hi,

    I moved this thread to Word for Developers forum for better support.

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 14, 2014 8:37 AM
  • Hi,

    Based on the code you want to use the Spell Check feature of Word applicaiton. I tested the code, there is no error as you described above. The error message is very strange since you are not translate the range to a string.

    Are you developing with Visual Studio? If yes, I suggest you repari it and Miscrosoft .Netframwork to see whether this issue was fixe.

    In addition, I don't think you can achieve the goal via the code. If there is spell errors, the Word applicaiton displays the Spelling and Grammar dialog box and it bloks the code to execute since the application is not visible.

    Hope it is helpful.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 14, 2014 9:07 AM
    Moderator
  • Hi,

    Based on the code you want to use the Spell Check feature of Word applicaiton. I tested the code, there is no error as you described above. The error message is very strange since you are not translate the range to a string.

    Are you developing with Visual Studio? If yes, I suggest you repari it and Miscrosoft .Netframwork to see whether this issue was fixe.

    In addition, I don't think you can achieve the goal via the code. If there is spell errors, the Word applicaiton displays the Spelling and Grammar dialog box and it bloks the code to execute since the application is not visible.

    Hope it is helpful.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thanks for replying.

    I don't think I'm going to repair it, but if you say that the code I have doesn't work properly to achieve it's aim, could you suggest alternative code for a spellchecker?

    Also, yes I am using Visual Studio 2013.

    Thanks again.


    ---- JDS404 ---- Check out my blog at www.interlinkjds.wordpress.com!


    • Edited by JDS404 Monday, July 14, 2014 10:04 AM
    Monday, July 14, 2014 9:43 AM
  • Hi JDS

    Try using = doc.Content instead and see if that gives you a better result.

    doc.Content should give you the entire main document "Story" as a RAnge. The term you're using, .Range(), is a METHOD with two parameters. VBA will manage to return a Range object, but .NET doesn't always. So try it with .Content and see if that helps :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, July 14, 2014 2:13 PM
    Moderator
  • Hi JDS

    Try using = doc.Content instead and see if that gives you a better result.

    doc.Content should give you the entire main document "Story" as a RAnge. The term you're using, .Range(), is a METHOD with two parameters. VBA will manage to return a Range object, but .NET doesn't always. So try it with .Content and see if that helps :-)


    Cindy Meister, VSTO/Word MVP, my blog


    Thanks, that helped!

    ---- JDS404 ---- Check out my blog at www.interlinkjds.wordpress.com!

    Monday, July 14, 2014 2:30 PM