Please bear with me everyone, it may look like a Word question but it is really to do with Excel so please read on! Basically, I have a series of complex text forms which simply cannot be adjusted in Word. I have therefore prepared them in Excel where they can be manipulated.
I have made a VBA macro which then copies the cells into Word, converts the tables to text, formats them, etc. My problem is that I need to select a range of text between two keywords and format them. This section can change in length so using fixed line ranges in VBA will not work.
I have found the following code which works perfectly within Word. My problem is that I cannot adapt this same code to work when it is running from within Excel's VBA and the whole aim is of course to have one file/macro to do everything!
Below is working demonstration code within Word. The first 2 lines type "Hello and Goodbye" for you and the remaining 'real' code selects everything between Hello & Goodbye, ie " and ". (in my real case there are up to several pages between both keywords)
Now for the Excel conversion part! I have adapted the script to the best of my ability below:
Any help very gratefully received!
I have made a VBA macro which then copies the cells into Word, converts the tables to text, formats them, etc. My problem is that I need to select a range of text between two keywords and format them. This section can change in length so using fixed line ranges in VBA will not work.
I have found the following code which works perfectly within Word. My problem is that I cannot adapt this same code to work when it is running from within Excel's VBA and the whole aim is of course to have one file/macro to do everything!
Below is working demonstration code within Word. The first 2 lines type "Hello and Goodbye" for you and the remaining 'real' code selects everything between Hello & Goodbye, ie " and ". (in my real case there are up to several pages between both keywords)
Code:
Sub SelectRangeBetween()
' Types the text
Selection.HomeKey Unit:=wdStory
Selection.TypeText Text:="Hello and Goodbye"
' The Real script
Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute findText:="Hello", Forward:=True, Wrap:=wdFindStop
Set myrange = Selection.Range
myrange.End = ActiveDocument.Range.End
myrange.Start = myrange.Start + 5
myrange.End = myrange.Start + InStr(myrange, "Goodbye") - 1
myrange.Select
End With
End Sub
Code:
Sub SelectRangeBetween()
Dim wrdApp As Word.Application
Set wrdApp = CreateObject("Word.Application")
With wrdApp
.Documents.Add
.Visible = True
' Types the text
.Selection.HomeKey Unit:=wdStory
.Selection.TypeText Text:="Hello and Goodbye"
' The Real script
Dim myrange As Range
.Selection.HomeKey wdStory
.Selection.Find.ClearFormatting
With .Selection.Find
.Execute findText:="Hello", Forward:=True, Wrap:=wdFindStop
Set myrange = Selection.Range
' Problem is here:
myrange.End = wrdApp.ActiveDocument.Range.End
myrange.Start = myrange.Start + 5
myrange.End = myrange.Start + InStr(myrange, "Goodbye") - 1
myrange.Select
End With
End With
End Sub