K Feening
asked on
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.w ord.applic ation is not defined
and In the program I set dim wrd as Word.Dialog
it give error word.dialog is not found
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.w
and In the program I set dim wrd as Word.Dialog
it give error word.dialog is not found
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
... which sends the entire textbox contents to the clipboard, you can just use this....
To return the text back to the textbox, use this....
Your complete modified code...
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)
... which sends the entire textbox contents to the clipboard, you can just use this....
Clipboard.SetText(txtComment.Text)
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()
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
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.
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.
ASKER
Thanks - Visual Studios 2008 professional vsn 9.0.30729.4462 QFE
Microsoft.net framework Vsn 3.5 SP1 2007
Microsoft.net framework Vsn 3.5 SP1 2007
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).
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).
ASKER
Thanks Again
Added the code and when CoAllowSetForegroundWindow (oWord, 0) in in the code I get error
**** SUMMARY ****
LEVEL:0 TYPE: 'System.AccessViolationExc eption'
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
Added the code and when CoAllowSetForegroundWindow
**** SUMMARY ****
LEVEL:0 TYPE: 'System.AccessViolationExc
MESSAGE: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
Remove CoAllowSetForegroundWindow
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
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
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
ASKER
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(txtComme
txtComment.Text = Clipboard.GetText(vbCFText
Open in new window