View Full Version : Solved: loop through word file and delete paragraphes and combine tables
freehope
07-16-2010, 08:51 AM
Hi All,
        Good day
 
 I want to ask about  the bellow:
 
 1\ how can I merge multiple word fils into one word file  sequensely
 
 2\ loop through  that word file and delete all the paragraphes 
    between tables
 
 3\ If all remaining tables with the same fields name..
     I mean the tables headers indeed  with the same name
     ..how can I loop through these table and make it all with one 
     table header ??
 
  ....with my Apprecaition  ..wait your vb code help to complete
      my issue  for the proper work on office xp 2000
fumei
07-16-2010, 11:27 AM
1. define how you mean sequentially?  Are all the files in one folder?  Does sequential mean alphabetically?  Does it mean by creation date?
2.  if you delete ALL the paragraphs between tables Word automatically joins all those tables into ONE table.
3.  Tables do not have headers.  They have Rows that are defined as repeating header rows.  Those MUST be the first row(s) of the table.
Can you determine if Table_A has the same, say, first two rows as Table_B?  Yes.
As for: "..how can I loop through these table and make it all with one 
table header ??"
I do not understand this.  They DO have the same table heading rows (as defined).  But what are you trying to say here?  Are you saying you want to merge these tables somehow?  What if:
Table_A - repeating header row = "Yadda"
Table_B - repeating header row = "Whatever"
Table_C - repeating header row = "Yadda"
So, OK, Table_A and Table_C have the same repeating heading row.  But they are separated by Table_B.  What do you want to happen?
Please be as clear and consise as possible.
freehope
07-16-2010, 06:40 PM
Dear Gerry  ,
 
yes, the word files which I want to merge it into one word file at the same folder and its name for ex. A,B,C,D ...I want it in one word file in the same folder sequencely means Alphabetizing  at that new word file.
 
and you say if loop and delete paragraphes between tables  automatically joins all those tables into ONE table..I need that exactely..
then all these tables beginig with the same heading rows name ..so I want keep one of them as heading and all tables detailes under that heading row..cuase all these tables begining with the same heading row(same fields names)
...
hope you understand me now correctely.
wait your response  ASAP with my appreciation 
 
Thanks,
ALaa
.net developer
fumei
07-19-2010, 10:16 AM
Still not following.
".cuase all these tables begining with the same heading row(same fields names)"
But rhere will not BE all these tables.  If you remove the paragraphs, there will ONE table, and whatever heading row it has...will be the heading row.
Is it possible to post a dummy document wilth examples of what you are trying to do?  Make one of the multiple tables, and another showing what you want that one to end up like.  Perhaps we can figure it out that way.
freehope
07-24-2010, 04:00 AM
Hi 
 
All I want to know now that...
 how can I loop through word file and delete paragraphes between tabels ?
 
please if there is VB code make this issue tell me about it 
 
thanks,
 
