Transferring data between documents using VB or WB

A

Accounts

Hi,
I want to create a toolbar button which opens a new document using a
template, and then copies in data from fields from the previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it creates a new
document?

Or is there a new for VB to highlight, and then copy into the clipboard
the information stored at a particular bookmark in a document, so that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are inputted via a
form from the original document, and then populate this document at the
bookmarks.
The button I want will then open the label template, and pop in the data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 
D

Doug Robbins - Word MVP

Hi Accounts,

No, VB does not loose the data stored in variables in a document when it
creates a new document, but you have to refer to the location of the
variables as is done in the following code which defines source as the
activedocument, then creates a variable in it, then opens a new document and
inserts into it, the value of the variable that was created in the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the WordBasic bits as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
A

Accounts

Thanks for all of that.
Two questions for you:
Is it possible to specify the template name when creating the new document in
the line Documents.Add?
And by [DocumentObject] I assume you are referring to the variable name given to
the new document in the line Set Target =, so that Target is the
[DocuemtnObject]?


Hi Accounts,

No, VB does not loose the data stored in variables in a document when it
creates a new document, but you have to refer to the location of the
variables as is done in the following code which defines source as the
activedocument, then creates a variable in it, then opens a new document and
inserts into it, the value of the variable that was created in the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the WordBasic bits as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Hi,
I want to create a toolbar button which opens a new document using a
template, and then copies in data from fields from the previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it creates a new
document?

Or is there a new for VB to highlight, and then copy into the clipboard
the information stored at a particular bookmark in a document, so that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are inputted via a
form from the original document, and then populate this document at the
bookmarks.
The button I want will then open the label template, and pop in the data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 
D

Doug Robbins - Word MVP

Hi Accounts,

Use:

Set Target = Documents.Add(Template:="label.dot")

and then

Target.Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Thanks for all of that.
Two questions for you:
Is it possible to specify the template name when creating the new document in
the line Documents.Add?
And by [DocumentObject] I assume you are referring to the variable name given to
the new document in the line Set Target =, so that Target is the
[DocuemtnObject]?


Hi Accounts,

No, VB does not loose the data stored in variables in a document when it
creates a new document, but you have to refer to the location of the
variables as is done in the following code which defines source as the
activedocument, then creates a variable in it, then opens a new document and
inserts into it, the value of the variable that was created in the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the WordBasic bits as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Hi,
I want to create a toolbar button which opens a new document using a
template, and then copies in data from fields from the previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it creates a new
document?

Or is there a new for VB to highlight, and then copy into the clipboard
the information stored at a particular bookmark in a document, so that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are inputted via a
form from the original document, and then populate this document at the
bookmarks.
The button I want will then open the label template, and pop in the data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 
A

Accounts

Hi,
Thanks again for all of this.

When I run this, I get an error message "Object has been deleted".

This is code I have in there
Dim Source As Document, Target As Document

Set Source = ActiveDocument

Set Target = Documents.Add(Template:="label.dot")

Target.Bookmarks("Name").Range.InsertBefore Source.Variables("To_$").Value

Do I need to re-assign the variables To_$ etc to a new variable in the Target
document, or before creating the Target document?


Hi Accounts,

Use:

Set Target = Documents.Add(Template:="label.dot")

and then

Target.Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Thanks for all of that.
Two questions for you:
Is it possible to specify the template name when creating the new document in
the line Documents.Add?
And by [DocumentObject] I assume you are referring to the variable name given to
the new document in the line Set Target =, so that Target is the
[DocuemtnObject]?


Hi Accounts,

No, VB does not loose the data stored in variables in a document when it
creates a new document, but you have to refer to the location of the
variables as is done in the following code which defines source as the
activedocument, then creates a variable in it, then opens a new document and
inserts into it, the value of the variable that was created in the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the WordBasic bits as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
I want to create a toolbar button which opens a new document using a
template, and then copies in data from fields from the previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it creates a new
document?

Or is there a new for VB to highlight, and then copy into the clipboard
the information stored at a particular bookmark in a document, so that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are inputted via a
form from the original document, and then populate this document at the
bookmarks.
The button I want will then open the label template, and pop in the data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 
A

Accounts

Hi,
The variable "To_$" is being assigned in the original document (Source), from
information filled in by the user in a form.
The line from that file is
To_$ = Ddlg.Name

