Delete Parragraph(s) using Several ActiveX Control Buttons

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 »

They generally read all threads in this forum, so they may jump in without asking. Macropod and Charles Kenyon are the ones that come to mind first.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

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

Post by Rudi »

RaudelJr wrote:
HansV wrote:I really don't know. Perhaps one of our Word experts can comment.
Who should I ask?
You don't have to find them; they will find you! :wink:

(Jay Freedman and Paul Edstein (Macropod) are two MVP's that come to mind, but there are a few other expert Word users that also frequent this forum.)
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

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

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

Post by RaudelJr »

Thanks,

My End Statement "weird solution" was weird indeed.
It was kind of like pulling the hand break. So sometimes it crashes Word and forces a restart. :sad:

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 forgot to mention Jay Freedman; fortunately Rudi referred to him!
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 forgot to mention Jay Freedman; fortunately Rudi referred to him!
Thank you guys, I'll wait for one of them to read this thread and see if they have anything else to add.

Raudel

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 »

Raudel asked me to take another look.
While I know a lot about Word, I know next to nothing about ActiveX controls. That is because by the time I started learning, they were already considered "legacy" stuff.
I'm a lawyer, not a programmer; anything I have to say about ActiveX is pretty worthless.

My suggestion is that you can use QAT buttons saved in your document or template to accomplish the same thing. I also suggested a userform.

http://addbalance.com/word/QATmodification.htm
http://www.gmayor.com/Userform.htm
http://gregmaxey.com/word_tip_pages/cre ... rForm.html
http://gregmaxey.com/word_tip_pages/pop ... bobox.html

I don't know userforms that well, either. When I need one, I build it. It is usually a bit of a pain getting started.

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

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

Post by RaudelJr »

Hi All,

I tried something funky. I resized the buttons to disappear. That way the paragraph and the buttons go away, per say.

And Word doesn't crash. :laugh:

Code: Select all


Sub DeleteCurrentParagraph()
    On Error GoTo Skip
    ActiveDocument.Unprotect
Skip:
    Selection.Previous(Unit:=wdParagraph, Count:=1).Delete
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

Private Sub CommandButton1_Click()
On Error GoTo Skip
    ActiveDocument.Unprotect
Skip:
    CommandButton1.Width = 1
    CommandButton1.Height = 1
    CommandButton1.Font.Size = 1
    
    DeleteCurrentParagraph

End Sub

Private Sub CommandButton2_Click()
On Error GoTo Skip
    ActiveDocument.Unprotect
Skip:
    CommandButton2.Width = 1
    CommandButton2.Height = 1
    CommandButton2.Font.Size = 1
    
    DeleteCurrentParagraph

End Sub

Private Sub CommandButton3_Click()
On Error GoTo Skip
    ActiveDocument.Unprotect
Skip:
    CommandButton3.Width = 1
    CommandButton3.Height = 1
    CommandButton3.Font.Size = 1
    
    DeleteCurrentParagraph

End Sub



Now for some reason, I still have an issue with my last TextFormField - When I tab out of it. It runs the following Code:

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
    Selection.HomeKey Unit:=wdStory
'    ActiveDocument.Bookmarks("Text1").Select
End Sub
The cursor goes straight to the top, but the "Tab" I used to go to the top of the document somehow stays in memory and as soon as the document is unlocked it adds a tab to the top row.

Not sure why.

Thanks everyone for the help you have been providing.

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

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

Post by RaudelJr »

Hi,

I needed to Keep OriginalProtection when pressing the DeleteCurrentParagraph Buttons. So I did a bit of research and modified the code as shows below.

I'm sure is a bit messy as I added it to all the button subroutines too, I'm sure there is probably a way to simplify it.



Code: Select all


Sub DeleteCurrentParagraph()
Dim OriginalProtection As WdProtectionType
    OriginalProtection = ActiveDocument.ProtectionType
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
    Selection.Previous(Unit:=wdParagraph, Count:=1).Delete
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
End Sub

Private Sub CommandButton1_Click()
    Dim OriginalProtection As WdProtectionType
    OriginalProtection = ActiveDocument.ProtectionType
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
    CommandButton1.Width = 0.5
    CommandButton1.Height = 0.5
    CommandButton1.Font.Size = 0.5
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
   
    DeleteCurrentParagraph

End Sub

Private Sub CommandButton2_Click()
    Dim OriginalProtection As WdProtectionType
    OriginalProtection = ActiveDocument.ProtectionType
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
    CommandButton2.Width = 0.5
    CommandButton2.Height = 0.5
    CommandButton2.Font.Size = 0.5
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
   
    DeleteCurrentParagraph

End Sub

Private Sub CommandButton3_Click()
    Dim OriginalProtection As WdProtectionType
    OriginalProtection = ActiveDocument.ProtectionType
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
    CommandButton3.Width = 0.5
    CommandButton3.Height = 0.5
    CommandButton3.Font.Size = 0.5
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
   
    DeleteCurrentParagraph

End Sub

I have another question that users are asking me. Is there a way to Undo the Macro actions? If they happen to Delete the incorrect paragraph, can they Contro+Z and undo their mistake?

I read a little bit on this and there seems to be some Macros that Start an UndoSaver, but not sure if this is something I should do. I tried it and it seems to not really work unless the document is unprotected and in Developer/design mode to Undo the changes. But the resized button doesn't get resized back. (Just entertaining the idea)

Undo codes

Code: Select all


Sub StartUndoSaver() 
    On Error Resume Next 
    ActiveDocument.Bookmarks.Add "_InMacro_" 
    On Error GoTo 0 
End Sub 


Sub EndUndoSaver() 
    On Error Resume Next 
    ActiveDocument.Bookmarks("_InMacro_").Delete 
    On Error GoTo 0 
End Sub 


Sub EditUndo() ' Catches Ctrl-Z 
    If ActiveDocument.Undo = False Then Exit Sub 
    While BookMarkExists("_InMacro_") 
        If ActiveDocument.Undo = False Then Exit Sub 
    Wend 
End Sub 


Sub EditRedo() ' Catches Ctrl-Y 
    If ActiveDocument.Redo = False Then Exit Sub 
    While BookMarkExists("_InMacro_") 
        If ActiveDocument.Redo = False Then Exit Sub 
    Wend 
End Sub 


Private Function BookMarkExists(Name As String) As Boolean 
    On Error Resume Next 
    BookMarkExists = Len(ActiveDocument.Bookmarks(Name).Name) > -1 
    On Error GoTo 0 
End Function 
Thanks for the help in advance.

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 »

You could omit the unprotect/protect code from DeleteCurrentParagraph if you move the code to reprotect the document below the DeleteCurrentParagraph line in the On Click event procedures of the command buttons.

I can't help you with the undo question, I simply don't know.
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:You could omit the unprotect/protect code from DeleteCurrentParagraph if you move the code to reprotect the document below the DeleteCurrentParagraph line in the On Click event procedures of the command buttons.

I can't help you with the undo question, I simply don't know.
Like this? That really simplified the DeleteCurrentParagraph Code. I could almost eliminate it instead.

Code: Select all

Sub DeleteCurrentParagraph()
    Selection.Previous(Unit:=wdParagraph, Count:=1).Delete
End Sub

Private Sub CommandButton1_Click()
    Dim OriginalProtection As WdProtectionType
    OriginalProtection = ActiveDocument.ProtectionType
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
        DeleteCurrentParagraph
        CommandButton1.Width = 0.5
        CommandButton1.Height = 0.5
        CommandButton1.Font.Size = 0.5
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
End Sub

Private Sub CommandButton2_Click()
    Dim OriginalProtection As WdProtectionType
    OriginalProtection = ActiveDocument.ProtectionType
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
        DeleteCurrentParagraph
        CommandButton2.Width = 0.5
        CommandButton2.Height = 0.5
        CommandButton2.Font.Size = 0.5
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
End Sub

Private Sub CommandButton3_Click()
    Dim OriginalProtection As WdProtectionType
    OriginalProtection = ActiveDocument.ProtectionType
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Unprotect
        DeleteCurrentParagraph
        CommandButton3.Width = 0.5
        CommandButton3.Height = 0.5
        CommandButton3.Font.Size = 0.5
    If OriginalProtection <> wdNoProtection Then ActiveDocument.Protect Type:=OriginalProtection, NoReset:=True
End Sub
Thanks HansV

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 »

Yes, you might as well replace the calls to DeleteCurrentParagraph with

Code: Select all

    Selection.Previous(Unit:=wdParagraph, Count:=1).Delete
and do away with DeleteCurrentParagraph.
Best wishes,
Hans