Re: work with a word doc file by Peter
Peter
Mon Feb 09 00:30:23 CST 2004
Hi isabelle
Each document will only contain 1 fax number? Does the fax number contain
anything other than numbers, spaces, hypens etc? Is the fax number
parenthesised as below or was that just for illustration. What I'm trying
to determine is how do you know when you've reached the end of the fax
number?
Also, if you're using the fax number as the name of the new document what
happens when you send another fax to the same number, since you already
have a file with that name.
You need to clearly state what you're trying to achieve and how you intend
to achieve. I'm not convinced you've thought this through thoroughly.
I'm happy to help with snippets and give you pointers but I don't want to
take on the responsibility of writing a fairly substantial script for you.
As a start here's how you get Word running and do a search. The code
assumes that the fax number starts with the text "Fax:" and the number
consists of Numbers, Spaces, Hyphens:
Option Explicit
Dim wdApp
Dim docNew
Dim rngFaxNo
Dim objArgs
Dim strDocument
Dim strOriginalPrinter
' Get the command line arguments
Set objArgs = WScript.Arguments
If objArgs.Count = 0 Then
MsgBox "You must supply the name of the file to search"
WScript.Quit (1)
Else
strDocument = objArgs.Item(0)
End If
Set wdApp = WScript.CreateObject("Word.Application")
With wdApp.Application
' Open the document specified in the cmd line
Set docNew = .Documents.Open(strDocument)
' Search the document for the fax number
If SearchDoc(docNew, rngFaxNo) Then
MsgBox "Fax number = " & rngFaxNo.Text
Else
MsgBox "Fax number not found"
End If
docNew.Close 0 ' 0 = wdDoNotSaveChanges
.Quit
End With
Set wdApp = Nothing
Function SearchDoc(byval Doc, ByRef Range)
Dim rngReplace
Dim rngFaxNo
Set rngReplace = Doc.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Fax:"
.Replacement.Text = ""
.Forward = True
.Wrap = 1 ' wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
' Find the fax number prefix text
If .Execute Then
' Return the fax number in the Word range object "Range"
Set rngFaxNo = rngReplace
rngFaxNo.Collapse 0 ' wdCollapseEnd
rngFaxNo.MoveEndWhile ("0123456789 -")
set Range = rngFaxNo
SearchDoc = True
End If
End With
End Function
Call the script some thing like:
ScriptName "C:\My Files\Junk.doc"
This will search the Word document C:\My Files\Junk.doc
HTH + Cheers - Peter
"isabelle" <recup60@yahoo.fr> wrote in news:402684a5$0$28153
$636a15ce@news.free.fr:
>
>>
>> Questions:
>>
>> 1. Are you always going to search for the same string, if no please
> provide a
>> sample of what your search string is. Is you need to search for more
that
> one
>
> yes i search always for the same string(FAX)
>
>> string you need to specify that as well.
>> 2. When you create a new Word document what's it's name. Is it's name
> derived
>> from the original?
>
> the doc's name will have the phone number after FAX
>
> in a word document there's FAX to transmit to different
> people with always the same string (FAX:phone number)
> so for each number i want to create each word file.
>
> thanks
>
>
>
>