I've tried creating new variables, assigning the values from the original
document to these, then creating the new document based on the label.dot
template, but I still get the error "Object has been deleted"
These are the lines to do this
Set Source = ActiveDocument
Dim To_1$
Dim address1$
Dim Attn1$

To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

Thanks

Hi Accounts,

I don't see in the cod where you have created the variables in Source;

You need to use:

Dim Source As Document, Target As Document

Set Source = ActiveDocument
Source.Variables("To_$").Value = "whatever it is"
Set Target = Documents.Add(Template:="label.dot")
Target.Bookmarks("Name").Range.InsertBefore
Source.Variables("To_$").Value

That's also assuming that the bookmark "Name" exists in label.dot

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Hi,
Thanks again for all of this.

When I run this, I get an error message "Object has been deleted".

This is code I have in there
Dim Source As Document, Target As Document

Set Source = ActiveDocument

Set Target = Documents.Add(Template:="label.dot")

Target.Bookmarks("Name").Range.InsertBefore Source.Variables("To_$").Value

Do I need to re-assign the variables To_$ etc to a new variable in the Target
document, or before creating the Target document?


Hi Accounts,

Use:

Set Target = Documents.Add(Template:="label.dot")

and then

Target.Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Thanks for all of that.
Two questions for you:
Is it possible to specify the template name when creating the new document
in
the line Documents.Add?
And by [DocumentObject] I assume you are referring to the variable name
given to
the new document in the line Set Target =, so that Target is the
[DocuemtnObject]?



Doug Robbins - Word MVP wrote:

Hi Accounts,

No, VB does not loose the data stored in variables in a document when it
creates a new document, but you have to refer to the location of the
variables as is done in the following code which defines source as the
activedocument, then creates a variable in it, then opens a new document
and
inserts into it, the value of the variable that was created in the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the WordBasic bits
as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
I want to create a toolbar button which opens a new document using a
template, and then copies in data from fields from the previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it creates a new
document?

Or is there a new for VB to highlight, and then copy into the
clipboard
the information stored at a particular bookmark in a document, so that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are inputted via a
form from the original document, and then populate this document at
the
bookmarks.
The button I want will then open the label template, and pop in the
data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 
D

Doug Robbins - Word MVP

Hi Accounts,

If these are the lines of code that are assigning the values to the
variables, they are back to front
To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

use

Source.Variables("To_$").Value = To_1$

or

Source.Variables("To_$").Value = Ddlg.Name

I would be re-writing the whole thing to get rid of all of the Word.Basic
hangovers. That is, replace the userdialog with a userform.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Hi,
The variable "To_$" is being assigned in the original document (Source), from
information filled in by the user in a form.
The line from that file is
To_$ = Ddlg.Name

I've tried creating new variables, assigning the values from the original
document to these, then creating the new document based on the label.dot
template, but I still get the error "Object has been deleted"
These are the lines to do this
Set Source = ActiveDocument
Dim To_1$
Dim address1$
Dim Attn1$

To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

Thanks

Hi Accounts,

I don't see in the cod where you have created the variables in Source;

You need to use:

Dim Source As Document, Target As Document

Set Source = ActiveDocument
Source.Variables("To_$").Value = "whatever it is"
Set Target = Documents.Add(Template:="label.dot")
Target.Bookmarks("Name").Range.InsertBefore
Source.Variables("To_$").Value

That's also assuming that the bookmark "Name" exists in label.dot

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Hi,
Thanks again for all of this.

When I run this, I get an error message "Object has been deleted".

This is code I have in there
Dim Source As Document, Target As Document

Set Source = ActiveDocument

Set Target = Documents.Add(Template:="label.dot")

Target.Bookmarks("Name").Range.InsertBefore Source.Variables("To_$").Value

Do I need to re-assign the variables To_$ etc to a new variable in the Target
document, or before creating the Target document?



Doug Robbins - Word MVP wrote:

Hi Accounts,

Use:

Set Target = Documents.Add(Template:="label.dot")

and then

Target.Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Thanks for all of that.
Two questions for you:
Is it possible to specify the template name when creating the new document
in
the line Documents.Add?
And by [DocumentObject] I assume you are referring to the variable name
given to
the new document in the line Set Target =, so that Target is the
[DocuemtnObject]?



Doug Robbins - Word MVP wrote:

