Ignore tables and content controls by Word VBA

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Ignore tables and content controls by Word VBA

Post by Sam1085 »

Hi,

I've few macros to find some document properties. I've to ignore tables while running those macros. Please see below sample codes:

Code: Select all

Sub CheckWidowOrphanControls()
' Check Widow/Orphan Controls
Application.ScreenUpdating = False
Const message As String = "Widow/Orphan = False"
Dim oPar As Paragraph
Dim oRng As Word.Range

For Each oPar In ActiveDocument.Paragraphs
Set oRng = oPar.Range
    With oRng
        With .Find
            .ClearFormatting
            .Text = "^13"
            .Execute
        End With
        Set oRng = oPar.Range
        If oPar.Range.Information(wdWithInTable) = False Then
        If oPar.WidowControl = False Then
        If .Find.Found Then
                .Select
                Selection.Comments.Add Range:=Selection.Range
                Selection.TypeText Text:=message
                Set oRng = Nothing
        End If
        End If
    End If
  End With
Next
Application.ScreenUpdating = True
End Sub
Currently this macro use the following line to ignore tables. It's works very well.

Code: Select all

If oPar.Range.Information(wdWithInTable) = False Then
But the problem is if table count is very high then macro takes more time to complete the process. Any better way to ignore tables in the entire document (Without any impact on the document or tables).

any help is much appreciated. Thanks!
Last edited by Sam1085 on 29 Oct 2016, 10:55, edited 1 time in total.
-Sampath-

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Ignore tables by Word VBA

Post by macropod »

Hans and I have both provided you with code for doing that: https://social.msdn.microsoft.com/Forum ... um=worddev" onclick="window.open(this.href);return false; You said there that my code worked well...
Paul Edstein
[Fmr MS MVP - Word]

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

Re: Ignore tables by Word VBA

Post by Rudi »

Hi Sam,

Paul is correct in pointing out your request for code on the Microsoft forums. You could have provided a cross reference to the thread with possible solutions that others could have referenced and used for further development. I have been searching and playing around with code to try assist (and this page could have assisted me), partly as I had some time, but also as I felt challenged to try; bearing in mind my Word VBA is weak. Anyways, please ensure you do refer to any cross-posting you have done on other sites. It is considered good manners.

I cannot say the code below will work or not, but I tried to merge your code with code from here (by Frosty), which tries to completely skip tables altogether instead of ignoring tables but still scanning through them. You will need to see first if the code works and second if it speeds things up by actually skipping the tables completely. It looks lengthy because the code needs to be doubled up to run one more time for all data below the last table.

Try this:

Code: Select all

Public Sub AnotherWayToDoHorribleThingsToACat()
Dim oTable As Table
Dim rngWorking As Range
Dim lStart As Long
Const message As String = "Widow/Orphan = False"
Dim oPar As Paragraph
Dim oRng As Word.Range
    'get our starting position
    lStart = ActiveDocument.Content.Start
    'intialize our range
    Set rngWorking = ActiveDocument.Content

    'get the range of everything in front of the current table
    For Each oTable In ActiveDocument.Tables
        rngWorking.Start = lStart
        rngWorking.End = oTable.Range.Start
        With rngWorking
            With .Find
                .ClearFormatting
                .Text = "^13"
                .Execute
            End With
            If .Paragraphs.WidowControl = False Then
                If .Find.Found Then
                    .Select
                    Selection.Comments.Add Range:=Selection.Range
                    Selection.TypeText Text:=message
                    Set oRng = Nothing
                End If
            End If
        End With
        lStart = oTable.Range.End
    Next

    'now that we're done, we need everything after the last table to the end of the document
    rngWorking.Start = ActiveDocument.Tables(ActiveDocument.Tables.Count).Range.End
    rngWorking.End = ActiveDocument.Content.End
    With rngWorking
        With .Find
            .ClearFormatting
            .Text = "^13"
            .Execute
        End With
        If .Paragraphs.WidowControl = False Then
            If .Find.Found Then
                .Select
                Selection.Comments.Add Range:=Selection.Range
                Selection.TypeText Text:=message
                Set oRng = Nothing
            End If
        End If
    End With
End Sub
Regards,
Rudi

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

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Ignore tables by Word VBA

Post by Sam1085 »

Thanks Rudi and Macropod,

Thanks both. It'll very helpful to improve my knowledge and macro efficiency. I tried both of your codes (Hans and Macropod) but I think different office versions have different functions and process running speeds. As I tested, It's work very faster in office 2016. But I've to run this macros only on Office 2010. Therefore few macros takes too much time. That's why I'm asking it again in this forum.

One more thing I've to know about content controls. I'm trying to skip rich text content controls by using the same way.

Code: Select all

