+ Reply to Thread
Results 1 to 12 of 12

Set word variable to a document that’s already open

  1. #1
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Set word variable to a document that’s already open

    Hi there,

    Thanks for looking.

    I'm using the following variables as part of my larger procedure...

    Please Login or Register  to view this content.
    ...and setting them as follows:

    Please Login or Register  to view this content.
    I just want to know how I'd set the wrdMyDoc variable if the desired document is already opened as I don't want to re-open it again in an another instance of Word as is the current case.

    Note that as the code uses early binding a reference to 'Microsoft Word nn.n Object Library' is required. Also cell C1 contains the directory while cell C2 contains the file name.

    Any advice would be greatly appreciated.

    Kind regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Set word variable to a document that’s already open

    Try something like this see if it helps:

    Please Login or Register  to view this content.
    Last edited by millz; 11-22-2013 at 12:17 AM. Reason: add wdDoc.Close

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Set word variable to a document that’s already open

    Hi millz,

    Thanks for that. Unfortunately it always produces the 'Bad file name' error at this line...

    Please Login or Register  to view this content.
    ...even though the file is definitely open and visible. I do vaguely recall this being a known issue and that the only way around it was to use the document's index number which I'm not sure how to get, would you?

    The only other way I can think of is to try and return each name in the Taskbar to check if the file is open but this seems like overkill to me.

    Appreciate any further help.

    Robert

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Set word variable to a document that’s already open

    Oh, I tried with a simple 'new' document, and it was having a name of "Document1". I am guessing you need to trim the file extension from the file name (cell C2).

    Maybe:
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Set word variable to a document that’s already open

    No, still no go. Did you get it to work on a local machine or from a server (as I'm trying)?

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Set word variable to a document that’s already open

    This is what I just tried again. If word is not opened, macro launches it, and open the document Doc1.docx. If Doc1 is already opened, no error occurs.

    Edit: I work between Access and Excel a lot, so part of the code is what I actually use to launch Excel from Access, only amended to try to make it work for you. I don't know how differently would it work between a local machine and a server.

    Please Login or Register  to view this content.
    Last edited by millz; 11-22-2013 at 02:41 AM.

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Set word variable to a document that’s already open

    No, whatever I try it keeps returning the Bad File Name error message. I'll keep digging.

    I really appreciate your efforts

    Regards,

    Robert

  8. #8
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Set word variable to a document that’s already open

    Perhaps you can try with GetObject("path of file")

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Set word variable to a document that’s already open

    Perhaps you can try with GetObject("path of file")
    No as that opens the same document in a separate instance of Word - just what I'm trying to avoid.

    I've almost solved it with this, I found the work around was to use an object if Word is already open as so (the code is quite long as it copies a named range from Excel into a bookmark of the same name in Word and has error checking built in):

    Please Login or Register  to view this content.
    The only remaining issue is the cell fill color from Excel isn't be transferred to the Word bookmark. Not sure why.

    Thank you both, especially millz

    Robert

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Set word variable to a document that’s already open

    I am not sure I understand. Using:
    Please Login or Register  to view this content.
    if the document is already open the code will refer to it and assign its Application to wrdApp; if the document is not open it will be opened and its Application assigned to wrdApp.

  11. #11
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Set word variable to a document that’s already open

    Hi Izandol,

    Yes, that does seem like the case. I think it had opened the same document from another test I was doing (I've been on this for days!!).

    Great work Izandol and thanks for posting

    Robert

  12. #12
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Set word variable to a document that’s already open

    I believe you intended to give me good reputation? If yes, thank you for the intention at least.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Already Open Word Document
    By yawnzzzz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-16-2014, 11:22 PM
  2. open excel source document in background when i open word document
    By hootiebsc in forum Word Programming / VBA / Macros
    Replies: 5
    Last Post: 03-22-2013, 07:50 PM
  3. Word browsing folder and open txt files in word document.
    By naseerrahaman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-16-2013, 03:38 AM
  4. Replies: 0
    Last Post: 06-07-2012, 02:21 PM
  5. [SOLVED] Assign a variable to a word document
    By Anson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-06-2005, 05:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1