how to hide the ActiveDocument.FormFields

  • Thread starter hemaneelagiri via OfficeKB.com
  • Start date
H

hemaneelagiri via OfficeKB.com

hi
i have word documents in that there are 5 fields(empno,name,address,
designation, salary) which have bookmarks.
i am filling those through VBA and those will fill in loop means suppose
there are 10 records in a table. when i run the applicaltion 10 pages will
display respcetive 5 fields.
till that every thing is fine
but where there is no address for 5th employee i want to hide that addresses
filed in 5th page.
now i ma getting blank space between name and designation

For Each frmfld In ActiveDocument.FormFields
Select Case frmfld.Name
Case "mta072" 'Address
If (strAdd = "" Or strAdd= " ") Then
frmfld.Select
Selection.TypeText " " **********************
Else
frmfld.Select
Selection.TypeText "Address:" & vbTab & strAdd
End If


how can i hide that place

thanks in advance
 
G

Graham Mayor

Assuming you mean that the formfield mta072 is in a line on its own and you
want to hide that line, I guess I would use something like the following. I
have of course added in some lines to make it work as a stand-alone

Dim frmfld As FormField
Dim strAdd As String
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

strAdd = ""
For Each frmfld In ActiveDocument.FormFields
Select Case frmfld.name
Case "mta072" 'Address
If (strAdd = "" Or strAdd = " ") Then
frmfld.Range.Paragraphs(1).Range.Font.Hidden = True
Else
frmfld.Range.Paragraphs(1).Range.Font.Hidden = False
frmfld.Result = "Address:" & vbTab & strAdd
End If
End Select
Next
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

hemaneelagiri via OfficeKB.com

when i have tried with this in all pages that is hideing.. even when data is
there those records also hiding.. can u please suggest me..
what is frmfld.Range.Paragraphs(1). in this 1 means..
and one more thing Y the password code.. is it necessary.. to hide for single
column
thanks




Graham said:
Assuming you mean that the formfield mta072 is in a line on its own and you
want to hide that line, I guess I would use something like the following. I
have of course added in some lines to make it work as a stand-alone

Dim frmfld As FormField
Dim strAdd As String
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

strAdd = ""
For Each frmfld In ActiveDocument.FormFields
Select Case frmfld.name
Case "mta072" 'Address
If (strAdd = "" Or strAdd = " ") Then
frmfld.Range.Paragraphs(1).Range.Font.Hidden = True
Else
frmfld.Range.Paragraphs(1).Range.Font.Hidden = False
frmfld.Result = "Address:" & vbTab & strAdd
End If
End Select
Next
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
hi
i have word documents in that there are 5 fields(empno,name,address,
[quoted text clipped - 22 lines]
thanks in advance
 
G

Graham Mayor

frmfld.Range.Paragraphs(1).Range refers to the paragraph in which the
formfield is located.
Your latest post includes the term 'column'. Do I take it that this form
field is in a table? In which case the macro will only hide the field, and
not the table row containing the field.
You must unlock the form to apply formatting - hence the extra code to do
so.
The macro hides the paragraph containing field mta072 if strAdd is empty or
contains a space
If it contains anything else it writes "Address:" & vbTab & strAdd i.e. the
text Address: followed by a tab and the content of strAdd into the field
mta072.

Are you sure that mail merge would not be more appropriate for this job?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


hemaneelagiri via OfficeKB.com said:
when i have tried with this in all pages that is hideing.. even when data
is
there those records also hiding.. can u please suggest me..
what is frmfld.Range.Paragraphs(1). in this 1 means..
and one more thing Y the password code.. is it necessary.. to hide for
single
column
thanks




Graham said:
Assuming you mean that the formfield mta072 is in a line on its own and
you
want to hide that line, I guess I would use something like the following.
I
have of course added in some lines to make it work as a stand-alone

Dim frmfld As FormField
Dim strAdd As String
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If

strAdd = ""
For Each frmfld In ActiveDocument.FormFields
Select Case frmfld.name
Case "mta072" 'Address
If (strAdd = "" Or strAdd = " ") Then
frmfld.Range.Paragraphs(1).Range.Font.Hidden = True
Else
frmfld.Range.Paragraphs(1).Range.Font.Hidden = False
frmfld.Result = "Address:" & vbTab & strAdd
End If
End Select
Next
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
hi
i have word documents in that there are 5 fields(empno,name,address,
[quoted text clipped - 22 lines]
thanks in advance
 
H

hemaneelagiri via OfficeKB.com

ok but here my code is in loop

please c this


Set mrst = GetEmpDetails(mintRecord_Id)

For intX = 0 To mrst.RecordCount - 1
If intX > 0 Then
Selection.GoTo What:=wdGoToSection, Which:=wdGoToLast
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.InsertBreak Type:=wdSectionBreakContinuous
Selection.InsertFile Filename:=mstrTemplatePath, Range:="mtaLoop",
ConfirmConversions:=False, Link:=False, Attachment:=False

End If

For Each frmfld In ActiveDocument.FormFields
Select Case frmfld.Name
Case "mta072" 'Address
If (strAdd = "" Or strAdd = " ") Then
frmfld.Range.Paragraphs(1).Range.Font.Hidden = True
Else
frmfld.Range.Paragraphs(1).Range.Font.Hidden = False
frmfld.Result = "Address:" & vbTab & strAdd
End If
End Select
Next frmfld


Call AgendaItem(intX)
mrst.MoveNext
Next intX


so when i am giving like above
first record there is no addresse so it is hiding and second record there is
address data even though it is hiding
please help me




Graham said:
frmfld.Range.Paragraphs(1).Range refers to the paragraph in which the
formfield is located.
Your latest post includes the term 'column'. Do I take it that this form
field is in a table? In which case the macro will only hide the field, and
not the table row containing the field.
You must unlock the form to apply formatting - hence the extra code to do
so.
The macro hides the paragraph containing field mta072 if strAdd is empty or
contains a space
If it contains anything else it writes "Address:" & vbTab & strAdd i.e. the
text Address: followed by a tab and the content of strAdd into the field
mta072.

Are you sure that mail merge would not be more appropriate for this job?
when i have tried with this in all pages that is hideing.. even when data
is
[quoted text clipped - 39 lines]
 
G

Graham Mayor

Frankly I don't see how this could work. You are apparently inserting a
section break at the end of the document and then adding another copy of the
same document at the end. The problem with that is that the newly inserted
document will have the same set of field names that have already been used.
Bookmarks must be unique, thus the fields in the added section will not be
named and so they will not be loaded with the data. If you are going to
insert another set of fields then they will need a new set of names to
match.

One alternative (that I haven't tested) would be to convert all the fields
to text in the existing document before inserting the new copy of the
document. If the fields don't exist there should be no conflict with the
bookmark names.

I still feel that this is mail merge and that you are trying to re-invent
the wheel for no good reason that has yet become apparent.

You may be interested in
http://www.gmayor.com/Form_Fields_and_Mail_Merge.htm and there are some
other examples on my web site at http://www.gmayor.com/SelectFile.htm and
http://www.gmayor.com/word_vba_examples.htm that you may find useful in
pointing a way forward if you wish to pursue this roll-your-own mail merge
project.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


hemaneelagiri via OfficeKB.com said:
ok but here my code is in loop

please c this


Set mrst = GetEmpDetails(mintRecord_Id)

For intX = 0 To mrst.RecordCount - 1
If intX > 0 Then
Selection.GoTo What:=wdGoToSection, Which:=wdGoToLast
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.InsertBreak Type:=wdSectionBreakContinuous
Selection.InsertFile Filename:=mstrTemplatePath,
Range:="mtaLoop",
ConfirmConversions:=False, Link:=False, Attachment:=False

End If

For Each frmfld In ActiveDocument.FormFields
Select Case frmfld.Name
Case "mta072" 'Address
If (strAdd = "" Or strAdd = " ") Then
frmfld.Range.Paragraphs(1).Range.Font.Hidden = True
Else
frmfld.Range.Paragraphs(1).Range.Font.Hidden = False
frmfld.Result = "Address:" & vbTab & strAdd
End If
End Select
Next frmfld


Call AgendaItem(intX)
mrst.MoveNext
Next intX


so when i am giving like above
first record there is no addresse so it is hiding and second record there
is
address data even though it is hiding
please help me




Graham said:
frmfld.Range.Paragraphs(1).Range refers to the paragraph in which the
formfield is located.
Your latest post includes the term 'column'. Do I take it that this form
field is in a table? In which case the macro will only hide the field, and
not the table row containing the field.
You must unlock the form to apply formatting - hence the extra code to do
so.
The macro hides the paragraph containing field mta072 if strAdd is empty
or
contains a space
If it contains anything else it writes "Address:" & vbTab & strAdd i.e.
the
text Address: followed by a tab and the content of strAdd into the field
mta072.

Are you sure that mail merge would not be more appropriate for this job?
when i have tried with this in all pages that is hideing.. even when
data
is
[quoted text clipped - 39 lines]
thanks in advance
 
H

hemaneelagiri via OfficeKB.com

Thanks for your reply i will chekc with those sites

Graham said:
Frankly I don't see how this could work. You are apparently inserting a
section break at the end of the document and then adding another copy of the
same document at the end. The problem with that is that the newly inserted
document will have the same set of field names that have already been used.
Bookmarks must be unique, thus the fields in the added section will not be
named and so they will not be loaded with the data. If you are going to
insert another set of fields then they will need a new set of names to
match.

One alternative (that I haven't tested) would be to convert all the fields
to text in the existing document before inserting the new copy of the
document. If the fields don't exist there should be no conflict with the
bookmark names.

I still feel that this is mail merge and that you are trying to re-invent
the wheel for no good reason that has yet become apparent.

You may be interested in
http://www.gmayor.com/Form_Fields_and_Mail_Merge.htm and there are some
other examples on my web site at http://www.gmayor.com/SelectFile.htm and
http://www.gmayor.com/word_vba_examples.htm that you may find useful in
pointing a way forward if you wish to pursue this roll-your-own mail merge
project.
ok but here my code is in loop
[quoted text clipped - 58 lines]
 
H

hemaneelagiri via OfficeKB.com

Thanks for your reply i will chekc with those sites

Graham said:
Frankly I don't see how this could work. You are apparently inserting a
section break at the end of the document and then adding another copy of the
same document at the end. The problem with that is that the newly inserted
document will have the same set of field names that have already been used.
Bookmarks must be unique, thus the fields in the added section will not be
named and so they will not be loaded with the data. If you are going to
insert another set of fields then they will need a new set of names to
match.

One alternative (that I haven't tested) would be to convert all the fields
to text in the existing document before inserting the new copy of the
document. If the fields don't exist there should be no conflict with the
bookmark names.

I still feel that this is mail merge and that you are trying to re-invent
the wheel for no good reason that has yet become apparent.

You may be interested in
http://www.gmayor.com/Form_Fields_and_Mail_Merge.htm and there are some
other examples on my web site at http://www.gmayor.com/SelectFile.htm and
http://www.gmayor.com/word_vba_examples.htm that you may find useful in
pointing a way forward if you wish to pursue this roll-your-own mail merge
project.
ok but here my code is in loop
[quoted text clipped - 58 lines]
 
H

hemaneelagiri via OfficeKB.com

Thanks for your reply i will chekc with those sites

Graham said:
Frankly I don't see how this could work. You are apparently inserting a
section break at the end of the document and then adding another copy of the
same document at the end. The problem with that is that the newly inserted
document will have the same set of field names that have already been used.
Bookmarks must be unique, thus the fields in the added section will not be
named and so they will not be loaded with the data. If you are going to
insert another set of fields then they will need a new set of names to
match.

One alternative (that I haven't tested) would be to convert all the fields
to text in the existing document before inserting the new copy of the
document. If the fields don't exist there should be no conflict with the
bookmark names.

I still feel that this is mail merge and that you are trying to re-invent
the wheel for no good reason that has yet become apparent.

You may be interested in
http://www.gmayor.com/Form_Fields_and_Mail_Merge.htm and there are some
other examples on my web site at http://www.gmayor.com/SelectFile.htm and
http://www.gmayor.com/word_vba_examples.htm that you may find useful in
pointing a way forward if you wish to pursue this roll-your-own mail merge
project.
ok but here my code is in loop
[quoted text clipped - 58 lines]
 

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