arsaboo
10-22-2008, 08:36 AM
Hi,
I have a word document and I want to put each sentence in a new line (insert a carriage return after the .)
I have tried several options, but seem to be getting nowhere. I would be grateful if someone could guide me on this. The vba code should be able to distinguish between a full stop and a decimal point. One way to distinguish is that a period is followed by a space. I modified the code on another post and it works for full stops, but there are other separators like ; or :
How can I modify the code to work for all the separators like period, colon, semi colon, etc.
Option Explicit
Sub FindWordCopySentence()
Dim appExcel As Object
Dim objSheet As Object
Dim t As String
Dim aRange As Range
Dim intRowCount As Integer
intRowCount = 1
t = InputBox("Enter the filename:")
Set aRange = ActiveDocument.Range
With aRange.Find
Do
.Text = ". " ' the word I am looking for
.Execute
If .Found Then
aRange.Expand Unit:=wdSentence
aRange.Copy
aRange.Collapse wdCollapseEnd
If objSheet Is Nothing Then
Set appExcel = CreateObject("Excel.Application")
'Change the file path to match the location of your test.xls
Set objSheet = appExcel.workbooks.Open("C:\temp\" & t & ".xlsx").Sheets("Sheet1")
intRowCount = 1
End If
objSheet.Cells(intRowCount, 1).Select
objSheet.Paste
intRowCount = intRowCount + 1
End If
Loop While .Found
End With
If Not objSheet Is Nothing Then
appExcel.workbooks(1).Close True
appExcel.Quit
Set objSheet = Nothing
Set appExcel = Nothing
End If
Set aRange = Nothing
End Sub
Any assistance will be greatly appreciated.
Regards,
Alok R. Saboo
I have a word document and I want to put each sentence in a new line (insert a carriage return after the .)
I have tried several options, but seem to be getting nowhere. I would be grateful if someone could guide me on this. The vba code should be able to distinguish between a full stop and a decimal point. One way to distinguish is that a period is followed by a space. I modified the code on another post and it works for full stops, but there are other separators like ; or :
How can I modify the code to work for all the separators like period, colon, semi colon, etc.
Option Explicit
Sub FindWordCopySentence()
Dim appExcel As Object
Dim objSheet As Object
Dim t As String
Dim aRange As Range
Dim intRowCount As Integer
intRowCount = 1
t = InputBox("Enter the filename:")
Set aRange = ActiveDocument.Range
With aRange.Find
Do
.Text = ". " ' the word I am looking for
.Execute
If .Found Then
aRange.Expand Unit:=wdSentence
aRange.Copy
aRange.Collapse wdCollapseEnd
If objSheet Is Nothing Then
Set appExcel = CreateObject("Excel.Application")
'Change the file path to match the location of your test.xls
Set objSheet = appExcel.workbooks.Open("C:\temp\" & t & ".xlsx").Sheets("Sheet1")
intRowCount = 1
End If
objSheet.Cells(intRowCount, 1).Select
objSheet.Paste
intRowCount = intRowCount + 1
End If
Loop While .Found
End With
If Not objSheet Is Nothing Then
appExcel.workbooks(1).Close True
appExcel.Quit
Set objSheet = Nothing
Set appExcel = Nothing
End If
Set aRange = Nothing
End Sub
Any assistance will be greatly appreciated.
Regards,
Alok R. Saboo