How to approach this problem?

R

Robert

I want to create a Word document using a template. The
document will then be inserted into another program that
accepts Word files, but cannot have any controls or form
fields in the document, just text. Is there a way to
remove all the controls and fields, but leave the text?
Thanks.
 
J

Jay Freedman

Hi, Robert,

See if this macro does what you want.

Dim oInline As InlineShape
Dim oShp As Shape
Dim oFld As Field

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

For Each oShp In ActiveDocument.Shapes
If oShp.Type = msoOLEControlObject Then
oShp.Delete
End If
Next oShp

For Each oFld In ActiveDocument.Fields
oFld.Unlink
Next oFld
 
R

Robert

Thanks again; your help is much appreciated. I used the
two macros you suggested and they work well when run from
the toolbar. So I added a button that the user can click
at the bottom of the page which unprotects the document
and runs the two macros. The button works fine when there
is only one page, but doesn't work at all if the button is
on the second page. If I unprotect the pages and run the
macros from the toolbar, it works fine even if there is
more than one page. Any idea why the button doesn't work
when it is on the second page? Thanks.
 
J

Jay Freedman

Hi, Robert,

I don't know why a button on the second page won't work, but you don't
say what kind of button it is -- a command button from the Control
Toolbox, or a MacroButton field, or something else??

I don't care for the Control Toolbox widgets at all, because they
don't play nicely with Word. Let me suggest an alternative: Create a
custom toolbar in your template, put your button on it, and let it
float near the bottom of the window instead of docked at the top. That
combines the advantages of the toolbar button (it actually works as
intended, and you don't have to do fancy footwork to prevent it from
printing) with those of the in-document button (it doesn't get lost
among dozens of other buttons).

BTW, I meant that code to be all in one macro, not two. I guess I
shouldn't have left out the Sub and End Sub statements. Something like
this:

Sub ZapControlsAndFields()
Dim oInline As InlineShape
Dim oShp As Shape
Dim oFld As Field

If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

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

For Each oShp In ActiveDocument.Shapes
If oShp.Type = msoOLEControlObject Then
oShp.Delete
End If
Next oShp

For Each oFld In ActiveDocument.Fields
oFld.Unlink
Next oFld
End Sub
 
R

Robert

I was using a command button on the bottom of the form;
the custom toolbar you suggested with a button for the
macro works well, except the name for the macro on the
button is long and a little clunky. By the two macros, I
was referring to this one and one you suggested in an
earlier post.
I am using the find and replace macro you suggested
earlier to delete paragraphs from a list with many
paragraphs, basically a checklist. The user deletes the
character combination in the textbox at the beginning of
the paragraph to have the paragraph remain in the report.
So it is sort of counterintuitive. I'd rather use form
checkboxes, but I don't know if find can be used to find
unchecked checkboxes. Also, the macro to delete the form
fields doesn't have any effect on checkboxes. Thanks for
any suggestions.
 
J

Jay Freedman

Hi, Robert,

To change the name displayed on the button, reopen the Tools >
Customize dialog, then right-click the button and edit the Name item.

I hope you don't want to preserve the checked/unchecked state of the
checkboxes, as that's a bit complicated (though doable). To just
delete them, as well as dropdown form fields if you have any, change
the lines

For Each oFld In ActiveDocument.Fields
oFld.Unlink
Next oFld

to these lines:

For Each oFld In ActiveDocument.Fields
If (oFld.Type = wdFieldFormCheckBox) Or _
(oFld.Type = wdFieldFormDropDown) Then
oFld.Delete
Else
oFld.Unlink
End If
Next oFld
 
R

Robert

I am using this code to find and delete the paragraphs:
Sub ZapPara()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindStop
.Text = "quicker" ' substitute your word

Do While .Execute
oRg.Paragraphs(1).Range.Delete
Loop
End With
End Sub

Instead of using the text "quicker" to delete the
paragraph, I'd like to use an unchecked checkbox to find
the paragraph. Is there some character code to identify an
unchecked checkbox? I want to find the unchecked boxes and
delete the associated paragraph, then clean up by removing
all the checked checkboxes. Thanks for all the help.
 
J

Jay Freedman

Hi, Robert,

No, there is not any "character code" for an unchecked checkbox or any way
to use the .Find to get at one. What you need to do is loop through the
FormFields collection; each time you find a field that represents a
checkbox, look at its .Value to decide whether to delete the paragraph that
contains the field. (I assume that the checkbox is actually *in* the
paragraph that you want to delete, and not just "nearby".)

To do this, add the following code to the ZapControlsAndFields macro after
the If..Then..End If group that unprotects the document, but before the For
Each oInline loop:

For Each oFldCB In ActiveDocument.FormFields
If oFldCB.Type = wdFieldFormCheckBox Then
If oFldCB.CheckBox.Value = False Then
oFldCB.Range.Paragraphs(1).Range.Delete
End If
End If
Next oFldCB

Also add this line to the list of Dim statements at the beginning of the
macro:

Dim oFldCB As FormField
 

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