Challenging problem... Please help if you can

J

Jeffery B Paarsa

In a VB script I am capturing couple of fields from the Form Screen and I
would like to populate the captured fields from the Form screen into a MS
Word template that I have created myself into a Text field in the Template
called PName.

I am done up to the point that I start the Word in either visible or
invisible mode but I have not been able to pass the captured field into the
Template Text field called PName and then print it. I have looked at a few
examples on MSDN but none would come close enough to what I planning to do.
 
D

Doug Robbins - Word MVP

Your description of what you are trying to do seems a bit jumbled to me -
maybe the terms that you are using, but what I would do is have the code
create document variables and in the template I would insert DOCVARIABLE
fields. The code in the template would also include commands to update the
fields in the document that is created from the template so that the
DOCVARIABLE fields display the information that was stored in the variables.

To create the variables use

With ActiveDocuments
.Variables("[varname]").Value = [probably the name of a control on your
form.Text]
'etc for more variables
.Fields.Update
End With

In the template, you would have a { DOCVARIABLE [varname] } field

Note the square brackets and what's inside them need to be replaced with the
actual items that you are using.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jeffery B Paarsa

I am sorry I am rather Rookie on Word programming etc... But I have to
confess that your advice as a Word MVP completely threw me off track of my
thoughts... I Had never heard about the DOCVARIABLE etc...

Yes I should have said that I have a Word Template full of Text Fields that
all of these Text Fields have a unique Bookmark id showing on their "Text
Form Field Options" dialog box. I would like to populate some of these
bookmarks “Text Fields on the Word Template†from the data extracted from the
VB Form windows after starting the Word using my Template id then print.

I hope I did not confuse you this time..

Jeff B Paarsa


Doug Robbins - Word MVP said:
Your description of what you are trying to do seems a bit jumbled to me -
maybe the terms that you are using, but what I would do is have the code
create document variables and in the template I would insert DOCVARIABLE
fields. The code in the template would also include commands to update the
fields in the document that is created from the template so that the
DOCVARIABLE fields display the information that was stored in the variables.

To create the variables use

With ActiveDocuments
.Variables("[varname]").Value = [probably the name of a control on your
form.Text]
'etc for more variables
.Fields.Update
End With

In the template, you would have a { DOCVARIABLE [varname] } field

Note the square brackets and what's inside them need to be replaced with the
actual items that you are using.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Jeffery B Paarsa said:
In a VB script I am capturing couple of fields from the Form Screen and I
would like to populate the captured fields from the Form screen into a MS
Word template that I have created myself into a Text field in the Template
called PName.

I am done up to the point that I start the Word in either visible or
invisible mode but I have not been able to pass the captured field into
the
Template Text field called PName and then print it. I have looked at a few
examples on MSDN but none would come close enough to what I planning to
do.
 
D

Doug Robbins - Word MVP

To insert something into a Text FormField, you use the .Result attribute of
the formfield

ActiveDocument.FormFields("[name of bookmark assigned to the
FormField]").Result = [some data]

But, unless you want the document to be protected from some other reason, I
would use document variables as previously mentioned.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Jeffery B Paarsa said:
I am sorry I am rather Rookie on Word programming etc... But I have to
confess that your advice as a Word MVP completely threw me off track of my
thoughts... I Had never heard about the DOCVARIABLE etc...

Yes I should have said that I have a Word Template full of Text Fields
that
all of these Text Fields have a unique Bookmark id showing on their "Text
Form Field Options" dialog box. I would like to populate some of these
bookmarks "Text Fields on the Word Template" from the data extracted from
the
VB Form windows after starting the Word using my Template id then print.

I hope I did not confuse you this time..

Jeff B Paarsa


Doug Robbins - Word MVP said:
Your description of what you are trying to do seems a bit jumbled to me -
maybe the terms that you are using, but what I would do is have the code
create document variables and in the template I would insert DOCVARIABLE
fields. The code in the template would also include commands to update
the
fields in the document that is created from the template so that the
DOCVARIABLE fields display the information that was stored in the
variables.

To create the variables use

With ActiveDocuments
.Variables("[varname]").Value = [probably the name of a control on
your
form.Text]
'etc for more variables
.Fields.Update
End With

In the template, you would have a { DOCVARIABLE [varname] } field

Note the square brackets and what's inside them need to be replaced with
the
actual items that you are using.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Jeffery B Paarsa said:
In a VB script I am capturing couple of fields from the Form Screen and
I
would like to populate the captured fields from the Form screen into a
MS
Word template that I have created myself into a Text field in the
Template
called PName.

