PDA

View Full Version : Connect to Another Word Document without Opening



ASkolnick
08-31-2011, 10:00 AM
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

Frosty
08-31-2011, 10:55 AM
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)

ASkolnick
08-31-2011, 01:32 PM
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.

Frosty
08-31-2011, 01:56 PM
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.

ASkolnick
08-31-2011, 02:35 PM
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

Frosty
08-31-2011, 02:50 PM
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).

Frosty
08-31-2011, 05:21 PM
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:

#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

Frosty
08-31-2011, 07:35 PM
Ack... missed the most critical part... need to do .Documents not just Documents.


#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

macropod
09-09-2011, 05:40 AM
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).

Frosty
09-09-2011, 03:40 PM
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.

Solrac3030
09-12-2011, 02:32 PM
If the content you want to add in doc 2 is bookmarked you can addit to doc 1 using the following code: Selection.InsertFile FileName:="C:\Doc_Name.doc", Range:="Bookmark_Name", ConfirmConversions:=False, Link:=False, Attachment:=False

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.

ASkolnick
09-13-2011, 06:25 AM
Thanks. This alternate method may be exactly what I am looking for.