PDA

View Full Version : avoid tables



saban
11-20-2006, 03:40 AM
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


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


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:
Selection.Collapse Direction:=wdCollapseEnd

fumei
11-20-2006, 07:14 AM
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. Dim x As ParagraphWhile 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: Dim iResponse As Integer
Dim tTable As Table 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.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

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

Good luck.

fumei
11-20-2006, 07:24 AM
One other thing...Set myrange = ActiveDocument.Range(Start:=s1, End:=e2) 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.

saban
11-21-2006, 02:42 AM
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

fumei
11-21-2006, 03:28 AM
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?

saban
11-21-2006, 03:40 AM
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:
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

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

saban
11-21-2006, 07:38 AM
Sub deletezerowidth()
Dim t As Table
For Each t In ActiveDocument.Tables
If t.PreferredWidth = 0 Then
t.Delete
End If
Next
End Sub

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

fumei
11-21-2006, 08:01 AM
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. 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 You do NOT need to be doing all (or ANY!) of that selecting. In fact...it is bad coding.

saban
11-21-2006, 09:39 AM
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)

fumei
11-21-2006, 10:03 AM
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?

mdmackillop
11-21-2006, 02:23 PM
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!

saban
11-22-2006, 01:35 AM
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 Selection.TypeParagraph

And this replaces the zero width table with paragraph

But I am not sure if this is the best way

fumei
11-22-2006, 02:40 AM
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.

saban
11-22-2006, 08:17 AM
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:


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

fumei
11-22-2006, 11:21 AM
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.Dim oTable As Table
For Each oTable In ActiveDocument.Tables
If oTable.Columns.Count = 0 Then
oTable.Delete
End If
Nextwill 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.

saban
11-28-2006, 01:23 AM
Yes i need other tables! I dont need just the tables that are zero width

THNX

TonyJollans
11-28-2006, 02:45 AM
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?

TonyJollans
11-28-2006, 02:49 AM
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.

fumei
11-28-2006, 04:59 AM
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.