Results 1 to 13 of 13
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    GetObject (Word 2000)

    Hi

    I have a problem using Visual Basic for Word.

    I have a form, from which the user can select from a list of documents (variable=lstMergeDocument). Whenever they change their selection, the macro examines the custom properties in the selected document (which isn't open at that time).

    The relevant code starts like this:
    <pre>Private Sub lstMergeDocument_Change()
    Dim MyWordFile As Object, MyProperty$
    If Trim$(lstMergeDocument) = "" Then GoTo Finish
    Set MyWordFile = GetObject(ActiveDocument.Path + Application.PathSeparator + "Subdocs" _
    + Application.PathSeparator + lstMergeDocument + ".doc")
    </pre>


    The last statement (Set MyWordFile ...) usually, but not always, causes this error:
    <hr>Runtime error 287. Appication-defined or object-defined error<hr>

    Note: The code within parentheses (activedocument .... doc) evaluates correctly.


    Any ideas?
    Dale

  2. #2
    Star Lounger
    Join Date
    Nov 2001
    Location
    Illinois, USA
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Dale,
    Have you tried including "Word.Document" as the second parameter in the GetObject function in case VBA automation thinks there is more than one app that can open a .doc file? Also, there is a KB article Q163492 that talks about problems with automation getting an object due to delays in Word if it is busy doing something else at the time; not sure if this might be relevant to your problem, but may be worth a look.

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Thanks, Dottie

    I tried adding "Word.Document" to the GetObject statement, but no joy.

    I also waited till the machine was doing nothing, then pressed F8 repeatedly, but still got the same message. (I presume this proves it isn't a delay problem.)

    Grrrr@#$#%@?

    But thanks for the suggestion
    Dale

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: GetObject (Word 2000)

    I don't get that error in my little test. I do notice that tilde-prefixed temp files are created, and they don't go away even if I set the object to Nothing. Any chance you are creating a new instance of WinWord that is running into a problem with an open file??

    Is it necessary to reference the file in this manner? Assuming you are using DSOFILE.DLL, you can use a string for the file name:

    Dim myPropReader As New DSOleFile.PropertyReader, myProps As DSOleFile.DocumentProperties
    Set myProps = myPropReader.GetDocumentProperties(strTestFile)
    Stop 'Inspect Locals window
    Set myProps = Nothing
    Set myPropReader = Nothing

    (I did get "file is open" errors for all the files for which I had used GetObject, even though I shut down and restarted Word, and deleted the tilde-prefixed files. Something bad going on there.) Hope this helps.

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Unfortunately, I don't have DSOFILE.DLL on my (Windows98) machine.

    Thanks for the suggestion
    Dale

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: GetObject (Word 2000)

    It is freely available here: <A target="_blank" HREF=http://support.microsoft.com/support/kb/articles/Q224/3/51.ASP>Dsofile.exe Lets You Edit Office Document Properties from Visual Basic and ASP</A>. But if you weren't using DSOFile, how were you reading the properties without opening the file?

  7. #7
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    I searched my PC again for dsofile, and definitely don't have it. Maybe Office 2000 uses a different DLL?

    I tried installing dsofile, but (like you) get unexpected (unwanted) results. In the end, I decided to give up and use a workaround. Grrr.

    Thanks for the suggestions, though
    Dale

  8. #8
    Star Lounger
    Join Date
    Nov 2001
    Location
    Illinois, USA
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Hi Dale,
    Having had little experience with UserForms in Word, I have only just discovered that GetObject behaves differently in a UserForm than it does in a Word Document. Originally, in testing your code, I had just added the list box, command button, and code directly to a Word document, and was unable to recreate the error; GetObject worked every time. Today, out of curiosity I moved it to a UserForm, and, lo and behold, got the exact error you've been fighting. I am glad to hear that you have developed a workaround, but, just in case you're still looking for alternatives, you might try moving your stuff to a document. Also, if anyone out there knows why GetObject acts differently between Document and UserForm, an explanation would be greatly appreciated. Thanks.

  9. #9
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Hi Dottie

    I tried moving the code from the template to the document, and got a marked improvement. In particular, the runtime error no longer appears.

    However, "Set MyWordFile=Nothing" doesn't remove the document from the list of projects (in the Visual Basic editor). So, if you repeat the process for different files, you end up with a whole pile of projects listed in the editor, and Word eventually hangs.

    As jscher said, earlier in this thread, "something bad is going on there".

    Thanks for the suggestion
    Dale

  10. #10
    Star Lounger
    Join Date
    Nov 2001
    Location
    Illinois, USA
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Hi Dale,
    Those references are really behaving strangely! After reading your comments, I tried running the code again in the document and saw just what you are describing. Besides seeing the pile of projects in the editor, when I looked at Tools --> References, each of the documents I'd used the GetObject method on showed as references named "Project" in the list but without checkmarks! If I tried to check them "it" complained that it would be a conflict with an existing reference. Iterating through the Documents collection revealed that each of the referenced documents was there as though they had been opened. Adding a MyWordFile.Close before the Set MyWordFile = Nothing seems to resolve the issue; no pile of projects listed in the editor. However, if the documents are sort of getting opened anyway (I'm guessing they are 'cause they are in the Documents collection), maybe you'd be just as well off using a Documents.Open instead of the misbehaving GetObject.

  11. #11
    Star Lounger
    Join Date
    Nov 2001
    Location
    Illinois, USA
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Oops! I probably spoke too soon when suggesting using an outright Open. It works, even in the UserForm, if you don't mind the document being visible, but if you open it with the visible parameter set to False you get the same unpleasant behavior as with the GetObject. Hmm.., so does this mean GetObject does an invisible open? Well, I guess since you didn't want the documents Open in the first place you won't want them to be visible, so this isn't going to be a help. Sorry.

  12. #12
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Hi Dottie

    I tried "MyWordFile.Close SaveChanges:=wdDoNotSaveChanges", but it ignores the switch and asks "do I want to save changes". I also turned off auto macros ("WordBasic.DisableAutoMacros 1"), but that didn't stop the message from appearing.

    In this particular situation, I'd be happy to actually open the document, provided it wasn't visible. How do you do that? I can't find a Visible property for a document object.

    Dale

  13. #13
    Star Lounger
    Join Date
    Nov 2001
    Location
    Illinois, USA
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: GetObject (Word 2000)

    Hi Dale,

    Actually, the visibility is controlled by a parameter in the open method:
    Set MyWordFile = Documents.Open(strDocName, , , , , , , , , , , False).
    However, when I tried this out the invisible documents behaved just as badly as the ones obtained through the GetObject method, so it might still wreak havoc with your references.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •