Delete Parragraph(s) using Several ActiveX Control Buttons

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Delete Parragraph(s) using Several ActiveX Control Buttons

Post by RaudelJr »

Hi,

I'm Trying to Delete Parragraph(s) using Several ActiveX Control Buttons

Code: Select all

Sub DeleteCurrentParagraph()
On Error GoTo Skip
    ActiveDocument.Unprotect Password:=""
Skip:
    Selection.StartOf Unit:=wdParagraph
    Selection.MoveEnd Unit:=wdParagraph
    Selection.Delete
    ActiveDocument.Protect _
        Type:=wdAllowOnlyFormFields, NoReset:=False, Password:=""
End Sub

Private Sub CommandButton1_Click()
DeleteCurrentParagraph
End Sub

Private Sub CommandButton2_Click()
DeleteCurrentParagraph
End Sub

Private Sub CommandButton3_Click()
DeleteCurrentParagraph
End Sub
My problem is that once I delete the first paragraph using the first button, I can no longer delete the second paragraph using the second button or third button.

Basically only the first button I press works, and all the others stop working.

Is there a way to correct this?

Thank you in advance.

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

I don't see why you need 3 buttons, since they all run the same code...
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

They delete different paragraphs.

Different sections in a template.

User will decide which to keep and which to delete.

I need three buttons so the paragraph section/location is different. Or calculated at the time it is being pressed as it can change once a paragraph is removed. The button is also removed during the deleting process.

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

Could you attach a sample template (zipped) or document?
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

PS When I try it, clicking the button removes the paragraph containing the button, including the button itself. So if the three buttons are located in three different paragraphs, each button will delete a different paragraph.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

HansV wrote:PS When I try it, clicking the button removes the paragraph containing the button, including the button itself. So if the three buttons are located in three different paragraphs, each button will delete a different paragraph.
I'll attach a file shortly.

What u mention is what happens.

The button is also removed. But the remaining button loose the function of the Macro.

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

> But the remaining button loose the function of the Macro.

That doesn't happen when I try it; that's why I'd like to see a copy of your document or template.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

HansV wrote:> But the remaining button loose the function of the Macro.

That doesn't happen when I try it; that's why I'd like to see a copy of your document or template.
Here is the Attachment.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

I see the behavior that you describe. I did a little searching; it seems that command buttons aren't supposed to work at all in a protected document, so that it works the first time is probably a bug, not a feature.

You could move the DeleteCurrentParagraph macro to a standard module in the template, and assign it to a custom keyboard shortcut and/or a Quick Access Toolbar button (specify that it should be for documents based on the template only). The user can then click in one of the form fields, and use the keyboard shortcut or QAT button to delete the paragraph.

A slightly shorter version of the macro:

Code: Select all

Sub DeleteCurrentParagraph()
    On Error GoTo Skip
    ActiveDocument.Unprotect
Skip:
    Selection.Paragraphs(1).Range.Delete
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
Best wishes,
Hans

User avatar
Charles Kenyon
5StarLounger
Posts: 641
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by Charles Kenyon »

Another option would be a UserForm to control this. That could be launched by a QAT button.

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

HansV wrote:I see the behavior that you describe. I did a little searching; it seems that command buttons aren't supposed to work at all in a protected document, so that it works the first time is probably a bug, not a feature.

You could move the DeleteCurrentParagraph macro to a standard module in the template, and assign it to a custom keyboard shortcut and/or a Quick Access Toolbar button (specify that it should be for documents based on the template only). The user can then click in one of the form fields, and use the keyboard shortcut or QAT button to delete the paragraph.

A slightly shorter version of the macro:

Code: Select all

Sub DeleteCurrentParagraph()
    On Error GoTo Skip
    ActiveDocument.Unprotect
Skip:
    Selection.Paragraphs(1).Range.Delete
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

Your shorter code is GREAT!.

Now, what is weird is that if instead of using Delete you use Copy. it works for all buttons.

updated code below.

Code: Select all

Sub DeleteCurrentParagraph()
    On Error GoTo Skip
    ActiveDocument.Unprotect
Skip:
    Selection.Paragraphs(1).Range.Copy
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
[/quote]

So it seems that the issue is only when the Button is deleted along with the paragraph.

Not sure what the issue is there.

I have a Template full of paragraphs used to "Copy" instead of "Delete" and it works fine.
I just ran into this problem now that I needed a template to Delete paragraphs.

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

Using command buttons in a document protected for forms is not 100% reliable, as you have found. I don't know of a solution.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

HansV wrote:Using command buttons in a document protected for forms is not 100% reliable, as you have found. I don't know of a solution.
Thank you HansV

I might have to rely on MacroButtons instead.

One other thing on the zipped document previously attached; The very last TextFormField has a Macro that runs on Exit "DocUnpro" It references TextFormField Bookmarked as Text1.

When Tabing out of the last TextFormField, it deletes Text1 completely. Almost as if it select the form and then applies the tab to it. But this behavior is incorrect. It should simply select it as instructed by the code ActiveDocument.Bookmarks("Text1").Select

Same issue happens when referencing any other TextField. I'm a bit puzzled, and all users Tab out of the last field so they encounter the behavior mentioned.

Code: Select all

Sub DocUnpro()
'
' DocUnpro Macro
'
'If document is protected, unprotect it.
    If ActiveDocument.ProtectionType <> wdNoProtection Then
        ActiveDocument.Unprotect Password:=""
    End If
    
    With ActiveDocument.Sections(1)
        .Headers(wdHeaderFooterPrimary).Range.Text = ""
    End With
    
    ActiveDocument.Bookmarks("Text1").Select
End Sub

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

Your macro unprotects the document. It doesn't delete Text1, at least not when I try it. But since the form field has been selected and the document is now unprotected, you will overwrite the form field the moment you start typing. If you protect the document again at the end of the macro, this won't happen.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

HansV wrote:Your macro unprotects the document. It doesn't delete Text1, at least not when I try it. But since the form field has been selected and the document is now unprotected, you will overwrite the form field the moment you start typing. If you protect the document again at the end of the macro, this won't happen.
Interesting, different behaviors in different machines. With me, it does just as you mention, it unlocks the documents as it's meant to be, it Select Text1 TextField, and then it seems to have that Tab in memory, and it applies it after selecting Text1 TextField, which in turn, removes Text1 TextField completely replacing it with a Tab. User ends up needing to Undo to get the text back or Cntl+z.

I'll have to see into this later.

The ActiveX Control Buttons is a complete Mistery (as they don't worked Locked or Unlocked for deleting purposes atleast).

Thank you HansV for all your help, I really appreciate it.

Raudel

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

Hi HansV,

I figured it out. In the Macro I added the "End" which basically forces the macro to reset. allowing the next button to restart.

Code: Select all

Sub DeleteCurrentParagraph()
    On Error GoTo Skip
    ActiveDocument.Unprotect
Skip:
    Selection.Paragraphs(1).Range.Delete
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
    End
End Sub

Private Sub CommandButton1_Click()
    DeleteCurrentParagraph
End Sub

Private Sub CommandButton2_Click()
    DeleteCurrentParagraph
End Sub

Private Sub CommandButton3_Click()
    DeleteCurrentParagraph
End Sub
As for the Text1 TextFieldForm issue, I have no idea. On my end it still takes the Tab used to exit out of the Last TextFielfForm and applies it once Text1 is selected, thus, clearing it's content.

At least the Delete buttons now work. :grin:

Raudel.

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

What a weird solution!
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

HansV wrote:What a weird solution!
Is it sane? Or am I looking at unforeseen issues?

User avatar
HansV
Administrator
Posts: 78920
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by HansV »

I really don't know. Perhaps one of our Word experts can comment.
Best wishes,
Hans

RaudelJr
2StarLounger
Posts: 136
Joined: 17 Apr 2017, 19:16

Re: Delete Parragraph(s) using Several ActiveX Control Butto

Post by RaudelJr »

HansV wrote:I really don't know. Perhaps one of our Word experts can comment.
Who should I ask?