Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

access, VB and word

  • 04-05-2005 1:33pm
    #1
    Closed Accounts Posts: 202 ✭✭


    I've got this program which needs to open a word doc (the same one each time) from access and make a few adjustments to it then save the doc in a different name. this I can do.

    the problem is that when I run the program more than once it gives me an error saying that the first doc is in use by another user. In other words I dont think the program is closing the word doc properly.

    this is the code to open it:

    Dim wordApp As Word.Application
    Set wordApp = CreateObject("Word.Application")
    Dim wordDoc As Word.Document
    Set wordDoc = wordApp.Documents.Open(FileName:=Name)

    (all the modifying done to the doc is in here)


    then this closes it:

    wordDoc.close
    Set wordDoc = Nothing
    wordApp.Quit
    Set wordApp = Nothing


    if anyone can see what I'm doing wrong it'd be a great help. thanks


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Are you running two instances of your program at the same time? If so that's your problem - the first instance has locked the word document and won't unlock it until it's instance of word.exe has closed the file.


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    From (dim) memory, Word/VB is a horrid combination for exactly the type of problem you're describing.

    If I'm remembering right, its the case that even when you use wordApp.Quit, Word isn't actually closed (or is only closed in some cases if you do the right stuff...whatever that is). Its only actually properly closed and re-referenced when the VB app itself quits. And when developing, that (may) involve closing and re-opening the Visual Studio IDE.

    Dumb. Ugly. Microsoft.

    I also seem to recall a bug existing in Word (amongst other apps) which means that it doesn't release its reference to the last document closed when no other doc remains open...until Word is properly exited or another document is opened.

    So....your solution should be just to make worApp a global variable initialised to Nothing, and simply test at the start something like :

    if (wordApp is Nothing) then
    Set wordApp = CreateObject("Word.Application")
    end if

    Of course, all of the above may be a load of horse. It could be that what you're saying is that if you have more than one copy of the application open at the same time then you get this problem. Well...of course you will...each copy of the App will have its own copy of Word, and only one can lock the doc. If this is what you're saying, then read up on the parameters for wordApp.Documents.Open and one of those should allow you to open in "shared" mode.

    jc


  • Closed Accounts Posts: 202 ✭✭eyebrows


    I only have one instance of the program running at a time. I forgot to say last time that on the first run it works(as I said) on the second run I get:
    (when trying to edit the doc)

    "run-time error '462'

    The remote server machine does not exist or is unavailable"

    and only then (after the first error, 3rd try) do i get an error saying it's in use by another user (which I now see only happens cos the program never got to the closing part before aborting itself)

    to set wordApp as a global do I just create it at the top of the program outside any subs. Then where do I initialise it to Nothing(at the start of the sub?)

    BTW thanks for the help


  • Closed Accounts Posts: 202 ✭✭eyebrows


    I also seem to recall a bug existing in Word (amongst other apps) which means that it doesn't release its reference to the last document closed when no other doc remains open...until Word is properly exited or another document is opened.

    ok bonkey I think your right. if I run the program with a different word doc opened aswell it works for as many times as I want. so it must be somthing to do with it not derefencing itself.


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    eyebrows wrote:
    "run-time error '462'
    At the risk of pointing out the obvious...when you get an error like this, the absolutely first thing you should do is cut n paste it into google.

    If you did, you'd find the first link led you to this Microsoft Knowledgebase Article.
    to set wordApp as a global do I just create it at the top of the program outside any subs. Then where do I initialise it to Nothing(at the start of the sub?)

    Yup. Thats it. I *think* (haven't coded VB in a while) you can say :

    Dim wordApp as Nothing

    Alternately, don't worry about the init-to-Nothing bit for now.
    ok bonkey I think your right. if I run the program with a different word doc opened aswell it works for as many times as I want. so it must be somthing to do with it not derefencing itself.
    Maybe / Probably

    Check the KB article I linked to above...it explains why the app wouldn't be exiting cleanly when you told it to. If you fix that problem, the references should go away, and everything will be hunky dory.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Dim wordApp As Word.Application

    Shouldn't this be enough? Without Set wordApp = new Word.Application then the object is nothing.

    I'd put the Set wordApp = new Word.Application into Sub Main(). Where ever my application ends (and I try to have only one place for that in VB) is where I'd then dereference wordApp. I take it that's what you mean by initialise it to nothing?


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Evil Phil wrote:
    Dim wordApp As Word.Application

    Shouldn't this be enough?

    Should be, yeah. I guess its just habit from other languages that I init everything explicitly.
    I'd put the Set wordApp = new Word.Application into Sub Main(). Where ever my application ends (and I try to have only one place for that in VB) is where I'd then dereference wordApp. I take it that's what you mean by initialise it to nothing?

    Y'huh. Sub Main()....thats what I couldn't think of the name of.

    jc


  • Closed Accounts Posts: 202 ✭✭eyebrows


    Cheers lads. Got it working now (spent ages trying to get that to work last week)


  • Registered Users, Registered Users 2 Posts: 254 ✭✭collsoft


    eyebrows wrote:
    I've got this program which needs to open a word doc (the same one each time) from access and make a few adjustments to it then save the doc in a different name. this I can do.

    the problem is that when I run the program more than once it gives me an error saying that the first doc is in use by another user. In other words I dont think the program is closing the word doc properly.

    this is the code to open it:

    Dim wordApp As Word.Application
    Set wordApp = CreateObject("Word.Application")
    Dim wordDoc As Word.Document
    Set wordDoc = wordApp.Documents.Open(FileName:=Name)

    (all the modifying done to the doc is in here)


    then this closes it:

    wordDoc.close
    Set wordDoc = Nothing
    wordApp.Quit
    Set wordApp = Nothing


    if anyone can see what I'm doing wrong it'd be a great help. thanks


    OK, I know that you got your code working, but I had the same problem with a mail merge application that I was working on, and it took me ages to figure out what was going wrong.

    When you create an instance of Word.Application, there are a number of implicit global objects created in the word object library (ActiveDocument for example) which you can see using the object browser to look at the "globals" section of the Word Object Library

    Some of these global objects reference your particular instance of the Word.Application object.

    These globals continue to exist even after your call to "Set wordApp = Nothing".

    Here is where the problem comes - a lot of Word function calls have optional paramaters, which if not set, use some of these global references.

    The first time your code runs, these global objects are instanced, reference your wordApp and everything is OK - wordApp is then killed.

    The second time your code runs, you have a new wordApp object, and you make a call which does not explicity define each optional paramater - this causes one of the internal global objects to get used. However, this global object has a reference to the WordApp from the first run -which does not exist anymore - this causes the error 462 (remote machine disconnected) and it causes your program to crash and terminate.

    Now if you examine the process list in task manager you will see that you have still got a winword.exe process running. This is where word has got your file still open in the background. This process is still running because the error 462 resulted in your wordApp.Quit & Set wordApp = Nothing not being called.

    Now when you run the program for the 3rd time, you get the "File aleady in use" error because of the hidden Word process. In fact, if you open task manager now you will see that you have two winword.exe processes.


Advertisement