Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need to add correct stress accents to every word in a Word document. I have a script called "DoAccentuate" which determines the correct accentuation for any word that is input. However, I don't know how to capture the currently selected found word, process it with my DoAccentuate script, and replace that same word with the result (without effecting the formatting of the text in Word). This is what I tried.
VB
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim document As Word.Document
        document = Globals.ThisAddIn.Application.ActiveDocument

        Dim FindObject As Word.Find = document.Application.Selection.Find
        With FindObject
            .ClearFormatting()
            .Text = "<*>"
            .MatchWildcards = True
            .Replacement.ClearFormatting()
            .Replacement.Text = Accentuate.Accentuate.DoAccentuate(document.Application.Selection.Text)
            .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

        End With
    End Sub

The "DoAccentuate" function comes from an open source Latin program; the function returns a stress accented word as a string (based on some complex patterns) for any Latin word sent to is as a string. For sake of argument, imagine "DoAccentuate" works like this an simply tacks on a letter at the end of the string:
VB
Public Class Accentuate
    Shared Current_Word As String

    Public Shared Function DoAccentuate(ByVal s As String) As String
       Dim Accented_word As String
       Current_word = s
       Accented_word = Current_word & "ǽ"
       DoAccentuate = Accented_word
    End Function
End Class
Posted
Updated 7-Apr-13 23:20pm
v3
Comments
Maciej Los 6-Apr-13 18:17pm    
Please, post the body of DoAccentuate function.
Have you seen this: http://msdn.microsoft.com/en-us/library/office/aa211953%28v=office.11%29.aspx[^]
johhosoe48 7-Apr-13 7:18am    
Thanks for the link; it seems "document.Application.Selection.Text" is the wrong thing for me to send to the DoAccentuate function...what I want to send is the string representing the word which the MSWord wildcard search "<*>" selects.

I am wanting to automatically accent all the words in the document...not having to manually select an individual word.
Kenneth Haugland 7-Apr-13 7:15am    
I got a headeach for scrolling down here, please just post the relevant code and not everything.
[no name] 7-Apr-13 7:39am    
Agreed. I am not going to wade through all of those If/ElseIf statements.
johhosoe48 8-Apr-13 5:25am    
Sorry about that. I shortend the post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900