Reference form fields between separate documents? 
Author Message
 Reference form fields between separate documents?

Hi there,

Bit of a long and complicated one this so bare with my while I try to
explain.

I have a document (doc1 say) that has bookmarked form fields for user
input. What I would like to produce is a macro (or some procedure) that
opens a new document (doc2 say), inserts a letter template, copies and
pastes some values from the fields in doc1, opens a print dialogue box
that asks the user how many copies of doc2 they would like to print,
prints the document the requested number of times then closes the
document without saving.

Basically I want to remove any user input for doc2 other than asking how
many copies are required for print, as all the information for the
letter is contained in fields in doc1. I do not need to save doc2, I
just need to be able to compile and print letter on the fly.

The first problem that I have hit is referencing the fields in doc1 from
doc2.When I have copied text from fields in the past I have done so in
the same document using something like the following code;

With ActiveDocument.Bookmarks
         .DefaultSorting = wdSortByName
         .ShowHidden = False
End With
Selection.TypeText Text:=ActiveDocument.FormFields("field1").Result

Is there a way that I can say 'look in any document' rather than just
the active document??

And is the rest of the automation that I would like possible??

Hope this makes sense.

Carl.



Fri, 30 Jul 2004 20:21:55 GMT  
 Reference form fields between separate documents?
Hi Carl,

Sounds like this is a protected form, not a userform.   (Userform is
Microsoft's term for a custom dialog box, a graphical pop-up that you
create in the Visual Basic Editor.)  At any rate:

The short answer is: object variables.

    Dim doc1 as Document, doc2 as Document
    ' Assuming doc1 is open & active now:
    Set doc1 = ActiveDocument
    ' Assign new throwaway doc to second object variable
    Set doc2 = Documents.Add

Now you can refer to each of them by their variable name and transfer
your field results like this [untested]:

    doc2.Range.InsertAfter doc1.FormFields("Text1").Result & vbCr
    doc2.Range.InsertAfter doc1.FormFields("Text2").Result & vbCr
    ' etc.

    doc2.PrintOut Background:=False
    doc2.Close SaveChanges:=wdDoNotSaveChanges    

Note: Since you probably want to run this code from doc1 and since doc1
is protected, you should probably run it as an Exit Macro or Entry Macro
in that form.

Hope this helps.


-- See the MVP FAQ at http://www.mvps.org/word ---------------------------
------------------ "Life is nothing if you're not obsessed." --John Waters
--------------------------------------------------------------------------
Reply to group ONLY. No attachments please. MVPs do not work for Microsoft.

Quote:

> Hi there,

> Bit of a long and complicated one this so bare with my while I try to
> explain.

> I have a document (doc1 say) that has bookmarked form fields for user
> input. What I would like to produce is a macro (or some procedure) that
> opens a new document (doc2 say), inserts a letter template, copies and
> pastes some values from the fields in doc1, opens a print dialogue box
> that asks the user how many copies of doc2 they would like to print,
> prints the document the requested number of times then closes the
> document without saving.

> Basically I want to remove any user input for doc2 other than asking how
> many copies are required for print, as all the information for the
> letter is contained in fields in doc1. I do not need to save doc2, I
> just need to be able to compile and print letter on the fly.

> The first problem that I have hit is referencing the fields in doc1 from
> doc2.When I have copied text from fields in the past I have done so in
> the same document using something like the following code;

> With ActiveDocument.Bookmarks
>          .DefaultSorting = wdSortByName
>          .ShowHidden = False
> End With
> Selection.TypeText Text:=ActiveDocument.FormFields("field1").Result

> Is there a way that I can say 'look in any document' rather than just
> the active document??

> And is the rest of the automation that I would like possible??

> Hope this makes sense.

> Carl.



Fri, 30 Jul 2004 21:20:31 GMT  
 Reference form fields between separate documents?
This is superb information Mark, sounds like just what I need. One quick
question if I may? I have bookmarks in doc2 which are the points at
which I want to paste\reference the entered field information from doc1.
This was my original code for doing this in the same document (sorry I
missed the first line out in my initial message)

Selection.GoTo What:=wdGoToBookmark, Name:="bookmark1"
     With ActiveDocument.Bookmarks
         .DefaultSorting = wdSortByName
         .ShowHidden = False
     End With
Selection.TypeText Text:=ActiveDocument.FormFields("text1").Result

How do I move to the relevant bookmark in doc2 using whilst keeping your
code? what does the 'doc2.Range.InsertAfter' code refer to?

Thanks once more.

