Reference to ContentControl of Word document - delete whole paragraph

jaryszek

Board Regular
Joined
Jul 1, 2016
Messages
213
Hi Guys,

I have wrote the macro where I am looping throw word document and delete contentcontrols where there is no checkbox = true:

Code:
Dim applWord As Object
Dim docWord As Object
Dim DocActive As Object
Dim Path_Folder As String
Dim Path_Plik As String
Dim Nazwa_Dokumentu As String
Dim Nazwa_Dokumentu_temp As String
Dim str_tmppath  As String
Dim Wiersz_wpisy As Long
Dim ContentItems As Object
Dim i As Integer
Dim Dictionary_Word As Object
Set Dictionary_Word = CreateObject("Scripting.Dictionary")
Dim counter As Integer
Dim ExcVariable As String
Dim Wbworkbook As Workbook
Dim Wysokosc As Integer
Dim Szerokosc As Integer
Dim SmallVariable As Integer
Dim Licznikus As Integer

Set applWord = CreateObject("Word.Application")
applWord.Visible = True

Set docWord = applWord.Documents.Open(Path_Plik)


For Each ContentItems In docWord.contentcontrols

Licznikus = Licznikus + 1


If Licznikus > 4 Then


ContentItems.Paragraphs.Delete


    Debug.Print ContentItems.Tag
    Debug.Print ContentItems.Type


    If ContentItems.Type = "1" Then
        GoTo Nastepca
    ElseIf ContentItems.Type = "8" And ContentItems.Checked = True Then
            If Err.Number <> 0 Then
                GoTo Nastepca
            End If
                counter = counter + 1
                Dictionary_Word.Add ContentItems.Tag, 1
                Dim vkeys, vitems
                vkeys = Dictionary_Word.keys
                vitems = Dictionary_Word.items
    Else
Nastepca:
            If Not Dictionary_Word.exists(ContentItems.Tag) Then
                ContentItems.Delete True
            End If
    End If
End If
Next ContentItems

This is working very well.
My document BEFORE macro looks like:

pelnomocnictwo.png


And after macro looks like:

after_macro.png




The problem is that when I am using Delete property - this working fine but it left empty lines between point 1 and 4.
So insted of delete property there should be Backspace there.
Something like this:

contentcontrol.delete backspace or contentcontrol.paragraph delete.

Is there any solution for this?

I will appreciate any kind of your support and help,
Thank You
Jacek Antek
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
The code you posted could not possibly work as there is no such method as .Paragraphs.Delete or a .Paragraphs property for a ContentControl. Hence, code like:
ContentItems.Paragraphs.Delete
would invariably fail. At the very least you would need to have code like:
ContentItems.Range.Paragraphs(1).Range.Delete
Even so, looping forwards through a collection and deleting items from it is risky, as items may be missed as a result; it's always safer to loop backwards through the collection instead.

As for:
ContentItems.Delete True
that only deletes the content control and its contents; it does not affect the associated paragraph.
 
Upvote 0
Wow Macropod you are the best !
Thank you very much - your code:
Code:
[COLOR=#333333]ContentItems.Range.Paragraphs(1).Range.Delete[/COLOR]
is working like a charm.

What did you mean about this?:

Even so, looping forwards through a collection and deleting items from it is risky, as items may be missed as a result; it's always safer to loop backwards through the collection instead.

I can not see any dangers connected with this:
In my Document i have Checkboxes called as bullets in list so checkbox 1 is refferencing to bullet 1 etc. etc.

I am checking first checkbox and if it is true then I have "1" added to my collection. After that when loop comes to PlainTextControl I am checking if this 1 exists in collection. If yes - do nothing, if not - delete paragraph.

How can i do it backwards?

Looping throw each contentcontrol is read from left to right so Checkbox 1 first, second PlainTextControl with number 1, third Checkbox 2 and after that PlainSecondControl with number 2...

Jacek Antek
 
Last edited:
Upvote 0
What did you mean about this?:
Even so, looping forwards through a collection and deleting items from it is risky, as items may be missed as a result; it's always safer to loop backwards through the collection instead.
I can not see any dangers connected with this
It's a well established fact that looping forwards through a collection and deleting items from it can lead to items being skipped when two or more consecutive items should be deleted. The behaviour is inconsistent and, if it works in this case, consider yourself lucky. That said, you shouldn't rely on an approach with known inconsistent behaviour. Looping backwards is always reliable. To loop backwards, you'd use code along the lines of:
Code:
With docWord
  For Licznikus = .ContentControls.Count To 1 Step -1
    If Licznikus > 4 Then
      .ContentControls(Licznikus).Range.Paragraphs(1).Range.Delete
    End If
  Next
End With
plus, of course, whatever other control structures you want inside the loop.
 
Upvote 0

Forum statistics

Threads
1,213,492
Messages
6,113,967
Members
448,537
Latest member
Et_Cetera

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top