How to parse one variable to Microsoft Word

SparkOut SparkOutYNY at gmail.com
Sun Feb 22 05:22:41 EST 2009



Nhan, Tran Thi Thanh wrote:
> 
> 
> Dear SparkOut
> 
> If I want to find two or more "{{replace me}}" different and replace with
> "<<replacement text>>", I need to set up a array of
> objSelect.Find ? Do you have any ideas?
> 
> Regards
> 
> 

Make the working part a sub procedure and call it with the arguments you
need each time.
You can make it an array if you want and walk it, but with two or three or
just a few it's simpler without.

-- 


Dim objWord, objDoc, objSelect, result

on Error Resume Next

Const wdStory = 6
Const wdFindContinue = 1
Const wdReplaceAll = 2

result = "OK"

Set objWord = CreateObject("Word.Application") 
If Err <> 0 Then
  result = "Error opening Word application"
End If

If result = "OK" Then
  objWord.Visible = False
  With objWord
    Set objDoc = objWord.Documents.Open("{{path to doc}}")
    If Err <> 0 Then
      result = "Error opening template document"
    End If
  End With
End If

If result = "OK" Then
  Set objSelect = objWord.Selection
  objSelect.HomeKey(wdStory)
  doReplaceText "{{replace me}}","{{replacement text}}"
  doReplaceText "{{I need replacing}}","{{I was replaced}}"
End If

If result = "OK" Then
  objWord.Visible = True
  Set objSelect= Nothing
  set objDoc = Nothing
  Set objWord = Nothing
Else
  set objDoc = Nothing
  objWord.Close
  objWord.Quit
End If

Sub doReplaceText (tReplaceMe, tReplacementText)
  objSelect.HomeKey(wdStory)
  With objSelect.Find
      .Text = tReplaceMe
      .Forward = True
      .MatchWholeWord = True
      .Wrap = wdFindContinue
      .Format = False
      .Replacement.Text = tReplacementText
      .Execute ,,,,,,,,,,wdReplaceAll
  End With
  objSelect.HomeKey(wdStory)
End Sub


-- 
View this message in context: http://www.nabble.com/How-to-parse-one-variable-to-Microsoft-Word-tp22133088p22145264.html
Sent from the Revolution - User mailing list archive at Nabble.com.




More information about the use-livecode mailing list