Solved

Hide Unused Fields and Buttons - Word 2007

Posted on 2013-01-15
14
401 Views
Last Modified: 2013-04-09
Hi guys,

A later rendition on an earlier problem I had;

I would like to create a button on the quick access toolbar that will run through an entire document, find the unused form fields (caption text and grey highlight remains), and hide all of these fields, along with a number of command buttons too.

Thanks in advance!

Milos
0
Comment
Question by:RThreeOne
  • 7
  • 6
14 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38782325
Can you confirm that you are talking about Form Fields, and not Content Controls?  Neither type of control has a caption text property, but Content controls do have a PlaceholderText property. Perhaps that is what you mean.

Actually, I think that there are two errors in Word design. Firstly, there should have been a setting to stop PlaceholderText being printed. Secondly, it shouldn't be possible to put ActiveX controls on to a document for this very reason. They are incongruous in a printed document.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38782368
Thinking about it, as I recall, you are using form fields. This will delete all empty form fields in an unprotected document.
Sub DelFormFields()
Dim ffld As FormField
Dim bDeleted As Boolean

Do
    bDeleted = False
    For Each ffld In ActiveDocument
        If ffld.Type = wdFieldFormTextInput Then
            If ffld.Result = "" Then
                ffld.Delete
                bDeleted = True
            End If
        End If
    Next ffld
Loop While bDeleted
End Sub

Open in new window

If, by captions, you mean some text alongside the fields, we would need some way to locate the exact text. Is it a rule that the caption and the field are always in the same paragraph, and nothing else is?
0
 

Author Comment

by:RThreeOne
ID: 38803663
I am using both form fields and content controls. There are two command buttons which are currently successfully linked to delete as a bookmark " " to a button in the quick access toolbar. other than that, there is one drop down menu, and one calendar date picker in the document, the rest of the items are form fields.

I guess what I meant by caption is the initial text displayed in the greyed background of a form field. The drop down box and calendar picker however do have captions. I don't think these are an issue however as the document will be populated by the user and these components are necessary, thus will not be left untouched. It would only be some of the other form fields which may be left unused in the populating of the document.

Currently, your solution faults on the following line;

For Each ffld In ActiveDocument

Thanks again!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38805464
Sorry, that should have been;
For Each ffld In ActiveDocument.FormFields

I think that we might need to see a sample document.
0
 

Author Comment

by:RThreeOne
ID: 38821592
Sure. I'll try that and/or give a sample document in a couple days when I go back to work.

Cheers,

Milos
0
 

Author Comment

by:RThreeOne
ID: 38829336
I have attached a sample document containing the main functions I'm after.

In this case, I am wanting to erase the following and/or more bodies of text should they be left blank by the document's user;

[Street]
[Suburb State Postcode]

Click here to choose proposal type

[Customer site name]

[Insert other details/brief inclusions of proposal etc]

As you can see, some are ActiveX while others are form fields.

Thanks again
Sample-Doc.dotx
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38907061
You can delete unfilled content controls in a similar way, but you would have to check that the text placeholder text was still visible,
Sub DeleteUnusedCCs()
    Dim cc As ContentControl
    For Each cc In ActiveDocument.ContentControls
        If cc.ShowingPlaceholderText Then
            cc.Delete
        End If
    Next cc
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38907106
You can delete the buttons with this code
Sub DelButtons()
Dim ilsh As InlineShape

For Each ilsh In ActiveDocument.InlineShapes
    If ilsh.Type = wdInlineShapeOLEControlObject Then
        ilsh.Delete
    End If
Next ilsh

End Sub

Open in new window

0
 

Author Comment

by:RThreeOne
ID: 38908078
Thanks those work great, although I will have to change the Legacy forms into content controls, but thats fine. My only other question is, is it possible to shift the text up too?

And on that note, is it possible to delete and shift regulr text up based on the selection from a dropdown box? At the moment the dropdown box only input text elsewhere.

EDIT: To elaborate, the dropdown box;

"Choice 1" - Somebookmarks are updated with "Choice 1" text but nothing more.
"Choice 2" - The same bookmarks are updated with "Choice 2", as well as, a a seperate body of text elswhere in the document is deleted and the remaining text is shifted up

Thanks..
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 38908845
If you want, you can delete the whole paragraph, instead of just the control. This shows how to delete the paragraph for some form fields.
Sub DelFormFields()
Dim ffld As FormField
Dim bDeleted As Boolean 'deletion disturbs the collection and the For Each walk-through, so run loop until nothing is deleted

Do
    bDeleted = False
    For Each ffld In ActiveDocument
        If ffld.Type = wdFieldFormTextInput Then
            If ffld.Result = "" Then
                Select Case ffld.Name
                    Case "Text1", "Text3"
                        ffld.Range.Paragraphs.First.Range.Delete
                    Case Else
                        ffld.Delete
                End Select
                bDeleted = True
            End If
        End If
    Next ffld
Loop While bDeleted
End Sub

Open in new window

0
 

Author Comment

by:RThreeOne
ID: 38932243
Thanks again Graham, however i must say that that last macro does nothing from what I can see.

So far I have the document deleting the commandbuttons and one date picker cc whoeevr that is all.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38933036
You haven't mentioned it, so it might be because you haven't modified the code to list the relevant form fields by name. Fairly obviously you need to put your choice of fields in the Case line of the Select block.

(In line with the philosophy of this forum, I am only showing how it is done, not actually doing it for you)
0
 

Author Comment

by:RThreeOne
ID: 38936285
Yes sorry, I realised that after I typed it and forgot to edit the comment. I had not however had a chance to try it out.

I'll try it out next week, and thanks a lot for persisting!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now