dj44
10-02-2016, 02:24 PM
Hi folks,
hope everyones having a good sunday.:)
Well i thought i would catch up on some work this weekend - but now have a problem.
I was trying to copy some words into a 2 column table, but im afraid its gone wrong.
So scattered through out the document there are sentences such as these
============
CP1 : Some text
CT1: Some text
random stuff, random
CP2 : some Text
CT2 : some Text
================
I simply wanted to find these pairs of paragraphs - and put them in a 2 column table
So i would end up with
CP1 : Some text | CT1: Some text
CP2 : some Text | CT2 : some Text
and on etc
Now this is just a brief sample of the many i tried and failed to code.
http://www.msofficeforums.com/word-vba/21408-macro-search-all-words-r-place-new.html
Sub MakeTableFromWords()
Dim oDoc_Source As Document
Dim oDoc_Target As Document
Dim oTbl As Table
Dim oRange As Range
Dim n As Long
Dim strAllFound As String
Application.ScreenUpdating = False
Set oDoc_Source = ActiveDocument
Set oDoc_Target = Documents.Add
Set oTbl = oDoc_Target.Tables.Add(oTarget.Range, 1, 2)
oTarget.PageSetup.Orientation = wdOrientLandscape
With oTbl.Rows(1)
.Cells(1).Range.Text = "CP1"
.Cells(2).Range.Text = "CT1"
End With
With oTbl.Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleSingle
.InsideColor = wdColorGray40
.OutsideColor = wdColorGray40
End With
With oDoc_Source
Set oRange = .Range
With oRange.Find
.Text = CP1 ' Do I need a wild card search for CP1,CP2,CP3,CP4,CP5
While .Execute
oRange.MoveStart wdWord, -1
oDoc_Target.Range.InsertAfter oRange & vbCr
oRange.Collapse wdCollapseEnd
Wend
End With
With oDoc_Source
Set oRange = .Range
With oRange.Find
.Text = CT1
While .Execute
oRange.MoveStart wdWord, -1
oDoc_Target.Range.InsertAfter oRange & vbCr
oRange.Collapse wdCollapseEnd
Wend
End With
End With
End Sub
I also looked on all the forums for pointers but alot of them are too advanced and I just couldnt adapt it to this task.
I would be really grateful for any help - thank you
hope everyones having a good sunday.:)
Well i thought i would catch up on some work this weekend - but now have a problem.
I was trying to copy some words into a 2 column table, but im afraid its gone wrong.
So scattered through out the document there are sentences such as these
============
CP1 : Some text
CT1: Some text
random stuff, random
CP2 : some Text
CT2 : some Text
================
I simply wanted to find these pairs of paragraphs - and put them in a 2 column table
So i would end up with
CP1 : Some text | CT1: Some text
CP2 : some Text | CT2 : some Text
and on etc
Now this is just a brief sample of the many i tried and failed to code.
http://www.msofficeforums.com/word-vba/21408-macro-search-all-words-r-place-new.html
Sub MakeTableFromWords()
Dim oDoc_Source As Document
Dim oDoc_Target As Document
Dim oTbl As Table
Dim oRange As Range
Dim n As Long
Dim strAllFound As String
Application.ScreenUpdating = False
Set oDoc_Source = ActiveDocument
Set oDoc_Target = Documents.Add
Set oTbl = oDoc_Target.Tables.Add(oTarget.Range, 1, 2)
oTarget.PageSetup.Orientation = wdOrientLandscape
With oTbl.Rows(1)
.Cells(1).Range.Text = "CP1"
.Cells(2).Range.Text = "CT1"
End With
With oTbl.Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleSingle
.InsideColor = wdColorGray40
.OutsideColor = wdColorGray40
End With
With oDoc_Source
Set oRange = .Range
With oRange.Find
.Text = CP1 ' Do I need a wild card search for CP1,CP2,CP3,CP4,CP5
While .Execute
oRange.MoveStart wdWord, -1
oDoc_Target.Range.InsertAfter oRange & vbCr
oRange.Collapse wdCollapseEnd
Wend
End With
With oDoc_Source
Set oRange = .Range
With oRange.Find
.Text = CT1
While .Execute
oRange.MoveStart wdWord, -1
oDoc_Target.Range.InsertAfter oRange & vbCr
oRange.Collapse wdCollapseEnd
Wend
End With
End With
End Sub
I also looked on all the forums for pointers but alot of them are too advanced and I just couldnt adapt it to this task.
I would be really grateful for any help - thank you