Option Explicit
Public Function Searchwords(TotParas As Integer, MyTopic2$, Mytopic1$, _
Sourcefile As String, Transferfile As String) As Boolean
Dim Wapp As Object, Bigstring As String
Dim Mydata$, Temp As String, PagFlag As Boolean
Dim Myrange As Variant, Myrange2 As Variant, Adjust As Integer
Dim ThisParaLoc As Integer, LastParaLoc As Integer, FirstParaloc As Integer
Searchwords = True
If NoFileError(Sourcefile) Then
Exit Function
End If
On Error GoTo RetErr
Set Wapp = CreateObject("Word.Application")
Temp = Left(Sourcefile, Len(Sourcefile) - 4) & "T.doc"
Wapp.documents.Open Filename:=Temp, ReadOnly:=True
ThisParaLoc = 0
Bigstring = vbNullString
PagFlag = False
If Wapp.Options.Pagination = False Then
Wapp.Options.Pagination = True
PagFlag = True
End If
Wapp.activedocument.Select
LastParaLoc = Wapp.Selection.paragraphs.Count
On Error GoTo RetErr2
Do While ThisParaLoc < LastParaLoc
Set Myrange2 = Wapp.activedocument.paragraphs(ThisParaLoc + 1).Range
Myrange2.SetRange Start:=Myrange2.Start, _
End:=Wapp.activedocument.paragraphs(LastParaLoc).Range.End
Myrange2.Select
With Wapp.Selection.Find
.Text = Mytopic1
.Forward = True
.Execute
If .found = True Then
On Error GoTo RetErr3
If Wapp.Selection.Information(3) > 1 Then
Adjust = Wapp.Selection.Information(3) * 46 - 46
End If
.Parent.Expand Unit:=4
FirstParaloc = Wapp.Selection.Information(10) + Adjust
Set Myrange = Wapp.activedocument.paragraphs(FirstParaloc).Range
Myrange.SetRange Start:=Myrange.Start, _
End:=Wapp.activedocument.paragraphs(FirstParaloc + _
(TotParas - 1)).Range.End
ThisParaLoc = FirstParaloc + (TotParas - 1)
Myrange.Select
If MyTopic2 <> "1" Then
Myrange.Select
With Wapp.Selection.Find
.Text = MyTopic2
.Forward = True
.Execute
End With
If .found = True Then
Myrange.Select
Mydata = Wapp.Selection.Text
Else
GoTo Below
End If
End If
Mydata = Wapp.Selection.Text
Else
Exit Do
End If
End With
Bigstring = Bigstring + Mydata
Below:
Loop
On Error GoTo RetErr4
NoFileError (Transferfile)
BackToReal Transferfile, False
Wapp.documents.Open Filename:=Transferfile, ReadOnly:=False
Wapp.activedocument.Select
With Wapp.activedocument
.Range(0, .Characters.Count).Delete
.content.insertafter Bigstring
End With
Wapp.activedocument.Close savechanges:=True
If PagFlag Then
Wapp.Options.Pagination = False
End If
Wapp.Quit
Set Wapp = Nothing
BackToReal Sourcefile, True
Exit Function
RetErr: On Error GoTo 0: MsgBox "Source Doc Error": Kill Temp: GoTo Erbelow
RetErr2: On Error GoTo 0: MsgBox "Search Error": Kill Temp: GoTo Erbelow
RetErr3: On Error GoTo 0: MsgBox "Range Creation Error": Kill Temp: GoTo Erbelow
RetErr4: On Error GoTo 0: MsgBox "Transfer Doc Error": Kill Temp: Kill _
Left(Transferfile, Len(Transferfile) - 4) & "T.doc"
Erbelow:
Searchwords = False
If PagFlag Then
Wapp.Options.Pagination = False
End If
Wapp.Quit
Set Wapp = Nothing
End Function
Function NoFileError(Flpath As String) As Boolean
Dim fs As Object, TemP3 As String
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.fileexists(Flpath) Then
TemP3 = Left(Flpath, Len(Flpath) - 4) & "T.doc"
fs.copyfile Flpath, TemP3
NoFileError = False
Else
On Error GoTo 0
MsgBox "Error: This file does not exist: " & Flpath
NoFileError = True
End If
Set fs = Nothing
End Function
Function BackToReal(Retpath As String, FlScource As Boolean)
Dim fs As Object, Objwordapp As Object, d As Variant, TemP2 As String
TemP2 = Left(Retpath, Len(Retpath) - 4) & "T.doc"
Set fs = CreateObject("Scripting.FileSystemObject")
On Error GoTo Errcode
fs.copyfile TemP2, Retpath
Set fs = Nothing
Kill TemP2
Exit Function
Errcode:
On Error GoTo 0
If FlScource Then
MsgBox "Transfer proceeding. This source file remains open: " & Retpath
Else
MsgBox "Close all Word Docs. Transfer proceeding. This transfer file was open: " & Retpath
Set Objwordapp = GetObject(, "word.application")
With Objwordapp
.Application.Quit
End With
Set Objwordapp = Nothing
fs.copyfile TemP2, Retpath
End If
Set fs = Nothing
Kill TemP2
End Function
Sub Callfunction()
Dim TParas As Integer, MyTop1 As String, Mytop2 As String
Dim Sfile As String, Tfile As String
Sfile = "c:\vbaxtest.doc"
Tfile = "c:\test.doc"
MyTop1 = "seed"
Mytop2 = "1"
TParas = 5
If Searchwords(TParas, Mytop2, MyTop1, Sfile, Tfile) Then
MsgBox "Transfer complete"
Else
MsgBox "Transfer not successful"
End If
End Sub
|