Carl.

Quote:

> Hi Carl,

> Sounds like this is a protected form, not a userform.   (Userform is
> Microsoft's term for a custom dialog box, a graphical pop-up that you
> create in the Visual Basic Editor.)  At any rate:

> The short answer is: object variables.

>     Dim doc1 as Document, doc2 as Document
>     ' Assuming doc1 is open & active now:
>     Set doc1 = ActiveDocument
>     ' Assign new throwaway doc to second object variable
>     Set doc2 = Documents.Add

> Now you can refer to each of them by their variable name and transfer
> your field results like this [untested]:

>     doc2.Range.InsertAfter doc1.FormFields("Text1").Result & vbCr
>     doc2.Range.InsertAfter doc1.FormFields("Text2").Result & vbCr
>     ' etc.

>     doc2.PrintOut Background:=False
>     doc2.Close SaveChanges:=wdDoNotSaveChanges    

> Note: Since you probably want to run this code from doc1 and since doc1
> is protected, you should probably run it as an Exit Macro or Entry Macro
> in that form.

> Hope this helps.


> -- See the MVP FAQ at http://www.mvps.org/word ---------------------------
> ------------------ "Life is nothing if you're not obsessed." --John Waters
> --------------------------------------------------------------------------
> Reply to group ONLY. No attachments please. MVPs do not work for Microsoft.


>>Hi there,

>>Bit of a long and complicated one this so bare with my while I try to
>>explain.

>>I have a document (doc1 say) that has bookmarked form fields for user
>>input. What I would like to produce is a macro (or some procedure) that
>>opens a new document (doc2 say), inserts a letter template, copies and
>>pastes some values from the fields in doc1, opens a print dialogue box
>>that asks the user how many copies of doc2 they would like to print,
>>prints the document the requested number of times then closes the
>>document without saving.

>>Basically I want to remove any user input for doc2 other than asking how
>>many copies are required for print, as all the information for the
>>letter is contained in fields in doc1. I do not need to save doc2, I
>>just need to be able to compile and print letter on the fly.

>>The first problem that I have hit is referencing the fields in doc1 from
>>doc2.When I have copied text from fields in the past I have done so in
>>the same document using something like the following code;

>>With ActiveDocument.Bookmarks
>>         .DefaultSorting = wdSortByName
>>         .ShowHidden = False
>>End With
>>Selection.TypeText Text:=ActiveDocument.FormFields("field1").Result

>>Is there a way that I can say 'look in any document' rather than just
>>the active document??

>>And is the rest of the automation that I would like possible??

>>Hope this makes sense.

>>Carl.



Fri, 30 Jul 2004 23:17:49 GMT  
 Reference form fields between separate documents?

The doc2.Range.InsertAfter assumed you just wanted to paste the values into
an *empty* doc2.  (It basically says, insert the values after the range of
the entire document.)  To do what you want, substitute these lines instead:

  doc2.Bookmarks("Aaaa").Range.InsertAfter doc1.FormFields("Text1").Result
  doc2.Bookmarks("Bbbb").Range.InsertAfter doc1.FormFields("Text1").Result
  ' etc.

where Aaaa, Bbbb, etc., are the names of the bookmarks in doc2.

Two things are wrong with your code below, and one involves an important
concept.  Selection.GoTo is the least efficient way to address bookmarked
locations.  The macro recorder always uses the Selection object in the
code it writes on your behalf, simply because it can't guess what you
really want to do and thus merely records the result of each action.  
Using Ranges instead is almost always simpler, faster, and more reliable.
With bookmarks using Ranges is even easier than usual because each bookmark
has a range already defined, so you don't need the usual 'Set' statement
to define your own.

Second, the .DefaultSorting property affects only the way bookmarks are
displayed in the Insert-> Bookmarks dialog.  And that's irrelevant to the
steps you'll use (even if you use the Selection object).

Hope this helps.


-- See the MVP FAQ at http://www.mvps.org/word ---------------------------
------------------ "Life is nothing if you're not obsessed." --John Waters
--------------------------------------------------------------------------
Reply to group ONLY. No attachments please. MVPs do not work for Microsoft.

Quote:

> This is superb information Mark, sounds like just what I need. One quick
> question if I may? I have bookmarks in doc2 which are the points at
> which I want to paste\reference the entered field information from doc1.
> This was my original code for doing this in the same document (sorry I
> missed the first line out in my initial message)

> Selection.GoTo What:=wdGoToBookmark, Name:="bookmark1"
>      With ActiveDocument.Bookmarks
>          .DefaultSorting = wdSortByName
>          .ShowHidden = False
>      End With
> Selection.TypeText Text:=ActiveDocument.FormFields("text1").Result

> How do I move to the relevant bookmark in doc2 using whilst keeping your
> code? what does the 'doc2.Range.InsertAfter' code refer to?

> Thanks once more.

> Carl.


> > Hi Carl,

> > Sounds like this is a protected form, not a userform.   (Userform is
> > Microsoft's term for a custom dialog box, a graphical pop-up that you
> > create in the Visual Basic Editor.)  At any rate:

> > The short answer is: object variables.

> >     Dim doc1 as Document, doc2 as Document
> >     ' Assuming doc1 is open & active now:
> >     Set doc1 = ActiveDocument
> >     ' Assign new throwaway doc to second object variable
> >     Set doc2 = Documents.Add

> > Now you can refer to each of them by their variable name and transfer
> > your field results like this [untested]:

> >     doc2.Range.InsertAfter doc1.FormFields("Text1").Result & vbCr
> >     doc2.Range.InsertAfter doc1.FormFields("Text2").Result & vbCr
> >     ' etc.

> >     doc2.PrintOut Background:=False
> >     doc2.Close SaveChanges:=wdDoNotSaveChanges

> > Note: Since you probably want to run this code from doc1 and since doc1
> > is protected, you should probably run it as an Exit Macro or Entry Macro
> > in that form.

> > Hope this helps.


> > -- See the MVP FAQ at http://www.mvps.org/word ---------------------------
> > ------------------ "Life is nothing if you're not obsessed." --John Waters
> > --------------------------------------------------------------------------
> > Reply to group ONLY. No attachments please. MVPs do not work for Microsoft.


> >>Hi there,

> >>Bit of a long and complicated one this so bare with my while I try to
> >>explain.

> >>I have a document (doc1 say) that has bookmarked form fields for user
> >>input. What I would like to produce is a macro (or some procedure) that
> >>opens a new document (doc2 say), inserts a letter template, copies and
> >>pastes some values from the fields in doc1, opens a print dialogue box
> >>that asks the user how many copies of doc2 they would like to print,
> >>prints the document the requested number of times then closes the
> >>document without saving.

> >>Basically I want to remove any user input for doc2 other than asking how
> >>many copies are required for print, as all the information for the
> >>letter is contained in fields in doc1. I do not need to save doc2, I
> >>just need to be able to compile and print letter on the fly.

> >>The first problem that I have hit is referencing the fields in doc1 from
> >>doc2.When I have copied text from fields in the past I have done so in
> >>the same document using something like the following code;

> >>With ActiveDocument.Bookmarks
> >>         .DefaultSorting = wdSortByName
> >>         .ShowHidden = False
> >>End With
> >>Selection.TypeText Text:=ActiveDocument.FormFields("field1").Result

> >>Is there a way that I can say 'look in any document' rather than just
> >>the active document??

> >>And is the rest of the automation that I would like possible??

> >>Hope this makes sense.

> >>Carl.



Sat, 31 Jul 2004 02:28:46 GMT  
 Reference form fields between separate documents?
Carl,

One other conceptual point: Whenever you find yourself visualizing what
your code should do and saying it should "move to" a certain location
and insert material there, you can almost always improve the code by
abandoning the "move" idea and focusing on using Range objects.  You
can still insert material, but you do it, in effect, by just specifying
the place, not going there.  Screen flicker is reduced, your code runs
faster, and you're less likely to see unreliable behavior, especially
if switching between documents or working with non-visible documents.

It can be tough at first to get the hang of working with Range objects
because it diverges from the idea of recording the keystrokes you would
use to do the task manually.  But once it clicks for you, you'll always
write better code that way.  In some situations, most notably placing
material in a header or footer, trying to use Selections and/or recorded
code is not only inefficient but also near impossible (and thus endlessly
frustrating for one who hasn't made the mental leap out of the recording
mindset).


-- See the MVP FAQ at http://www.mvps.org/word --------------------------
----------------- "Life is nothing if you're not obsessed." --John Waters
-------------------------------------------------------------------------
Reply to group ONLY. Do not attach files. MVPs do not work for Microsoft.

Quote:

> The doc2.Range.InsertAfter assumed you just wanted to paste the values into
> an *empty* doc2.  (It basically says, insert the values after the range of
> the entire document.)  To do what you want, substitute these lines instead:

