Consulting

Results 1 to 12 of 12

Thread: Connect to Another Word Document without Opening

  1. #1

    Connect to Another Word Document without Opening

    From a Word document, I need to call to another Word document and capture specific bookmark sections.

    Document A gets opened by a 3rd party software.
    When a user form button is clicked on Document A, it needs to pull Bookmark Named ranges from Document B and paste the content of them into documet A.

    Due to the behavior of the 3rd party software, I can not just open Document B invisible, because it thinks when you close Document B, you are closing document A.

    I just want to pull the content from one word document to the other, without opening.

    Thans for any suggestions.




    I cannot use the Documents.Open with visibility hidden because items are called from a 3rd party and when

  2. #2
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    You're definitely not going to be able to access the bookmarks collection using some kind of generic system file open like FreeFile type stuff (a la, without using Word to open the document) without some pretty heavy coding to identify the bookmark ranges. It would be difficult but possible in 2007/2010 if the file is formatted in xml. If it is a binary file format (Word 2003 or earlier), this is basically impossible.

    However, you might be able to use CreateObject to open a second instance of Word, open the document using that second instance, and then get the info you need, and close the document and quit the second instance of Word.

    If you need something more specific, please post again. It's not terribly worthwhile to post code samples when there is nothing to test against (sounds like pretty shoddy coding if the 3rd party app is confusing documents, visible or not-- and I would first investigate ways of not opening a second instance of Word, including contacting that 3rd party vendor to make them get their product to work better)

  3. #3
    XML is no good, because I use form fields and tables as well inside it.
    I currently do open a 2nd instance of word invisible, however the close action seems to confuse the 3rd party software. It works on some computers and not others.

    Unfortunately, it is our own company that is the 3rd party provider and I am not part of that side of the development team.

  4. #4
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Not to be snippy, but if you're using CreateObject properly and the 3rd party app is still getting confused, my guess is that it might have to do with either the OS and/or the admin rights of the current user. Either way, it sounds like the right hand (you) needs to have a conversation with the left hand (your development team).

    This will be but the tip of an iceberg of problems for clients you distribute this application to otherwise.

  5. #5
    I am currently not using the CreateObjects method, I am using Application.documents.open.

    Would there be any difference? I don't see why there would be, but you have helped me out in the past

  6. #6
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Application.Documents.Open is using the current application to do something.

    I don't have time to give you a sample at the moment, but look up GetObject and CreateObject (there are a lot of examples, although typically people are using GetObject/CreateObject from separate applications... so people in Excel have need of using/launching Word, people in Word have need of using/launching Excel).

    In your case, it seems like you might have a need to launch a separate process of Word from within your existing Word process. Just make sure you quit that new process when you've gotten what you need.

    These methods actually create new processes (i.e., if you look in your Task Manager, before doing CreateObject on the word process, you would have a single WINWORD.EXE in your processes list-- after it, you would have two WINWORD.EXE processes listed).

    Don't believe the task bar when it shows multiple documents open-- that's just for convenience. You're still only utilizing one instance of Word (typically).

    See if you can do a little research on CreateObject and then post the code you're working with. I should (or someone else) be able to help you from there (or, if I get done with my current thing, I can whip up the code for you).

  7. #7
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Here's a bit of sample code, using conditional compiling. The reason I use conditional compiling here is simply to make it easier to write my code using autocomplete with the appWord object (setting it to an Application type within Word VBA will let you manipulate stuff a little more easily-- always working with a generic object means you need to know exactly the properties/methods you want to use).

    Hope this helps:
    [VBA]
    #Const DEVELOPING = True
    Public Sub CreateDifferentWordInstance()
    Dim doc As Document
    #If DEVELOPING Then
    Dim appWord As Application
    #Else
    Dim appWord As Object
    Set appWord = CreateObject("Word.Application")
    #End If
    With appWord
    'get your document (this is just a sample)
    Set doc = Documents.Add
    With doc
    .Content.InsertAfter "Hello, I'm some text"
    .Saved = True
    .Close
    End With
    #If DEVELOPING = False Then
    'quit the app
    .Quit
    #End If
    End With
    'release the object
    Set appWord = Nothing
    End Sub
    [/VBA]

  8. #8
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Ack... missed the most critical part... need to do .Documents not just Documents.

    [VBA]
    #Const DEVELOPING = False
    Public Sub CreateDifferentWordInstance()
    Dim doc As Document
    #If DEVELOPING Then
    Dim appWord As Application
    #Else
    Dim appWord As Object
    Set appWord = CreateObject("Word.Application")
    #End If
    With appWord
    'or not?
    .Visible = True
    'get your document (this is just a sample)
    Set doc = .Documents.Add
    With doc
    .Content.InsertAfter "Hello, I'm some text"
    .Saved = True
    .Close
    End With
    #If DEVELOPING = False Then
    'quit the app
    .Quit
    #End If
    End With
    'release the object
    Set appWord = Nothing
    End Sub
    [/VBA]

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could achieve the desired result via the insertion of an INCLUDETEXT field in the target document, pointing to the bookmark in the source document. Since it appears the source document might be using formfields, you might find you'll need to add the '\!' switch the the target document's INCLUDETEXT field.

    When you're done, the INCLUDETEXT field can either be left there (for future automatic updates) or unlinked (which just leaves the output in the target document).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    That's an excellent suggestion, Paul-- I've so rarely encountered a need for this, I never think of the builtin functions which allow pseudo-linking of documents.

    Let us know how it goes, ASKolnick.

  11. #11
    If the content you want to add in doc 2 is bookmarked you can addit to doc 1 using the following code: [VBA]Selection.InsertFile FileName:="C:\Doc_Name.doc", Range:="Bookmark_Name", ConfirmConversions:=False, Link:=False, Attachment:=False[/VBA]

    I use this code to insert paragraphs of text with text formfields into my document and then I am able to put text into those formfields as well. I think it works great and I do not have to open or do anything to the document that contains the paragraphs I use. Hope it helps in what you are doing.

  12. #12

    Gratitude

    Thanks. This alternate method may be exactly what I am looking for.

Posting Permissions

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