dj44
04-24-2016, 07:43 AM
Good day to all the good folks,
how is everyone doing.:grinhalo:
i am ticking along nicely until now.
Well its a long story.
I am trying to copy 3 paragraphs from a folder full of documents into 1 master table.
In each document
Paragraph 1 - ends with " - double quotation mark
Paragraph 2 - ends with the ) - Closing bracket
Paragraph Range 3 - Starts with a ' - single quotation mark and ends with a - (hyphen)
Column 1 | Column 2 | Column 3
Paragraph 1 | Paragraph 2 | Paragraph Range 3
Then loop through the next document.
Now i did try my best and google hasn't been a good friend as usual.
I found this
http://www.vbaexpress.com/forum/showthread.php?1322-Macro-to-find-string-and-copy-sentence-containing-string
I also found this from our good man Graham
http://www.vbaexpress.com/forum/showthread.php?54756-VBA-Word-Copy-Strings-into-a-Table
my 10th attempt ++
Sub Copy3ParagraphsintoTable()
' Copy 3 Paragraphs into a 3 column Table
Dim oSource As Document
Dim oTarget As Document
Dim oTable As Table
Dim oRow As Row
Dim oRng As Range
Dim sText As String
Dim oCell As Range
Dim i As Long
'Assign a variable name to the document
Set oSource = ActiveDocument
'Save the document (before changes are made to it)
oSource.Save
'Open a new document - the MASTER TABLE DOCUMENT
Set oTarget = Documents.Add
oTarget.PageSetup.Orientation = wdOrientLandscape
' Browse for the folder that has all the documents that contains the paragraphs
Dim myFolder As String, myFile As String
myFolder = GetFolder
' MAKE A NICE FORMATTED TABLE
'Create a table in that document and name the header row cells
Set oTable = oTarget.Tables.Add(oTarget.Range, 1, 2,3)
oTable.Rows(1).Cells(1).Range.Text = "Paragraph 1"
oTable.Rows(1).Cells(2).Range.Text = "Paragraph 2"
oTable.Rows(1).Cells(3).Range.Text = "Paragraph Range3"
oTable.Borders.InsideLineStyle = wdLineStyleSingle
oTable.Borders.OutsideLineStyle = wdLineStyleSingle
oTable.Borders.InsideColor = wdColorGray40
oTable.Borders.OutsideColor = wdColorGray40
'Set a range to the original document
Set oRng = oSource.Range
'Remove duplicated paragraph breaks
With oRng.Find
Do While .Execute(FindText:=" "", MatchWildcards:=True) ' FIND THE double quotation mark Paragraph 1
oRng.Text = vbCr
Loop
End With
'Reset a range to the original document
Set oRng = oSource.Range
'Locate the ID text
With oRng.Find
'paragraph 2 is missing?
'For paragraph range 3
Do While .Execute(FindText:="[']", MatchWildcards:=True)
On Error GoTo lbl_Exit
Do Until oRng.Next.Words(1) = " - " Or _ 'hyphen
oRng.End = oSource.Range.End
oRng.MoveEnd wdWord
Loop
'Include the "End" marker in the range
oRng.End = oRng.End + 3
'Add a row to the table
oTable.Rows.Add
'Set a variable name to the last row of the table
Set oRow = oTable.Rows.Last
'Fill the first cell in the row with the ID text
oRow.Cells(1).Range.Text = Split(oRng.Text, vbCr)(0)
'Clear the sText string variable
sText = ""
'Add the remaining text to the string
For i = 2 To oRng.Paragraphs.Count
sText = sText & oRng.Paragraphs(i).Range.Text
Next i
'Remove any paragraph breaks from the string
sText = Replace(sText, Chr(13), " ")
'Remove any double spaces from the string
sText = Replace(sText, " ", " ")
'Add the string to the second cell of the last row
oRow.Cells(2).Range.Text = sText
'Collapse the range to its end
oRng.Collapse 0
'And go round again
Loop
End With
'Close the original document without recording the changes
'oSource.Close 0
lbl_Exit:
Exit Sub
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
Now i do believe i have been a bit too ambitious - still got my byclicle training wheels on
I'm stuck and don't know what to do any more, paragraph 2 is MIA - not sure how to weave that into the code
Ever grateful if any one can help me
thank you good folks :)
DJ
how is everyone doing.:grinhalo:
i am ticking along nicely until now.
Well its a long story.
I am trying to copy 3 paragraphs from a folder full of documents into 1 master table.
In each document
Paragraph 1 - ends with " - double quotation mark
Paragraph 2 - ends with the ) - Closing bracket
Paragraph Range 3 - Starts with a ' - single quotation mark and ends with a - (hyphen)
Column 1 | Column 2 | Column 3
Paragraph 1 | Paragraph 2 | Paragraph Range 3
Then loop through the next document.
Now i did try my best and google hasn't been a good friend as usual.
I found this
http://www.vbaexpress.com/forum/showthread.php?1322-Macro-to-find-string-and-copy-sentence-containing-string
I also found this from our good man Graham
http://www.vbaexpress.com/forum/showthread.php?54756-VBA-Word-Copy-Strings-into-a-Table
my 10th attempt ++
Sub Copy3ParagraphsintoTable()
' Copy 3 Paragraphs into a 3 column Table
Dim oSource As Document
Dim oTarget As Document
Dim oTable As Table
Dim oRow As Row
Dim oRng As Range
Dim sText As String
Dim oCell As Range
Dim i As Long
'Assign a variable name to the document
Set oSource = ActiveDocument
'Save the document (before changes are made to it)
oSource.Save
'Open a new document - the MASTER TABLE DOCUMENT
Set oTarget = Documents.Add
oTarget.PageSetup.Orientation = wdOrientLandscape
' Browse for the folder that has all the documents that contains the paragraphs
Dim myFolder As String, myFile As String
myFolder = GetFolder
' MAKE A NICE FORMATTED TABLE
'Create a table in that document and name the header row cells
Set oTable = oTarget.Tables.Add(oTarget.Range, 1, 2,3)
oTable.Rows(1).Cells(1).Range.Text = "Paragraph 1"
oTable.Rows(1).Cells(2).Range.Text = "Paragraph 2"
oTable.Rows(1).Cells(3).Range.Text = "Paragraph Range3"
oTable.Borders.InsideLineStyle = wdLineStyleSingle
oTable.Borders.OutsideLineStyle = wdLineStyleSingle
oTable.Borders.InsideColor = wdColorGray40
oTable.Borders.OutsideColor = wdColorGray40
'Set a range to the original document
Set oRng = oSource.Range
'Remove duplicated paragraph breaks
With oRng.Find
Do While .Execute(FindText:=" "", MatchWildcards:=True) ' FIND THE double quotation mark Paragraph 1
oRng.Text = vbCr
Loop
End With
'Reset a range to the original document
Set oRng = oSource.Range
'Locate the ID text
With oRng.Find
'paragraph 2 is missing?
'For paragraph range 3
Do While .Execute(FindText:="[']", MatchWildcards:=True)
On Error GoTo lbl_Exit
Do Until oRng.Next.Words(1) = " - " Or _ 'hyphen
oRng.End = oSource.Range.End
oRng.MoveEnd wdWord
Loop
'Include the "End" marker in the range
oRng.End = oRng.End + 3
'Add a row to the table
oTable.Rows.Add
'Set a variable name to the last row of the table
Set oRow = oTable.Rows.Last
'Fill the first cell in the row with the ID text
oRow.Cells(1).Range.Text = Split(oRng.Text, vbCr)(0)
'Clear the sText string variable
sText = ""
'Add the remaining text to the string
For i = 2 To oRng.Paragraphs.Count
sText = sText & oRng.Paragraphs(i).Range.Text
Next i
'Remove any paragraph breaks from the string
sText = Replace(sText, Chr(13), " ")
'Remove any double spaces from the string
sText = Replace(sText, " ", " ")
'Add the string to the second cell of the last row
oRow.Cells(2).Range.Text = sText
'Collapse the range to its end
oRng.Collapse 0
'And go round again
Loop
End With
'Close the original document without recording the changes
'oSource.Close 0
lbl_Exit:
Exit Sub
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
Now i do believe i have been a bit too ambitious - still got my byclicle training wheels on
I'm stuck and don't know what to do any more, paragraph 2 is MIA - not sure how to weave that into the code
Ever grateful if any one can help me
thank you good folks :)
DJ