>   doc2.Bookmarks("Aaaa").Range.InsertAfter doc1.FormFields("Text1").Result
>   doc2.Bookmarks("Bbbb").Range.InsertAfter doc1.FormFields("Text1").Result
>   ' etc.

> where Aaaa, Bbbb, etc., are the names of the bookmarks in doc2.

> Two things are wrong with your code below, and one involves an important
> concept.  Selection.GoTo is the least efficient way to address bookmarked
> locations.  The macro recorder always uses the Selection object in the
> code it writes on your behalf, simply because it can't guess what you
> really want to do and thus merely records the result of each action.
> Using Ranges instead is almost always simpler, faster, and more reliable.
> With bookmarks using Ranges is even easier than usual because each bookmark
> has a range already defined, so you don't need the usual 'Set' statement
> to define your own.

> Second, the .DefaultSorting property affects only the way bookmarks are
> displayed in the Insert-> Bookmarks dialog.  And that's irrelevant to the
> steps you'll use (even if you use the Selection object).

> Hope this helps.


> -- See the MVP FAQ at http://www.mvps.org/word ---------------------------
> ------------------ "Life is nothing if you're not obsessed." --John Waters
> --------------------------------------------------------------------------
> Reply to group ONLY. No attachments please. MVPs do not work for Microsoft.


> > This is superb information Mark, sounds like just what I need. One quick
> > question if I may? I have bookmarks in doc2 which are the points at
> > which I want to paste\reference the entered field information from doc1.
> > This was my original code for doing this in the same document (sorry I
> > missed the first line out in my initial message)

> > Selection.GoTo What:=wdGoToBookmark, Name:="bookmark1"
> >      With ActiveDocument.Bookmarks
> >          .DefaultSorting = wdSortByName
> >          .ShowHidden = False
> >      End With
> > Selection.TypeText Text:=ActiveDocument.FormFields("text1").Result

> > How do I move to the relevant bookmark in doc2 using whilst keeping your
> > code? what does the 'doc2.Range.InsertAfter' code refer to?

> > Thanks once more.

> > Carl.


> > > Hi Carl,

> > > Sounds like this is a protected form, not a userform.   (Userform is
> > > Microsoft's term for a custom dialog box, a graphical pop-up that you
> > > create in the Visual Basic Editor.)  At any rate:

> > > The short answer is: object variables.

> > >     Dim doc1 as Document, doc2 as Document
> > >     ' Assuming doc1 is open & active now:
> > >     Set doc1 = ActiveDocument
> > >     ' Assign new throwaway doc to second object variable
> > >     Set doc2 = Documents.Add

> > > Now you can refer to each of them by their variable name and transfer
> > > your field results like this [untested]:

> > >     doc2.Range.InsertAfter doc1.FormFields("Text1").Result & vbCr
> > >     doc2.Range.InsertAfter doc1.FormFields("Text2").Result & vbCr
> > >     ' etc.

> > >     doc2.PrintOut Background:=False
> > >     doc2.Close SaveChanges:=wdDoNotSaveChanges

> > > Note: Since you probably want to run this code from doc1 and since doc1
> > > is protected, you should probably run it as an Exit Macro or Entry Macro
> > > in that form.

> > > Hope this helps.


> > > -- See the MVP FAQ at http://www.mvps.org/word ---------------------------
> > > ------------------ "Life is nothing if you're not obsessed." --John Waters
> > > --------------------------------------------------------------------------
> > > Reply to group ONLY. No attachments please. MVPs do not work for Microsoft.


> > >>Hi there,

> > >>Bit of a long and complicated one this so bare with my while I try to
> > >>explain.

> > >>I have a document (doc1 say) that has bookmarked form fields for user
> > >>input. What I would like to produce is a macro (or some procedure) that
> > >>opens a new document (doc2 say), inserts a letter template, copies and
> > >>pastes some values from the fields in doc1, opens a print dialogue box
> > >>that asks the user how many copies of doc2 they would like to print,
> > >>prints the document the requested number of times then closes the
> > >>document without saving.

> > >>Basically I want to remove any user input for doc2 other than asking how
> > >>many copies are required for print, as all the information for the
> > >>letter is contained in fields in doc1. I do not need to save doc2, I
> > >>just need to be able to compile and print letter on the fly.

> > >>The first problem that I have hit is referencing the fields in doc1 from
> > >>doc2.When I have copied text from fields in the past I have done so in
> > >>the same document using something like the following code;

