Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word add-on that uses a text file "help needed" 1

Status
Not open for further replies.

richiwatts

Technical User
Jun 21, 2002
180
GB
I am a complete beginner to VBA and with a lot of help I have
create a little add on i want for word. However, now I am getting a bit stuck.

This small add-on searches through text in a Word document for words that match those of a text file I have that contains about 3000 words. If it finds matching words it highlights them.
Anyway, I want to send this add-on to people I know and to get the add-on to work, all they have to do is put the .dot file in the word start up folder so that it will open when Word does. That bit isn't too hard to explain.
But the code requires that the person specifies where the text file is and i don't want to make people to have to go in a chnage the code. Are you still with me?

How can i make sure that the add-on has access to the word list in the text file if people put it on there computer? So that they don't have to change any code.

Is it possible for these words into the .dot file itself so it can search itself? That was just a guess and maybe someone has a better idea
 
Richiwatts,

I would adopt a strategy of copying the text file to the same location as your add-in, in this case, the Startup folder. Now you can use the following so that your code can determine where the text file is located:

Code:
MyPath = AddIns("YourAddInName").Path

Combine this with your filename when you open the text file.


HTH
M. Smith
 
That sound good,

I will give it a try thank you!
 
M Smith

It has taken me a while to get round to giving it a try.

Anyway i am getting an error, What am I doing wrong:

Before i had:
Open "c:\temp\data.txt" For Input As #1

I have changed it to what you said and I am getting an error message:

Open MyPath = AddIns("data.txt").Path For Input As #1

Have I entered it wrong? the error message I get is:
Compile error in hidden module: module 1


 
you should read Mike Smith's post more carefully

>>I would adopt a strategy of copying the text file to the >>same location as your add-in, in this case, the Startup >>folder. Now you can use the following so that your code >>can determine where the text file is located:
>>
>>MyPath = AddIns("YourAddInName").Path
>>
>>Combine this with your filename when you open the text file.
>>
>>
>>HTH
>>M. Smith

thus, you change

[tt]Open "c:\temp\data.txt" For Input As #1[/tt]

to

[tt]MyPath = AddIns("YourAddInName").Path
Open MyPath & "\data.txt" For Input As #1[/tt]
 
Justin, thanks for your clarification to Richiwatts. But I think he will still have a problem. I, in turn, should have read his post a little more carefully. Even though he specified Word, I saw "add-on" and translated it to mean Excel Add-In. He has created a template not an add-in (which apparently in Word is a compiled .wll file similar to a dll).

Richiwatts -- You can still use the same strategy, except that you will get the path from the ThisDocument object (representing your template). Here is the relevant code:

Code:
Path = ThisDocument.Path
  Open Path & Application.PathSeparator & "Data.txt" For Input As #1

Let us know if you have any other problems.

Regards,
M. Smith
 
No it is still not working. The file is a macro saved as a .dot file. I now have the .dot file and the .txt file in the start up folder. I have changed the code in hte Macro to:

Sub FindWords()
Dim oRg As Range
Dim LineFromFile As String, LineString As String
Dim WordArray As Variant
Dim indx As Integer, LparenPos As Integer, RparenPos As Integer

Path = ThisDocument.Path
Open Path & Application.PathSeparator & "data.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, LineString
LparenPos = InStr(LineString, "(")
Do While LparenPos > 0
RparenPos = InStr(LparenPos, LineString, ")")
If RparenPos > 0 Then
LineString = Left(LineString, LparenPos - 2) & _
Right(LineString, Len(LineString) - RparenPos)
LparenPos = InStr(LineString, "(")
End If
Loop
LineFromFile = LineFromFile & vbTab & LineString
Loop
Close #1

What could be wrong?
 
Explain what is happening when you run your macro. What errors are you getting, if any? I mocked up my solution and it worked fine, reading a line from a dummy text file.

Regards,
Mike
 
As soon as i try to run the macro I get this message.

Compile error in hidden module.

If i use this line of code:
Open "c:\temp\data.txt" For Input As #1
and put the data.txt file in the temp folder it works.

If I use this (yours)
Path = ThisDocument.Path
Open Path & Application.PathSeparator & "Data.txt" For Input As #1
and put the data.txt file in Word's start up folder, the same place as the .dot file that contains the macro i get the message.

I am using Office and Word 2000 if it makes a difference.

Richi

 
Richi,

The problem is you have not declared Path as a variable. DIMension this as String, re-save, then install the macro .dot file back to the Startup folder.

Regards,
Mike
 
You are a star Mike!!

It shows that i am a beginner doesn't it? I suppose everyone here started somewhere. :)
 
Richi,

Don't feel too bad. We've all been there! [sadeyes]

Some things you can do to help avoid this: In the VBA Editor, under Tools/Options Editor Tab, be sure "Require Variable Declaration" is checked. Develop your VBA code with your template open like a document until everything is working correctly, then final test it when actually loaded by Word on startup.

In this particular instance, the rather cryptic message "compile error in hidden module" occurred because your code was running from a loaded template, which automatically hides its code. Normally, you would have received the error message "Compile error: Variable not defined" which is much more informative.

Good Luck
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top