PDA

View Full Version : Multiple Active Douments



Benji84
09-02-2010, 04:33 AM
Hi,

I have created a userform that works using bookmark ranges. However, I was wondering whether it is possible for data from the userform to be sent to more than one active document.

I am redesigning my form so that the user can give an answer which is available for public viewing and for private i.e. once the user has finished inputting the data in the form they can save it into one active document and then edit that information and save it into another active document.

Thanks

Ben

Tinbendr
09-02-2010, 10:03 AM
When working with multiple documents, it's always best to specify them so that there is no change of cross-data insertion.

Dim SrcDoc As Document
Dim DestDoc As Document
Set ScrDoc = Documents.Open("SourceDocument.docx")
Set DestDoc = Documents.Open("DestinationDocument.docx")
'or
Dim aDoc As Document
Set aDoc = ActiveDocument

If you load from a template, then...

Dim SrcDoc As Document
Set SrcDoc = Documents.Add("Template")

fumei
09-02-2010, 11:37 AM
There is only - EVER - one Active Document.

That being said, Tinbendr is correct. If you make use of document objects, whatever document happens to be active becomes completely irrelevant.

So say you have:

Yadda.doc (Active)
Blah.doc (non-Active but open and therefore part of the Documents collection)
Whatever.doc (non-Active but open and therefore part of the Documents collection)

Sub DoSomething_WithNonActiveDoc()
Dim ThisDoc As Document
Dim AnotherDoc As Document
Dim YetAnotherDoc As Document

Set ThisDoc = ActiveDocument ' becomes Yadda.doc which is Active)
Set AnotherDoc = Documents("Blah.doc")
Set YetAnotherDoc = Documents("Whatever.doc")

YetAnotherDoc.Sections(1).Headers(1).Range.Text = "HoHoHo"
With AnotherDoc.Range.Find
.Text = "Merry Christmas"
.Replacement.Text = "Santa Claus"
.Execute Replace:=wdreplaceAll
End With
End Sub

1. The open (but NOT Active) document Whatever.doc (set as the object YetAnotherDoc) has the text "HoHoHo" inserted into the primary header of section 1;

2. The open (but NOT Active) document Blah.doc (set as the object AnotherDoc) has a Find and replace action performed

with NEITHER document ever becoming Active.

And yes, you can open a new document, or create a new document using document objects (as shown by Tinbendr's post).