If oPar.Range.Information(wdContentControlRichText) = False Then
But something went wrong :(

Any idea to do that? Thanks!
-Sampath-

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

Re: Ignore tables by Word VBA

Post by HansV »

Information works only with the members of wdInformation. wdContentControlRichText is not one of them, so Information(wdContentControlRichText) won't work.
S1308.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

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

Re: Ignore tables by Word VBA

Post by HansV »

A content control does not always take up an entire paragraph; a paragraph can contain other text, and it may contain multiple content controls.
What do you want to do if a paragraph contains a rich text control among other content? Skip the entire paragraph?
Best wishes,
Hans

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Ignore tables by Word VBA

Post by Sam1085 »

Thank you Hans for the information,

I've a macro to find multiple character spaces in my document. But my document contains multiple characters inside the rich text content control.

Sample Code:

Code: Select all

Sub CheckMultiSpace()
Dim oPar As Paragraph
' Check Multi Character Space
Application.ScreenUpdating = False
'Clear Comments
Application.UserName = "Multi Character Spaces" & vbTab
Application.UserInitials = "MS"
For Each oCom In ActiveDocument.Comments
    If oCom.Initial = "MS" Then
    oCom.Delete
End If
Next oCom
Const message As String = "Multi Character Spaces"
     With Selection.Find
         .ClearFormatting
         .Forward = True
         .Text = "^032{2,}"
         .MatchWildcards = True
         Selection.HomeKey Unit:=wdStory
         Do While .Execute
             ActiveDocument.Comments.Add Range:=Selection.Range, Text:=message
         Loop
     End With
Application.ScreenUpdating = True
End Sub
img.png
I got a run time error while running the macro in my word doc. I think the Reason is MS word doesn't allowed to add comment inside the content controls. That's why I've to skip find those multiple space characters. Is there any way to do that?

Thanks!
You do not have the required permissions to view the files attached to this post.
-Sampath-

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

Re: Ignore tables by Word VBA

Post by HansV »

Ah, so this has nothing to do with the original question. That is confusing!

If I run your macro in a document with rich text content controls that contain multiple spaces, it adds comments without causing an error.

But you could avoid inserting comments in content controls as follows:

Code: Select all

Sub CheckMultiSpace()
    Const message As String = "Multi Character Spaces"
    Dim oCom As Comment
    Dim rng As Range
    ' Check Multi Character Space
    Application.ScreenUpdating = False
    'Clear Comments
    Application.UserName = "Multi Character Spaces" & vbTab
    Application.UserInitials = "MS"
    For Each oCom In ActiveDocument.Comments
        If oCom.Initial = "MS" Then
            oCom.Delete
        End If
    Next oCom
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Forward = True
        .Text = "^032{2,}"
        .MatchWildcards = True
        Do While .Execute
            Set rng = ActiveDocument.Range(0, Selection.End)
            If rng.ContentControls.Count = 0 Then
                ActiveDocument.Comments.Add Range:=Selection.Range, Text:=message
            End If
        Loop
     End With
    Application.ScreenUpdating = True
End Sub
Notes:

You declared a variable oPara but didn't use it.
You used a variable oCom but didn't declare it.
Best wishes,
Hans

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

Re: Ignore tables by Word VBA

Post by HansV »

And another thing: you should restore the original username/initials at the end of the code. I found it VERY irritating that running your macro to test it changed my username!
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Ignore tables by Word VBA

Post by macropod »

Sam1085 wrote:I tried both of your codes (Hans and Macropod) but I think different office versions have different functions and process running speeds. As I tested, It's work very faster in office 2016. But I've to run this macros only on Office 2010.
As I said in the other thread, the code was developed & tested on Word 2010. It runs very fast there, without error. If you had problems, it was most likely due to implementation errors on your part, as indicated by the error messages you were getting.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Ignore tables and content controls by Word VBA

Post by Sam1085 »

Thank you Hans,

Apologies if confused you. I've changed the title name now.

I missed to declare oCom. Thanks for mention it. Your code is works for my documents.

Thanks again.
Last edited by Sam1085 on 29 Oct 2016, 11:07, edited 1 time in total.
-Sampath-

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Ignore tables and content controls by Word VBA

Post by Sam1085 »

HansV wrote:And another thing: you should restore the original username/initials at the end of the code. I found it VERY irritating that running your macro to test it changed my username!
Oh.. my bad sir. Yes, I already added personalize setting reset code into my macro file user form end.
-Sampath-

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

Re: Ignore tables and content controls by Word VBA

Post by HansV »

OK, thanks!
Best wishes,
Hans

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Ignore tables by Word VBA

Post by Sam1085 »

macropod wrote:
Sam1085 wrote:I tried both of your codes (Hans and Macropod) but I think different office versions have different functions and process running speeds. As I tested, It's work very faster in office 2016. But I've to run this macros only on Office 2010.
As I said in the other thread, the code was developed & tested on Word 2010. It runs very fast there, without error. If you had problems, it was most likely due to implementation errors on your part, as indicated by the error messages you were getting.
Yes Paul, I think my documents type would be the reason. Because my document contains too much tables and rows, rich text content controls, too much paragraphs...etc.
-Sampath-

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Ignore tables and content controls by Word VBA

Post by macropod »

Your document 'type' and the table count has nothing to do with the errors. Neither would the presence of rich text content controls or the paragraph count have any bearing on the matter. The fact that you had errors on Word 2010 (which the code was developed on) but not on Word 2016 also shows that it was an implementation error.
Paul Edstein
[Fmr MS MVP - Word]