Avatar of jdc1944
jdc1944
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Normal.dot template in use - VBA

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?
Microsoft WordVB Script

Avatar of undefined
Last Comment
jdc1944

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
GrahamSkan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
GrahamSkan

Incidentally setting an object to nothing doesn't achieve anything.  It happens automatically when the procedure ends.
GrahamSkan

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

jdc1944

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23