> > >>With ActiveDocument.Bookmarks
> > >>         .DefaultSorting = wdSortByName
> > >>         .ShowHidden = False
> > >>End With
> > >>Selection.TypeText Text:=ActiveDocument.FormFields("field1").Result

> > >>Is there a way that I can say 'look in any document' rather than just
> > >>the active document??

> > >>And is the rest of the automation that I would like possible??

> > >>Hope this makes sense.

> > >>Carl.



Sat, 31 Jul 2004 02:58:47 GMT  
 Reference form fields between separate documents?
That's great Mark, thanks again for taking the time to reply. I simply
wouldn't be able to tackle this kind of work without the help of you and
others.

You're spot on with what you said also. Quite a lot of my code is gained
by recording keystrokes and mouse clicks as I simply don't have the VB
experience to code from scratch.

Guess I owe you a drink?

Carl.

Quote:
> The doc2.Range.InsertAfter assumed you just wanted to paste the values into
> an *empty* doc2.  (It basically says, insert the values after the range of
> the entire document.)  To do what you want, substitute these lines instead:

>   doc2.Bookmarks("Aaaa").Range.InsertAfter doc1.FormFields("Text1").Result
>   doc2.Bookmarks("Bbbb").Range.InsertAfter doc1.FormFields("Text1").Result
>   ' etc.

> where Aaaa, Bbbb, etc., are the names of the bookmarks in doc2.

> Two things are wrong with your code below, and one involves an important
> concept.  Selection.GoTo is the least efficient way to address bookmarked
> locations.  The macro recorder always uses the Selection object in the
> code it writes on your behalf, simply because it can't guess what you
> really want to do and thus merely records the result of each action.  
> Using Ranges instead is almost always simpler, faster, and more reliable.
> With bookmarks using Ranges is even easier than usual because each bookmark
> has a range already defined, so you don't need the usual 'Set' statement
> to define your own.

> Second, the .DefaultSorting property affects only the way bookmarks are
> displayed in the Insert-> Bookmarks dialog.  And that's irrelevant to the
> steps you'll use (even if you use the Selection object).

> Hope this helps.



Sat, 31 Jul 2004 03:49:20 GMT  
 Reference form fields between separate documents?

Quote:

> That's great Mark, thanks again for taking the time to reply. I simply
> wouldn't be able to tackle this kind of work without the help of you and
> others.

> You're spot on with what you said also. Quite a lot of my code is gained
> by recording keystrokes and mouse clicks as I simply don't have the VB
> experience to code from scratch.

You will.  Like I said, it takes time to click, but once it does, coding
is suddenly a lot easier.

Quote:
> Guess I owe you a drink?

Keoke Coffee, no cream.  ;)
Quote:
> Carl.

> > The doc2.Range.InsertAfter assumed you just wanted to paste the values into
> > an *empty* doc2.  (It basically says, insert the values after the range of
> > the entire document.)  To do what you want, substitute these lines instead:

> >   doc2.Bookmarks("Aaaa").Range.InsertAfter doc1.FormFields("Text1").Result
> >   doc2.Bookmarks("Bbbb").Range.InsertAfter doc1.FormFields("Text1").Result
> >   ' etc.

> > where Aaaa, Bbbb, etc., are the names of the bookmarks in doc2.

> > Two things are wrong with your code below, and one involves an important
> > concept.  Selection.GoTo is the least efficient way to address bookmarked
> > locations.  The macro recorder always uses the Selection object in the
> > code it writes on your behalf, simply because it can't guess what you
> > really want to do and thus merely records the result of each action.
> > Using Ranges instead is almost always simpler, faster, and more reliable.
> > With bookmarks using Ranges is even easier than usual because each bookmark
> > has a range already defined, so you don't need the usual 'Set' statement
> > to define your own.

> > Second, the .DefaultSorting property affects only the way bookmarks are
> > displayed in the Insert-> Bookmarks dialog.  And that's irrelevant to the
> > steps you'll use (even if you use the Selection object).

> > Hope this helps.



Sat, 31 Jul 2004 03:55:59 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Using Visual Basic to cross-reference page numbers in separate documents

2. separating pages into separate documents

3. displaying values of fields to separate forms

4. Problem when unprotecting a document with form field text fields

5. How to split a name field into separate fields

6. Using one fill-in field as a bookmark reference for rest of document

7. WebBrowser Document object HTML form references question.

8. Make a reference to a MDI form with a reference of a MDI child form

9. File Copying with a form field reference

10. Referencing a field on a continuous view form

11. mailmerging to separate documents

12. Form field reference

 

 
Powered by phpBB® Forum Software