We help IT Professionals succeed at work.

Normal.dot template in use - VBA

jdc1944
jdc1944 asked
on
845 Views
Last Modified: 2012-05-28
As a bit of a background i have used a word document to add several command buttons.  The document is used as a bit of a menu to open various other documents.  When one of these buttons is pressed the code checks a .ini file for various things before it opens the desired document.

One of the problems i was having with the .ini file is that i wasn't opening/closing the object properly and as a result it didn't take long for a huge amount of WINWORD.EXE proccesses to appear and cause me grief.  To try and prevent this i tried to change the code i had written to open up the .ini file to declare and delete the word.exe objects (if thats what they are called).  This worked and i no longer get the WINWORD.EXE processes still appearing when i had finsihed when them.

This worked for a while but i'm now getting an error message every time i press one of the buttons.

"This file is in use by another application or user. (C:\Documents and Settings\...\Normal.dot)"

Now im no expert in VBA but i thought the problem might be that i am creating a word object to look at the .ini file (even though one has already been created automatically to open the Word menu) and i am then trying to delete that object even though it might still be in use by the Word menu.  This menu system is used by about 10 other people who don't get this error message.  I have also tried deleting the normal.dot template incase that was the isue.

The code i use to open the .ini file is
Function ReadINI(Path As String, Section As String, _
    Key As String) As String
Dim wd As Word.Application
    fileName = Path & "\settings.ini"
    ReadINI = ""
    Set wd = New Word.Application ' create the Word application object
    On Error Resume Next
    ReadINI = wd.System.PrivateProfileString(fileName, Section, Key)
    wd.Quit
    On Error GoTo 0
    Set wd = Nothing ' destroy the Word application object
End Function

Open in new window


Is there a problem with the way i am reading the .ini file or does it lay with Word and the template?

If it is the code, i still need some way to delete the word object every time i read the .ini file to stop it creating a large amount of redundant processes.

Any Ideas?
Comment
Watch Question

Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
Incidentally setting an object to nothing doesn't achieve anything.  It happens automatically when the procedure ends.
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
Also, where are you running this? If it is a Word macro, then you must already be in an instance of the  application.

Function ReadINI(Path As String, Section As String, _
    Key As String) As String
Dim wd As Word.Application
    fileName = Path & "\settings.ini"
    ReadINI = ""
    Set wd = Application ' use the current the Word application object
    On Error Resume Next
    ReadINI = wd.System.PrivateProfileString(fileName, Section, Key)
    On Error GoTo 0
End Function

Open in new window

Author

Commented:
Thanks for that.  It is running from a word macro so there is an instance already running.

I have just used the code example you provided and it works perfectly, thank you.

It has stopped the error message i was getting with the template file and it also doesn't create the extra WINDWORD.EXE processes.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.