Hi Accounts,

No, VB does not loose the data stored in variables in a document when it
creates a new document, but you have to refer to the location of the
variables as is done in the following code which defines source
as
the
activedocument, then creates a variable in it, then opens a new document
and
inserts into it, the value of the variable that was created in the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the
WordBasic
bits
as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups
for
the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
I want to create a toolbar button which opens a new document
using
a
template, and then copies in data from fields from the previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it creates
a
new
document?

Or is there a new for VB to highlight, and then copy into the
clipboard
the information stored at a particular bookmark in a document,
so
that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are
inputted
via a
form from the original document, and then populate this
document
at
the
bookmarks.
The button I want will then open the label template, and pop
in
the
data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 
A

Accounts

Doug,
Thanks.

The lines I have are in the macro for the button when it is pressed.
The user fills in the form, and the form has assigned the variables To_$, Attn$,
and address$. These then populate the document at certain bookmarks.
When the button is pressed, I wanted the data in the variables to be copied over
to some new variables in the macro for the button (To_1$, Attn1$ and address1$),
and these then populate the template at its bookmarks.

It looks like the macro in the original document, the one with the form, loses
all of its data once it has populated the document, so that when the button is
pressed, and the new macro runs, there is nothing in the To_$ etc. variables.

I've tried using the line
Source.Variables("To_$").Value = Ddlg.Name
in the macro for the button, but again there is no data for it to pass into the
variable.


Hi Accounts,

If these are the lines of code that are assigning the values to the
variables, they are back to front
To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

use

Source.Variables("To_$").Value = To_1$

or

Source.Variables("To_$").Value = Ddlg.Name

I would be re-writing the whole thing to get rid of all of the Word.Basic
hangovers. That is, replace the userdialog with a userform.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Hi,
The variable "To_$" is being assigned in the original document (Source), from
information filled in by the user in a form.
The line from that file is
To_$ = Ddlg.Name

I've tried creating new variables, assigning the values from the original
document to these, then creating the new document based on the label.dot
template, but I still get the error "Object has been deleted"
These are the lines to do this
Set Source = ActiveDocument
Dim To_1$
Dim address1$
Dim Attn1$

To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

Thanks

Hi Accounts,

I don't see in the cod where you have created the variables in Source;

You need to use:

Dim Source As Document, Target As Document

Set Source = ActiveDocument
Source.Variables("To_$").Value = "whatever it is"
Set Target = Documents.Add(Template:="label.dot")
Target.Bookmarks("Name").Range.InsertBefore
Source.Variables("To_$").Value

That's also assuming that the bookmark "Name" exists in label.dot

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
Thanks again for all of this.

When I run this, I get an error message "Object has been deleted".

This is code I have in there
Dim Source As Document, Target As Document

Set Source = ActiveDocument

Set Target = Documents.Add(Template:="label.dot")

Target.Bookmarks("Name").Range.InsertBefore
Source.Variables("To_$").Value

Do I need to re-assign the variables To_$ etc to a new variable in the
Target
document, or before creating the Target document?



Doug Robbins - Word MVP wrote:

Hi Accounts,

Use:

Set Target = Documents.Add(Template:="label.dot")

and then

Target.Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Thanks for all of that.
Two questions for you:
Is it possible to specify the template name when creating the new
document
in
the line Documents.Add?
And by [DocumentObject] I assume you are referring to the variable
name
given to
the new document in the line Set Target =, so that Target is the
[DocuemtnObject]?



Doug Robbins - Word MVP wrote:

Hi Accounts,

No, VB does not loose the data stored in variables in a document
when it
creates a new document, but you have to refer to the location of the
variables as is done in the following code which defines source as
the
activedocument, then creates a variable in it, then opens a new
document
and
inserts into it, the value of the variable that was created in the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the WordBasic
bits
as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for
the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
I want to create a toolbar button which opens a new document using
a
template, and then copies in data from fields from the previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it creates a
new
document?

Or is there a new for VB to highlight, and then copy into the
clipboard
the information stored at a particular bookmark in a document, so
that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are inputted
via a
form from the original document, and then populate this document
at
the
bookmarks.
The button I want will then open the label template, and pop in
the
data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 
D

Doug Robbins - Word MVP

I am sorry, I really do not understand what you are saying. If you want to
send the template(s) to me I will take a look at it/them for you

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Doug,
Thanks.

