Consulting

Results 1 to 7 of 7

Thread: From Excel , determine if Word is open

  1. #1

    Question From Excel , determine if Word is open

    Hi All,

    I am at novice level.
    I have been reading other "not dis-similiar" threads, but have failed to grasp what bits of code I need/dont need.
    So I'll ask the question.

    Background.
    I have a macro, that resides in an excel file.
    The macro does this.......
    copy multiple variables from excel sheet1
    open specific word document_1
    paste those variables in the correct place
    delete specific paragraphs (based on excel variables)
    save file as document_2
    The macro works very well indeed.

    Question
    There may be a problem occuring if either document_1 or document_2 is open.
    I thought the way to go would be this...
    Test if Word App is open
    If Yes, then stop excel macro (with excel msgbox message)
    If No, then continue.

    So, how do I test if Word App is open, please.



    My relevant code that may be of use.....
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
     
    Set objDoc = objWord.Documents.Open("I:\HOFolders\Raymond\Word Development\Ex 08 - semi final\HR Doc B - Template.doc")
    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use GetObject

    [vba]

    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    On Error Goto 0
    If Not objWord Is Nothing Then

    MsgBox "Word already open"
    End If
    [/vba]

    But why not just use that instance of Word if it is open?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3

    Post

    Quote Originally Posted by xld
    But why not just use that instance of Word if it is open?
    I guess I could, but as a novice, I was thinking that.....
    ..if Word App is not open, then by definition Word Doc_1 (and also Word Doc_2) is therefore not open.

    Perhaps rather 'crude and unrefined' but it would satisfy the basic test.
    I guess with more experience (and knowledge) I could refine it to be more succint.



    Just so I can expand my limited knowledge......
    What does getObject do ?
    What 'value' does it assign ?
    What if there are 2 objects 'open' ? (does it pick the 'first' ?)

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    GetObject will just pick up an instance of the targetted application. As you can see in my code, there is a Set variable that uses GetObject, so that variable will have a reference to that instance (if one exists, else it will be Nothing). If there are 2 instances, it will pick up one, but it is indeterminate which one.

    You can test if a document is already open, withing getting a WOrd instance at all.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thanks for the explanation.

    Quote Originally Posted by xld
    You can test if a document is already open, withing getting a Word instance at all.
    Maybe that is a better (more specific) way to go.
    How would I test to see if documant_1 is open.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    In a similar manner, set an object variable to the document and check it for nothing.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location

    Another option

    If you are using HR Doc B - Template.doc as a template, then save it as a template, then open a new file based on the template. This way, you only have one file open.

    [vba]Set objDoc = objWord.Documents.Add("I:\HOFolders\Raymond\Word Development\Ex 08 - semi final\HR Doc B - Template.dot")[/vba]

    David


Posting Permissions

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