Vb.Net Loading Word

Hi

I have vb.net with word 2010 using imports Microsoft.office.interop.

In the project references I have Microsoft word 14.0 object library
and my project compiles correctly

I gave the project to a friend and he has word 2013
in the project references he has Microsoft word 15.0 object library
he gets an error Type Microsoft.office.interop.word.application is not defined
and In the program I set dim wrd as Word.Dialog
it give error word.dialog is not found
KevinfeeningAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
In order to make use of different versions of MS Office products, I recommend using Late Binding.

For example, you would do this with Early Binding (as you have used)...

Dim wrd As New Word.Application
Dim doc As Word.Document = wrd.Document.Open(filepath)

Open in new window


In Late Binding you do not reference the object library, and use CreateObject to initialize...

Dim wrd As Object = CreateObject("Word.Application")
Dim doc As Object = wrd.Document.Open(filepath)

Open in new window


Doing it in this manner will eliminate most problems with differing versions.

See here and here for more information.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jacques Bourgeois (James Burger)PresidentCommented:
Late binding as explained by Wayne is the way to go. However you have be aware that it is not recommended unless you cannot do otherwise (which is the case with different versions of Office), and that for complex things, it's not an easy way to go.

The problem is that because an Object variable can be anything, the code editor is not able to validate that the properties and methods that you call really exist, that the parameters that you pass are of the right number and types, and that you catch return values in variables that are appropriate for the returned type.

This is made even more difficult by the fact that IntelliSense cannot provide you with the list of properties, methods and parameters that you usually get when working with early binding (using references and types variables instead of Object).

Debugging the application can be painful. It is thus better to make sure that everything works fine with Word.Applicatino and Word.Document and the like variables, and then modify the declarations to work with Object.

Also, in order to work with early binding, Option Strics must be set to Off. Most professional programmers will tell you that this should usually be set to On in order to let the editor and compiler flag a lot a potential problems. It is thus usually better to leave Option Strict to On in the project's properties, and create a module in wich you set Option Strict Off at the top. Off will then apply only to that module, that you can use for all the operations that require to work with Word.
0
KevinfeeningAuthor Commented:
Thanks sorry been away

Used late binding
Found code to use for spell checking have a richtextbox and a textbox  get these errors no matter which textbox I use

txtComment.SelStart = 0 - not member of textbox / not member of richtextbox
txtComment.SelLength = Len(txtComment.Text) - not member of textbox / not member of richtextbox
Clipboard.SetText(txtComment.SelText) - not member of textbox / not member of richtextbox
txtComment.Text = Clipboard.GetText(vbCFText) - vbCFText is not declared

Private Declare Function CoAllowSetForegroundWindow Lib "ole32.dll" (ByVal pUnk As Object, ByVal lpvReserved As Long) As Long

    Private Sub SpellCheck()
        Dim oWord As Object
        Dim oTmpDoc As Object
        Dim lOrigTop As Long

        ' Create a Word document object
        oWord = CreateObject("Word.Application")

        CoAllowSetForegroundWindow(oWord, 0)

        oTmpDoc = oWord.Documents.Add
        ' Position Word off screen to avoid having document visible
        lOrigTop = oWord.Top
        oWord.WindowState = 0
        oWord.Top = -3000

        oWord.Visible = True
        oWord.Activate()

        ' Copy the contents of the text box to the clipboard
        txtComment.SelStart = 0
        txtComment.SelLength = Len(txtComment.Text)
        Clipboard.Clear()
        Clipboard.SetText(txtComment.SelText)

        ' Assign the text to the document and check spelling

        With oTmpDoc
            .Content.Paste()
            .Activate()
            .CheckSpelling()

            ' After the user has made changes, use the clipboard to
            ' transfer the contents back to the text box
            .Content.Copy()
            txtComment.Text = Clipboard.GetText(vbCFText)
            ' Close the document and exit Word
            .Saved = True
            .Close()
        End With
        oTmpDoc = Nothing

        oWord.Visible = False

        oWord.Top = lOrigTop
        oWord.Quit()
        oWord = Nothing 
    end sub

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Wayne Taylor (webtubbs)Commented:
The code you've got looks like it comes from VB6. As per the error message, SelStart is not a member of the TextBox class. A few changes you can make...

Instead of this...

        ' Copy the contents of the text box to the clipboard
        txtComment.SelStart = 0
        txtComment.SelLength = Len(txtComment.Text)
        Clipboard.Clear()
        Clipboard.SetText(txtComment.SelText)

Open in new window


... which sends the entire textbox contents to the clipboard, you can just use this....

    Clipboard.SetText(txtComment.Text)

Open in new window


To return the text back to the textbox, use this....

            ' After the user has made changes, use the clipboard to
            ' transfer the contents back to the text box
            .Content.Copy()
            txtComment.Text = Clipboard.GetText()

Open in new window


Your complete modified code...

Private Declare Function CoAllowSetForegroundWindow Lib "ole32.dll" (ByVal pUnk As Object, ByVal lpvReserved As Long) As Long

    Private Sub SpellCheck()
        Dim oWord As Object
        Dim oTmpDoc As Object
        Dim lOrigTop As Long

        ' Create a Word document object
        oWord = CreateObject("Word.Application")

        CoAllowSetForegroundWindow(oWord, 0)

        oTmpDoc = oWord.Documents.Add
        ' Position Word off screen to avoid having document visible
        lOrigTop = oWord.Top
        oWord.WindowState = 0
        oWord.Top = -3000

        oWord.Visible = True
        oWord.Activate()

        ' Copy the contents of the text box to the clipboard
        Clipboard.SetText(txtComment.Text)

        ' Assign the text to the document and check spelling

        With oTmpDoc
            .Content.Paste()
            .Activate()
            .CheckSpelling()

            ' After the user has made changes, use the clipboard to
            ' transfer the contents back to the text box
            .Content.Copy()
            txtComment.Text = Clipboard.GetText()
            ' Close the document and exit Word
            .Saved = True
            .Close()
        End With
        oTmpDoc = Nothing

        oWord.Visible = False

        oWord.Top = lOrigTop
        oWord.Quit()
        oWord = Nothing 
    end sub

Open in new window

0
Jacques Bourgeois (James Burger)PresidentCommented:
Wayne seems to be right.

Be careful when you take code from somewhere else. VBScript, VB6 and VB.Net are different languages although they share a lot of common features. The environments in which they run are also different.

On a .NET TextBox, SelStart and SelLength have been replaced by SelectionStart and SelectionLength. You would have catched that easily if you had try to typed de command manually when you saw that they did not work from the cut and paste you probably did. The name of the properties would have appeared in the list of properties provided by IntelliSense.

If you have a background in VB6, try to lose your old habits. Things usually require less code in .NET than it did in VB6. In you case, as Wayne showed, you can do the whole operation in one line instead of three, because you do not need to select the text before sending it to the clipboard.

And when you need to do something that you cannot do directly in the framework, it is always better to try to find something that was designed from .NET instead of using external software to do the job. Things designed for .NET are usually easier to use, and working with Word will not work on computers that do not have Office installed.

You do not tell us which version of Visual Studio you are using, but in the recent ones, if you call Extensions and Updates in the Tools menu, you can look for add-ins for Visual Studio, many of which are free. If you look for Spell Check in there, you will find many that spell check inside of Visual Studio, but a few that also works in Windows or ASP applications.
1
KevinfeeningAuthor Commented:
Thanks - Visual Studios 2008 professional vsn 9.0.30729.4462 QFE
Microsoft.net framework Vsn 3.5 SP1 2007
0
Jacques Bourgeois (James Burger)PresidentCommented:
It's hard to follow these things, but I think that the Extensions and Update feature came in only in 2010.

Do you know that Visual Studio 2015 Community is the equivalent of the Professional, but is free for solo programmers or small companies. And it has more features than 2008 had. Skipping versions can lead to problems in the long run, and you already skipped 3 (2010-2012-2013).
0
KevinfeeningAuthor Commented:
Thanks Again

Added the code and when CoAllowSetForegroundWindow(oWord, 0) in in the code I get error
**** SUMMARY ****
LEVEL:0     TYPE: 'System.AccessViolationException'
MESSAGE: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

Remove CoAllowSetForegroundWindow(oWord, 0)  and works fine on my computer

Also I copy the program to my second computer and as explained in an open question Word Spell checker where I stated

I have 2 computers both with the same Windows 7 Professional service pack 7
 In VB I run the code supplied to get a spellchecker
 1 PC the spellchecker appears correctly but the other it sends to the back and you have to Alt TAB to see it
 where can I look to in the computers to fix the problem the one that works OK runs Word 2010 the other runs Word 2013

Any Thoughts
0
MacroShadowCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Wayne Taylor (webtubbs) (https:#a40981102)
-- Jacques Bourgeois (James Burger) (https:#a40982842)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

MacroShadow
Experts-Exchange Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.