Alaa
gmaxey
07-24-2010, 05:34 AM
Rough and assumes that you mean empty paragraphs (not paragraphs containing text:
 
Sub ScratchMaco()
Dim oTbl1 As Word.Table
Dim oTbl2 As Word.Table
Dim oRng As Word.Range
Dim oPar As Word.Paragraph
Dim i As Long
Dim bUpTick As Boolean
i = 0
Do
  bUpTick = False
  Set oTbl1 = ActiveDocument.Tables(ActiveDocument.Tables.Count - i - 1)
  Set oTbl2 = ActiveDocument.Tables(ActiveDocument.Tables.Count - i)
  Set oRng = ActiveDocument.Range
  oRng.Start = oTbl1.Range.End
  oRng.End = oTbl2.Range.Start
  For Each oPar In oRng.Paragraphs
    If Len(oPar.Range.Text) = 1 Then
      oPar.Range.Delete
    Else
      If Not oPar.Range.InRange(oTbl2.Range) Then
        bUpTick = True
      End If
    End If
  Next oPar
  If bUpTick = True Then i = i + 1
Loop Until oTbl1.Range.InRange(ActiveDocument.Tables(1).Range)
End Sub
freehope
08-08-2010, 06:56 PM
Hi All
Mr Gery Good day
I tried create new macro contian the code above copy it and past into 
the new macro then run ...noting happened ..the paragraphes between  tables as it was ...pleas e advise what ca I do to delete those  paragraphes at the attachment file and combine all tables into one  table..cause I need in the end result the file have one table 
thanks
appreciate your efforts
Alaa
          :help
gmaxey
08-08-2010, 07:25 PM
The code assumed that you didn't want paragraphs containing text deleted.  Try:
Sub ScratchMaco()
    Dim oTbl1 As Word.Table
    Dim oTbl2 As Word.Table
    Dim oRng As Word.Range
    Dim oPar As Word.Paragraph
    Dim i As Long
    i = 1
    Do
        On Error Resume Next
        Set oTbl1 = ActiveDocument.Tables(ActiveDocument.Tables.Count - i)
        If Err.Number = 5941 Then Set oTbl1 = ActiveDocument.Tables(1)
        Set oTbl2 = ActiveDocument.Tables(ActiveDocument.Tables.Count)
        Set oRng = ActiveDocument.Range
        oRng.Start = oTbl1.Range.End
        oRng.End = oTbl2.Range.Start
        For Each oPar In oRng.Paragraphs
          oPar.Range.Delete
        Next oPar
         i = i + 1
    Loop Until oTbl1.Range.InRange(ActiveDocument.Tables(1).Range)
End Sub
 
You may need to run it twice to clear all paragaphs.
freehope
08-10-2010, 09:57 PM
Hi All ,
 
Dear gmaxy .Good day 
please check my attachment file to know  the code mistakes in the out put word file   as below...
1: the code didn't delete the first paragraph although I ran it towice.
2: big mistake the code delete some of tables information between other tables, it included columns and rows of these tables with out its data ,so I need you solve this mistake and explaine to me if you please why this code make this error ..cause i have a little experience with c# not vb.
3: be noted  that  the larger size rows at the begining of every table ..I mean the same heading row fields names for every table like other tables,so I need to combine it in  one heading row ..
please be noted that the word file which i attached it before was a sample for a big word file..acualy I have 220 word files every file have 100 tables with the same heading name between those tables the paragraphes which I need to delete it with out delete any tables information as your code made .
I am realy appreciate your efforts ..thank you
wait your response to solve this issue so i can begin with it on my files.ASAP
Alaa.
gmaxey
08-11-2010, 04:23 AM
Try this:
 
Sub ScratchMaco()
Dim oTbl1 As Word.Table
Dim oTbl2 As Word.Table
Dim oRng As Word.Range
Dim oPar As Word.Paragraph
Dim i As Long, Count As Long
i = 1
For i = 2 To ActiveDocument.Tables.Count
  ActiveDocument.Tables(i).Rows(1).Delete
Next i
Do
  On Error Resume Next
  Set oTbl1 = ActiveDocument.Tables(ActiveDocument.Tables.Count - 1)
  If Err.Number = 5941 Then Set oTbl1 = ActiveDocument.Tables(1)
  Set oTbl2 = ActiveDocument.Tables(ActiveDocument.Tables.Count)
  Set oRng = ActiveDocument.Range
  oRng.Start = oTbl1.Range.End
  oRng.End = oTbl2.Range.Start
  oRng.Delete
  Loop Until oTbl1.Range.InRange(ActiveDocument.Tables(1).Range)
End Sub
freehope
08-12-2010, 08:26 PM
Dear Greg ,
Although the first paragraphe your code note delete it ,but I approve and very happy.
thaaaaaaaaaaaaaaaaaaaaanks   alot
 Realy appreciate your efforts
Alaa AbdelWahed
MCSD
gmaxey
08-12-2010, 08:31 PM
You're welcome.  I'm pleased that I was able to assist you with a satisfactory solution.
freehope
08-17-2010, 12:56 AM
Dear Greg,
                Good Day 
 
In some files I have an error when I tried run the code  that error says "run time error'5991': cannot access individual rows in this collection because the table has vertically merged cells"
 
seems there is merged cells at a table tried find it but i can't  cause maney tables in this file... 
could you help me please with code determine this error and keep continue looping to combine tables?
 
thanks ..
 
wait your response
 
Alaa
gmaxey
08-17-2010, 04:58 AM
You will probably have to resolve any merged cell issues first.  Run this code to locate and fix those types of tables:
 
Sub ScratchMacro()
Dim oTbl As Word.Table
For Each oTbl In ActiveDocument.Tables
  If Not oTbl.Uniform Then
    oTbl.Select
    MsgBox "Fix this table"
    Exit For
  End If
Next oTbl
End Sub
 
Run the code after you fix each table until all tables are fixed.
fumei
08-17-2010, 12:29 PM
1.  VBA does not work on tables with merged cells.  If you want to action tables with merged cells (at least using VBA) you must "fix" them, as Greg mentions.  I suspect though that this will be difficult for you.  Your TestWrong.docx does NOT have merged cells.  Neither does your 1231.docx file.
2.  If all you want to do is remove all paragraphs NOT in tables and make a repeating header row...then just do that.
Sub RemoveALLNonTableParagraphs_MakeRepeatingHeaderRow()
Dim oPara As Paragraph
Dim oTable As Table
For Each oPara In ActiveDocument.Paragraphs
    If oPara.Range.Information(wdWithInTable) = False Then
        oPara.Range.Delete
    End If
Next
ActiveDocument.Tables(1).Rows(1).Select
Selection.Rows.HeadingFormat = wdToggle
End Sub
On your 121.docx file, this removes ALL paragraphs between tables - which again makes ONE table! - and then uses the first row as a repeating heading row.
freehope
08-17-2010, 01:47 PM
Dear Greg,
 
Thaaaaaaanks ...
Realy Appreciate your efforts 
 
Thanks Gain
 
Alaa
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.