The lines I have are in the macro for the button when it is pressed.
The user fills in the form, and the form has assigned the variables To_$, Attn$,
and address$. These then populate the document at certain bookmarks.
When the button is pressed, I wanted the data in the variables to be copied over
to some new variables in the macro for the button (To_1$, Attn1$ and address1$),
and these then populate the template at its bookmarks.

It looks like the macro in the original document, the one with the form, loses
all of its data once it has populated the document, so that when the button is
pressed, and the new macro runs, there is nothing in the To_$ etc. variables.

I've tried using the line
Source.Variables("To_$").Value = Ddlg.Name
in the macro for the button, but again there is no data for it to pass into the
variable.


Hi Accounts,

If these are the lines of code that are assigning the values to the
variables, they are back to front
To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

use

Source.Variables("To_$").Value = To_1$

or

Source.Variables("To_$").Value = Ddlg.Name

I would be re-writing the whole thing to get rid of all of the Word.Basic
hangovers. That is, replace the userdialog with a userform.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Hi,
The variable "To_$" is being assigned in the original document
(Source),
from
information filled in by the user in a form.
The line from that file is
To_$ = Ddlg.Name

I've tried creating new variables, assigning the values from the original
document to these, then creating the new document based on the label.dot
template, but I still get the error "Object has been deleted"
These are the lines to do this
Set Source = ActiveDocument
Dim To_1$
Dim address1$
Dim Attn1$

To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

Thanks


Doug Robbins - Word MVP wrote:

Hi Accounts,

I don't see in the cod where you have created the variables in Source;

You need to use:

Dim Source As Document, Target As Document

Set Source = ActiveDocument
Source.Variables("To_$").Value = "whatever it is"
Set Target = Documents.Add(Template:="label.dot")
Target.Bookmarks("Name").Range.InsertBefore
Source.Variables("To_$").Value

That's also assuming that the bookmark "Name" exists in label.dot

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
Thanks again for all of this.

When I run this, I get an error message "Object has been deleted".

This is code I have in there
Dim Source As Document, Target As Document

Set Source = ActiveDocument

Set Target = Documents.Add(Template:="label.dot")

Target.Bookmarks("Name").Range.InsertBefore
Source.Variables("To_$").Value

Do I need to re-assign the variables To_$ etc to a new variable in the
Target
document, or before creating the Target document?



Doug Robbins - Word MVP wrote:

Hi Accounts,

Use:

Set Target = Documents.Add(Template:="label.dot")

and then

Target.Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups
for
the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Thanks for all of that.
Two questions for you:
Is it possible to specify the template name when creating the new
document
in
the line Documents.Add?
And by [DocumentObject] I assume you are referring to the variable
name
given to
the new document in the line Set Target =, so that Target is the
[DocuemtnObject]?



Doug Robbins - Word MVP wrote:

Hi Accounts,

No, VB does not loose the data stored in variables in a document
when it
creates a new document, but you have to refer to the
location of
the
variables as is done in the following code which defines
source
as
the
activedocument, then creates a variable in it, then opens a new
document
and
inserts into it, the value of the variable that was created
in
the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the WordBasic
bits
as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the
newsgroups
for
the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
I want to create a toolbar button which opens a new
document
using
a
template, and then copies in data from fields from the previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it
creates
a
new
document?

Or is there a new for VB to highlight, and then copy into the
clipboard
the information stored at a particular bookmark in a
document,
so
that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are inputted
via a
form from the original document, and then populate this document
at
the
bookmarks.
The button I want will then open the label template, and
pop
in
the
data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 
A

Accounts

What about passing the variables to the template when it is created?
Is that possible?
That way it has a local copy of the text to be added.
I am sorry, I really do not understand what you are saying. If you want to
send the template(s) to me I will take a look at it/them for you

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Accounts said:
Doug,
Thanks.

The lines I have are in the macro for the button when it is pressed.
The user fills in the form, and the form has assigned the variables To_$, Attn$,
and address$. These then populate the document at certain bookmarks.
When the button is pressed, I wanted the data in the variables to be copied over
to some new variables in the macro for the button (To_1$, Attn1$ and address1$),
and these then populate the template at its bookmarks.

It looks like the macro in the original document, the one with the form, loses
all of its data once it has populated the document, so that when the button is
pressed, and the new macro runs, there is nothing in the To_$ etc. variables.

I've tried using the line
Source.Variables("To_$").Value = Ddlg.Name
in the macro for the button, but again there is no data for it to pass into the
variable.


Hi Accounts,

If these are the lines of code that are assigning the values to the
variables, they are back to front

To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

use

Source.Variables("To_$").Value = To_1$

or

Source.Variables("To_$").Value = Ddlg.Name

I would be re-writing the whole thing to get rid of all of the Word.Basic
hangovers. That is, replace the userdialog with a userform.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
The variable "To_$" is being assigned in the original document (Source),
from
information filled in by the user in a form.
The line from that file is
To_$ = Ddlg.Name

I've tried creating new variables, assigning the values from the original
document to these, then creating the new document based on the label.dot
template, but I still get the error "Object has been deleted"
These are the lines to do this
Set Source = ActiveDocument
Dim To_1$
Dim address1$
Dim Attn1$

To_1$ = Source.Variables("To_$")
Attn1$ = Source.Variables("Attn$")
address1$ = Source.Variables("address$")

Thanks


Doug Robbins - Word MVP wrote:

Hi Accounts,

I don't see in the cod where you have created the variables in Source;

You need to use:

Dim Source As Document, Target As Document

Set Source = ActiveDocument
Source.Variables("To_$").Value = "whatever it is"
Set Target = Documents.Add(Template:="label.dot")
Target.Bookmarks("Name").Range.InsertBefore
Source.Variables("To_$").Value

That's also assuming that the bookmark "Name" exists in label.dot

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
Thanks again for all of this.

When I run this, I get an error message "Object has been deleted".

This is code I have in there
Dim Source As Document, Target As Document

Set Source = ActiveDocument

Set Target = Documents.Add(Template:="label.dot")

Target.Bookmarks("Name").Range.InsertBefore
Source.Variables("To_$").Value

Do I need to re-assign the variables To_$ etc to a new variable in the
Target
document, or before creating the Target document?



Doug Robbins - Word MVP wrote:

Hi Accounts,

Use:

Set Target = Documents.Add(Template:="label.dot")

and then

Target.Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups for
the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Thanks for all of that.
Two questions for you:
Is it possible to specify the template name when creating the new
document
in
the line Documents.Add?
And by [DocumentObject] I assume you are referring to the variable
name
given to
the new document in the line Set Target =, so that Target is the
[DocuemtnObject]?



Doug Robbins - Word MVP wrote:

Hi Accounts,

No, VB does not loose the data stored in variables in a document
when it
creates a new document, but you have to refer to the location of
the
variables as is done in the following code which defines source
as
the
activedocument, then creates a variable in it, then opens a new
document
and
inserts into it, the value of the variable that was created in
the
document.Source

Dim Source As Document, Target As Document
Set Source = ActiveDocument
Source.Variables("Test").Value = "This is a test."
Set Target = Documents.Add
Target.Range.InsertBefore Source.Variables("test").Value

Your code will run a lot quicker if you dispense with the
WordBasic
bits
as
in

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$

and use

[DocumentObject].Bookmarks("Name").Range.InsertBefore

Please post any further questions or followup to the newsgroups
for
the
benefit of others who may be interested. Unsolicited questions
forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
Hi,
I want to create a toolbar button which opens a new document
using
a
template, and then copies in data from fields from the
previous
document.
When I try it however, non of the data is being brought over.
Does VB lose all the data stored in variables when it creates
a
new
document?

Or is there a new for VB to highlight, and then copy into the
clipboard
the information stored at a particular bookmark in a document,
so
that
it can then paste it in the new dowcument at another bookmark?

This is the code I have already.
Documents.Add Template:="label.dot"

WordBasic.WW7_EditGoTo Destination:="Name"
WordBasic.Insert To_$
If Len(Attn$) > 1 Then
WordBasic.WW7_EditGoTo Destination:="Att"
WordBasic.Insert "Attention: " + Attn$
End If
WordBasic.WW7_EditGoTo Destination:="Address"
WordBasic.Insert address$

The data in the To_$, Attn$ and address$ variables are
inputted
via a
form from the original document, and then populate this
document
at
the
bookmarks.
The button I want will then open the label template, and pop
in
the
data
from the original document.

Its all WB code, but VB code would be nicer.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top