I am done up to the point that I start the Word in either visible or
invisible mode but I have not been able to pass the captured field into
the
Template Text field called PName and then print it. I have looked at a
few
examples on MSDN but none would come close enough to what I planning to
do.
 
J

Jeffery B Paarsa

Doug Thank you for your advice. I really don't want to be rude and ask you
to solve my problem. To read more about VBA Word programming I have ordered
two new books but I have been trying to get my code working by using your
advice and I have been failed so far. I am using Office 2003 but some how I
can not resolve the namespace for ActiveDocument. Here is the code I have
problem with...

Option Explicit On
Public Class frmPVisit
Dim WithEvents oApp As Microsoft.Office.Interop.Word.Application
Dim oWord As Object
Dim oTmpDoc As Object
Dim oMainDoc As Microsoft.Office.Interop.Word.Document
Dim oSel As Microsoft.Office.Interop.Word.Selection
Dim oFields As Microsoft.Office.Interop.Word.MailMergeFields
Dim wdApp1 As Microsoft.Office.Interop.Word.Application
Private Sub Print_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Print.Click
Dim myPatient As New Patient()
Dim LFName As String = ""
Dim Documents As String = ""
Dim rngTemp As Range
LFName = Me.TxtPLname.Text & ", " & Me.TxtPFName.Text
oWord = CreateObject("Word.Application")
ActiveDocument.FormFields("PName").Result = LFName
“ I am getting Name ActiveDocument is not Declared on it… with a blue
squigly line under it
oTmpDoc = oWord.Documents.Add("N:\Maryam doc\(ALL)MR History &
Physical.dot")
oWord.Visible = True
Me.TxtPLname.Text = ""
Me.TxtPFName.Text = ""
End Sub
End Class

--
Jeff B Paarsa


Doug Robbins - Word MVP said:
To insert something into a Text FormField, you use the .Result attribute of
the formfield

ActiveDocument.FormFields("[name of bookmark assigned to the
FormField]").Result = [some data]

But, unless you want the document to be protected from some other reason, I
would use document variables as previously mentioned.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Jeffery B Paarsa said:
I am sorry I am rather Rookie on Word programming etc... But I have to
confess that your advice as a Word MVP completely threw me off track of my
thoughts... I Had never heard about the DOCVARIABLE etc...

Yes I should have said that I have a Word Template full of Text Fields
that
all of these Text Fields have a unique Bookmark id showing on their "Text
Form Field Options" dialog box. I would like to populate some of these
bookmarks "Text Fields on the Word Template" from the data extracted from
the
VB Form windows after starting the Word using my Template id then print.

I hope I did not confuse you this time..

Jeff B Paarsa


Doug Robbins - Word MVP said:
Your description of what you are trying to do seems a bit jumbled to me -
maybe the terms that you are using, but what I would do is have the code
create document variables and in the template I would insert DOCVARIABLE
fields. The code in the template would also include commands to update
the
fields in the document that is created from the template so that the
DOCVARIABLE fields display the information that was stored in the
variables.

To create the variables use

With ActiveDocuments
.Variables("[varname]").Value = [probably the name of a control on
your
form.Text]
'etc for more variables
.Fields.Update
End With

In the template, you would have a { DOCVARIABLE [varname] } field

Note the square brackets and what's inside them need to be replaced with
the
actual items that you are using.
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

In a VB script I am capturing couple of fields from the Form Screen and
I
would like to populate the captured fields from the Form screen into a
MS
Word template that I have created myself into a Text field in the
Template
called PName.

I am done up to the point that I start the Word in either visible or
invisible mode but I have not been able to pass the captured field into
the
Template Text field called PName and then print it. I have looked at a
few
examples on MSDN but none would come close enough to what I planning to
do.
 
J

Jonathan West

Hi Jeffrey

You omitted one important fact from your earlier description - you are using
VB.NET not VBA. This does make something of a difference - the two languages
differ in ways that can easily trip you up.


Jeffery B Paarsa said:
Doug Thank you for your advice. I really don't want to be rude and ask
you
to solve my problem. To read more about VBA Word programming I have
ordered
two new books but I have been trying to get my code working by using your
advice and I have been failed so far. I am using Office 2003 but some how
I
can not resolve the namespace for ActiveDocument. Here is the code I have
problem with...

Option Explicit On
Public Class frmPVisit
Dim WithEvents oApp As Microsoft.Office.Interop.Word.Application
Dim oWord As Object
Dim oTmpDoc As Object
Dim oMainDoc As Microsoft.Office.Interop.Word.Document
Dim oSel As Microsoft.Office.Interop.Word.Selection
Dim oFields As Microsoft.Office.Interop.Word.MailMergeFields
Dim wdApp1 As Microsoft.Office.Interop.Word.Application

The fact that you have declared these variable in this way suggests that you
haven't imported the Word namespace. That makes a difference when you come
to use objects which (in VBA) are members of the Globals object.
Private Sub Print_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Print.Click
Dim myPatient As New Patient()
Dim LFName As String = ""
Dim Documents As String = ""
Dim rngTemp As Range
LFName = Me.TxtPLname.Text & ", " & Me.TxtPFName.Text
oWord = CreateObject("Word.Application")
ActiveDocument.FormFields("PName").Result = LFName
“ I am getting Name ActiveDocument is not Declared on it… with a blue
squigly line under it

And here is such an occasion. ActiveDocument is a member of Globals and so
in VBA doesn't need to be qualified. here it does - it is also a member of
the Application object, so you should change this line as follows

oWord.ActiveDocument.FormFields("PName").Result = LFName
oTmpDoc = oWord.Documents.Add("N:\Maryam doc\(ALL)MR History &
Physical.dot")
oWord.Visible = True
Me.TxtPLname.Text = ""
Me.TxtPFName.Text = ""
End Sub
End Class

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jeffery B Paarsa

Totally, Yes I am rookie on that aspect. Actually I am waiting for the
shipment of my Visual studio dot net and currently I am using the downloaded
version of Visual Basic Express. So on that part I may be missing a lot. I
thought I could play around with the Express version while waiting for my
shipment of my software and shipment of my books. So tell me please does
downloaded version of Visual Basic Express compatible with VBA or dot net?
 
J

Jeffery B Paarsa

Hi Jonathan,

Here are what I have ordered:
Word 2003 Document Automation with VBA, XML, XSLT, and Smart Documents
(Wordware Applications Library) (Paperback)
by Scott Driza

1 of: Word 2003 Visual Basic Programming [Paperback]
By: John Low

Do you have any other recommendation considering that I will be receiving
Visual Studio dot net 6.0 in next couple of weeks.
 
J

Jonathan West

Jeffery B Paarsa said:
Totally, Yes I am rookie on that aspect. Actually I am waiting for the
shipment of my Visual studio dot net and currently I am using the
downloaded
version of Visual Basic Express. So on that part I may be missing a lot.
I
thought I could play around with the Express version while waiting for my
shipment of my software and shipment of my books. So tell me please does
downloaded version of Visual Basic Express compatible with VBA or dot net?

OK, we need to be clear on this matter of "compatible".

VB6 and VBA are pretty nearly the same language - they have the same syntax
and depend in part on the same runtimes. VB6 can be used to make compiled
executables, while VBA is embedded within Office applications, and VBA code
is included within Office documents.

VB6 and VB.NET are two different languages. Microsoft still likes to claim
that the latter is a newer version of the former, but that claim is baloney.
If you want to read up about some of the history of this, take a look at
www.classicvb.org

The languages *are* similar, sufficiently so that if you forget which one
you are in, you can get confused by the differences in behavior. The
differences can be obvious, such as a Long being a 32-bit signed integer in
VB6/VBA and a 64-bit signed integer in VB.NET. Others can be more subtle,
such as the way object destruction is handled. Therefore, you can say that
VB6/VBA and VB.NET are not very compatible. Certainly Microsoft has never
managed to create a converter that could reliably convert anything more than
a "Hello World" application from one to the other.

But that, while it is the question you asked, I suspect it isn't quite what
you meant to ask. What you meant was "Is VB.NET compatible with the Word
object model?" And the answer to that is "yes". You can create a
Word.Application object and then use all the objects within the Word object
model fro VB.NET. The *way* in which you use them will have to be subtly
different from time to time. The Word object model is documented in the Word
VBA help files, and so all the code examples and definitions assume the use
of VBA. For instance, if a particular property is shown in the Help file as
being of type Long, you will have to remember that for you in VB.NET it is
actually an Integer.

One further thing. In the Word VBA editor, if you go to Tools References,
you will find that references to a couple of external libraries have been
set. If you open up the VBA editor in Excel, you will find that different
libraries have been checked. This gives you immediate access to the Excel
object model rather than the Word object model - all within the same
language. The VB.NET equivalent of this is importing Namespaces. For the
Word or Excel object models, this has to be done explicitly. I'll leave it
to you to look up in the VB.NET help how to import a namespace. If you
choose not to import the namespace, then this is another thing you need to
be aware of when trying to convert VBA code examples from the newsgroup or
the help file.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Jeffery B Paarsa

Jonathan, Thank you for being through and highly explanatory on this. I
really appreciate that.
 

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