Consulting

Results 1 to 19 of 19

Thread: avoid tables

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location

    avoid tables

    [vba]
    Dim novi As Document
    Dim novi1 As Document
    Dim x As Word.Paragraph

    Selection.HomeKey Unit:=wdStory

    For Each x In ActiveDocument.Paragraphs

    Dim iResponse As Integer
    Dim tTable As Table
    ActiveDocument.Tables(1).Select

    Selection.Collapse Direction:=wdCollapseEnd

    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "^p"
    .Replacement.Text = "^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    s1 = Selection.Start
    e1 = Selection.End
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.HomeKey Unit:=wdLine
    s2 = Selection.Start
    e2 = Selection.End
    Set myrange = ActiveDocument.Range(Start:=s1, End:=e2)
    myrange.Select
    Selection.TypeParagraph
    Next
    End Sub
    [/vba]

    How can I avoid tables? If table is found then select it and do
    [vba]Selection.Collapse Direction:=wdCollapseEnd
    [/vba]

    I would like macro to preform search in this way:

    1. Begin at the top of document
    2. Search for paragraphs but if table is found just avoid it and search for next paragraph mark


    Or better way would be if paragraph mark is within table then select this table and do:
    [vba]Selection.Collapse Direction:=wdCollapseEnd
    [/vba]

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Saban, I have to admit I find your posts frustrating. I have suggested things in a number of posts that would make your posts better - and get better responses. Also things that would make your coding better for YOU.

    Be consistent in your naming of things. [vba]Dim x As Paragraph[/vba]While certainly not "wrong", this is not very good naming. Look up standard naming conventions. Yes, you can name things whatever you like, but it makes thing easier for others if you follow some convention.

    Declaring "x" for a Word Paragraph is not so good. X looks like a number. Also "x" does not really "mean" anything. Names should - for the most part - give an indication of what the object, or variable, is.

    There are some standard letters for integers (i, j etc) that we are all used to...but "x" for a paragraph object?

    It may help if you - as I have suggested previously - step through your code, and see what it is actually doing.

    Let's walk through it. Let's say you have 5 paragraphs, and then a table, then 5 more paragraphs.

    For Each paragraph - so it will run this 5 times before it reaches the table.

    With Paragraph 1: select the first table (which has NOTHING to do with paragraph 1), and collapse the selection. The Selection is at the end of the table. Then you search for a paragraph mark, and do your thing.

    Then you go to the next paragraph in your For Each statement (paragraph 2);

    With Paragraph 2: select the first table again (which has NOTHING to do with paragraph 2), and collapse the selection. The Selection is at the end of the table. Then you search for a paragraph mark, and do your thing.

    and on and on.

    This code will of course also process against each paragraph in the table as well....

    What are you doing? You want to check each paragraph EXCEPT paragraphs in tables. This can be done. Easily. But first I have other comments.

    You have:[vba] Dim iResponse As Integer
    Dim tTable As Table [/vba]However, you never use these, it seems. What are they for? If they are not relevant to your post, why are you putting them in your post?

    I have suggested, a couple of times, that you really should work at understanding what a Range is. Using Selection works, but using Range is much, much better. This is a hint. A hint, because there is a simple way to do what you are asking - run through each paragraph but ignore a paragraph if it is in a table. But I am NOT going to tell you what it is.[vba]Dim oPara As Word.Paragraph
    Dim bol_InParagraph As Boolean
    For Each oPara In ActiveDocument.Paragraphs
    ' the test to see if current oPara is in table; ONE line
    bol_InParagraph = blah blah blah
    If bol_InParagraph = False Then
    ' if the paragraph is NOT in a table
    ' do whatever with that paragraph
    End If
    Next oPara[/vba]

    The use of Selection all the time does not work efficiently.

    Good luck.

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    One other thing...[vba]Set myrange = ActiveDocument.Range(Start:=s1, End:=e2) [/vba]OK, you are doing strange things with the Selection and making myrange, but...sure. However, you are declaring, and setting, variables e1, and s2. But you never use them. Why declare and set them?

    Again, if you understood what ranges are, and do, you would not need most of that code you have.

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Sorry for my ignorance and thnx for your explanation and directives on coding I am learning slowely I will listen to your advices they are very usefull I dont have any good resources for learning programming except for those books which are too complex for me and beside this english is not my mother language so it is even harder for me

    Thnx for everything
    Saban

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    No, I am not being critical really. But ARE you looking in Help? is your English good enough to read Help? Do you use the F1 key?

  6. #6
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    No it is Ok really. I very much appreciate all your help and advice

    I have figured out the thing with paragraphs in table here is the code:
    [vba]Sub tabelepero()
    Selection.HomeKey Unit:=wdStory
    Dim oPara As Word.Paragraph
    Dim bol_InParagraph As Boolean
    For Each oPara In ActiveDocument.Paragraphs
    ' the test to see if current oPara is in table; ONE line
    bol_InParagraph = oPara.Range.Information(wdWithInTable)
    oPara.Range.Select
    Selection.Collapse Direction:=wdCollapseEnd
    If bol_InParagraph = False Then
    ' if the paragraph is NOT in a table
    ' do whatever with that paragraph
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "^p"
    .Replacement.Text = "^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    s1 = Selection.Start
    e1 = Selection.End
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.HomeKey Unit:=wdLine
    s2 = Selection.Start
    e2 = Selection.End
    Set myrange = ActiveDocument.Range(Start:=s1, End:=e2)
    myrange.Select
    Selection.TypeParagraph
    Selection.Find.ClearFormatting
    Selection.Collapse Direction:=wdCollapseEnd
    End If
    Next oPara[/vba]

    But I dont know why it gets into infinity loop when reaching last paragraph
    (I would like macro to stop when last paragraph is reached)

    with this macro i can get rid of that zero width tables

  7. #7
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    [VBA]Sub deletezerowidth()
    Dim t As Table
    For Each t In ActiveDocument.Tables
    If t.PreferredWidth = 0 Then
    t.Delete
    End If
    Next
    End Sub[/VBA]

    This solved my problem with zero width tables
    (How can macro delete zero width tables and I cant )

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Well, I am glad you found the Range.Information(wdWithInTable).

    However....you are still doing that selection stuff. You do NOT need to do that! As far as I can see, ALL of the following code is not needed.[vba] Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "^p"
    .Replacement.Text = "^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    s1 = Selection.Start
    e1 = Selection.End
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.HomeKey Unit:=wdLine
    s2 = Selection.Start
    e2 = Selection.End
    Set myrange = ActiveDocument.Range(Start:=s1, End:=e2)
    myrange.Select
    Selection.TypeParagraph
    Selection.Find.ClearFormatting
    Selection.Collapse Direction:=wdCollapseEnd [/vba]You do NOT need to be doing all (or ANY!) of that selecting. In fact...it is bad coding.

  9. #9
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    But how can I then get rid of zero width table that is between
    (I thought I can find ^p and then goto next line and to the begining of that line and select everything that is between those two and replace it with paragraph)

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Please try and keep issues separate.

    You asked about avoiding tables. Ok, you can do that.

    Now, when you posted, were you aware there was a table between these particular paragraphs? You certainly did not say so.

    Besides...WHAT in that code is removing that table? Nothing that I can see.

    Making ranges, selecting it, typing a paragraph...what is it that you are trying to DO?

    For EACH paragraph, you are:

    1. selecting its range - I have no idea why, as you immediately collapse it
    2. if it is NOT in a table, repeat NOT in a table, you use Selection find, to find the next paragraph.
    3. you move down one line, then move the Selection to the start of the line. Why, I have no idea.
    4. you make a range object of the selection, then type a paragraph.

    I can not see the purpose to most of this.

    Are you trying to find more of those zero width tables? And BTW: try, on a test document, putting a few "normal" tables, and loop through them displaying a message box with the PreferredWidth. They will ALL come back as = 0.

    How about we start at the beginning. What are you trying to do?

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by fumei
    And BTW: try, on a test document, putting a few "normal" tables, and loop through them displaying a message box with the PreferredWidth. They will ALL come back as = 0.
    Ooops!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Well sorry for that I didnt mentioned any zero width table problem (my mistake)

    Acctually I am trying to get rid of these zero width tables that are apearing between paragraphs.
    one way was like this :
    first find paragraph mark (but as you can see with that corrupted document-that with zero table width the cursor will not move to another line if pressing right-arrow key)
    that is why I pressed down-arrow key to go to line bellow and then I go to the begining of that line and select everything from paragraph mark and begining of that line.
    And then I replace this selection with paragraph mark [vba]Selection.TypeParagraph [/vba]

    And this replaces the zero width table with paragraph

    But I am not sure if this is the best way

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Hi Malcom......<grin>......yes.....I was going to mention it earlier, but I was being polite....<grin>.

    Saban, your post was "avoid" tables. Now you are saying you are not trying to avoid them, you are trying to deal with them. You don't want to avoid them at all, you want to DO something with them. Not really the same thing, is it?

    Are you saying that there are lots of these tables between paragraphs? Are there any real tables in the document? Do you want to delete all of these tables? Do you want to get the text that is in them out?

    I still don't really know what you are trying to do. Further, I don't have enough information about the document. Further, as Tony pointed out, this file is corrupted. It seems you are trying to fix the corruption. That may, or may not, be possible.

  14. #14
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Yes avoid the ones that are not zero width
    I just want to delete zero width tables
    And i do not need text that is in these zero width tables

    So i came up with something like this:
    [vba]
    Sub deletezerowidth()
    Dim t As Table
    For Each t In ActiveDocument.Tables
    t.PreferredWidthType = wdPreferredWidthPoints
    If t.PreferredWidth = 0 Then
    t.Delete
    End If
    Next
    End Sub
    [/vba]

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    This file is corrupt.

    As usual, I ask questions, and you do not answer them all. Are there any other tables?

    Why not just delete all the tables? Eh? That would solve it. Delete them all. Maybe not. Maybe there are tables you actually want...who knows, since you did not answer the question.

    The table in question - and who knows if there are others...as you did not answer the question...has a Column Count = 0. It HAS NO COLUMNS!

    As stated, this file is corrupt. In my opinion, you should not trust anything out of this file.

    Using PreferredWidthType to delete is not reliable, as if there are other tables - which I don't know, as you did not answer the question - they very well may have the same PreferredWidthType.

    However, if you do have other tables you wish to keep, and they are OK, then delete tables by the Column.Count = 0. Real tables will not have this.[vba]Dim oTable As Table
    For Each oTable In ActiveDocument.Tables
    If oTable.Columns.Count = 0 Then
    oTable.Delete
    End If
    Next[/vba]will delete any table that has a column.count = 0.

    Use the tools you have...all that searching for paragraphs and selecting stuff is crap. Do what you want to do, which is - apparently - to delete funny tables.

  16. #16
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Yes i need other tables! I dont need just the tables that are zero width

    THNX

  17. #17
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    saban,

    Do you have lots of documents with these zero-width tables? If so, where are they coming from? Have they been created with Word?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  18. #18
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Oh, and BTW, PreferredWidth won't have a meaningful value unless one has been explicitly set - zero just means "ignore me", it does not mean the table has an actual width of zero.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  19. #19
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Which is why if you do a test for PreferredWidth across a bunch of tables, they will all come back as PreferredWidth = 0...it is the default.

    Did you try doing the search for tables with 0 columns? Legitimate tables will not have a column count = 0. Or...well they should not anyway.

    Again, though, it seems this file is corrupt and you really should try to recreate it as a proper file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •