finding a string in a word document

M

Medes

Hi,

I have a shared add-in that contains a simple button , i want that my button
do this:

when you click the button a windows opens that contains a textbox and a "ok"
button when you write a string in the textbox and clik on the "ok" button it
finds the string in the word document,

I am newbie , how it possible?

thanks,
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TWVkZXM=?=,
I have a shared add-in that contains a simple button , i want that my button
do this:

when you click the button a windows opens that contains a textbox and a "ok"
button when you write a string in the textbox and clik on the "ok" button it
finds the string in the word document,

I am newbie , how it possible?
Well, speaking only about locating the string in the document (that's what's in
your subject line), I suggest you begin by going into the Word interface.
Familiarize yourself with how Edit/Find works. Then start the macro recorder
(Tools/Macro/Record new macro) and repeat the actions you've practised (finding
any text string). Stop the macro recorder (you should see a "Macro" toolbar
with a Stop button). Press Alt+F11 to go into Word's VB-Editor, open the
"NewMacros" module and look at the recorded code.

This gives you the basic syntax you require in your Addin. By clicking in any
of the terms in the recorded code, then pressing F1, you can open Help to that
topic if you need more information.

Note that the macro recorder will almost always work with the SELECTION object.
For the most part, you should substitute SELECTION with the appropriate object
from Word's object model. In the case of FIND, that would be RANGE. (So,
instead of Selection.Find you should use Range.Find).

When automating Word and using FIND, you need to be aware of this potential
problem
Find crashes Word automation code
..NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;313104
COM
http://support.microsoft.com/kb/292744/EN-US/

If you need more assistance, you must tell us in which programming environment
you're working, and what version of Word you're targeting.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
M

Medes

Thank you so much ,

I use microsoft office 2003 and Visual Studio tools for office 2005 and
programming C#.

I dont know if i explain my problem correctly,

I can get or insert text in my document (word) if i have the button directly
in my ToolBar.
But when you click on the button which located on my Toolbar a new
System.Windows.Forms.Form is oppened , this Form contains a Button
(InsertBTN) when you click on this Button i get following error:

System.NullReferenceException: Object reference not set to an instance of an
object.
at MyAddin1.IgnitoForm.InsertBTN_Click(Object sender, EventArgs e) in
.......

here is a little bit of my code:

public partial class IgnitoForm : Form
{
Word.Application wordApp;
Connect MyConnect = new Connect();

private void OkBTN_Click(object sender, EventArgs e)
{
wordApp = MyConnect.wordApp;
wordApp.ActiveWindow.Selection.InsertBefore("some text"); //here
is the error
}
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?TWVkZXM=?=,

I'm guessing you need to pass the word application object your Addin is linked
to to the constructor of your form. That, or you need to use LateBinding
(GetObject) to get the currently running instance of the Word application. But
I'd pass the ThisDocument object from the VSTO instance to the form's
constructor...

If I'm understanding your code correctly, you're starting up a new instance of
something (whatever "Connect" is), and this may not be giving you what you
expect.
I use microsoft office 2003 and Visual Studio tools for office 2005 and
programming C#.

I dont know if i explain my problem correctly,

I can get or insert text in my document (word) if i have the button directly
in my ToolBar.
But when you click on the button which located on my Toolbar a new
System.Windows.Forms.Form is oppened , this Form contains a Button
(InsertBTN) when you click on this Button i get following error:

System.NullReferenceException: Object reference not set to an instance of an
object.
at MyAddin1.IgnitoForm.InsertBTN_Click(Object sender, EventArgs e) in
.......

here is a little bit of my code:

public partial class IgnitoForm : Form
{
Word.Application wordApp;
Connect MyConnect = new Connect();

private void OkBTN_Click(object sender, EventArgs e)
{
wordApp = MyConnect.wordApp;
wordApp.ActiveWindow.Selection.InsertBefore("some text"); //here
is the error
}

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top