How to work with word document?

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

51 Replies - 2181 Views - Last Post: 19 January 2013 - 09:25 AM Rate Topic: -----

#46 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: How to work with word document?

Posted 14 January 2013 - 06:05 PM

Glad I could help you to make progress :bananaman:

You might want to be aware that the Word Find/Replace feature can use a basic form of regular expressions. It may be possible to do something like: replace this character, but ONLY IF it is followed by this other character.
Was This Post Helpful? 2
  • +
  • -

#47 hurremSultan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-January 13

Re: How to work with word document?

Posted 19 January 2013 - 06:55 AM

I'm wondering is there any builtin class in C# that I could use for reading and replaceing every cahracter right after reading the character from file. In my case I don't have some special words, no need to get the whole word..
Any advice?

View Postasem0525, on 13 January 2013 - 12:42 PM, said:

Yes, exactly, the translit program for Kazakh language.

I'm just wondering, why whould you translit by words? Is it faster?
and there are also some rules that should be applied for some letter, and then there are some exception words that I need to check with database...
That's why I choosed first to check for certain letters, then for exception words, and then translit the rest of the document letter by letter.


View PostSkydiver, on 13 January 2013 - 10:55 AM, said:

Ah... Translit.

If I had to tackle this project the way I would go about it is I would have a single interface for getting words and putting back words, and multiple classes for reading and writing. Something like:

interface IFileReaderWriter : IDisposable
{
    bool MoveNext();
    string Current { get; set; }
}

void TranslitFile(string fileName)
{
    using (IFileReaderWriter readerWriter = CreateReaderWriter(fileName))
    {
        while (readerWriter.MoveNext())
        {
            string word = TranslitWord(readerWriter.Current));
            readerWriter.Current = word;
        }
    }
}

string TranslitWord(string word)
{
    // returns a translit version of the incoming word
}

IFileReaderWriter CreateReaderWriter(string fileName)
{
    if (Word97ReaderWriter.IsValid(fileName))
        return new Word97ReaderWriter(fileName);
    if (Word2007ReaderWriter.IsValid(fileName))
        return new Word2007ReaderWriter(fileName);
    if (PdfReaderWriter.IsValid(fileName))
        return new PdfReaderWriter(fileName);
    :
}

class Word2007ReaderWriter : IFileReaderWriter
{
    public static IsValid(string fileName)
    {
        // return true if file is a Word2007 file
    }

    public Word2007ReaderWriter(string fileName)
    {
        // open the file
    }

    public override bool MoveNext()
    {
        // write Current word to the file
        // read the next word into Current
        // return false if end of file
    }

    public override string Current { get; set; }

    public override void Dispose()
    {
        // flush all changes to the file
        // close the file
    }
}


Was This Post Helpful? 0
  • +
  • -

#48 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: How to work with word document?

Posted 19 January 2013 - 07:04 AM

Quote

I'm wondering is there any builtin class in C# that I could use for reading and replaceing every cahracter right after reading the character from file.


Can you explain what you want to do a little more clearly? Currently, it just sounds like a Find and Replace.
Was This Post Helpful? 0
  • +
  • -

#49 hurremSultan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-January 13

Re: How to work with word document?

Posted 19 January 2013 - 07:46 AM

I want to replace each character in file with another one, it is also like translit.

imlementing it by using Find.Execute() method is too long, in this case it spends time for searching and then replaces it, then search for another character from the begining of file again, so if I want to replace all the alphabetic letters it will go through the whole document 26 x2 (lower case and upper case) =48 times, but I want it to replace by 1 lookup, so like: It get the first character it is "a" replace with " a' ", if the next char is "c" replace with "s" etc, make it by one look up, so it goes through the whole document only one time.

I know I can implement it just by writing my own code, but I'm wondering may be there is some built-in class that can ease my life :)

any advice will be apreciated, thank you
Was This Post Helpful? 0
  • +
  • -

#50 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: How to work with word document?

Posted 19 January 2013 - 08:10 AM

You would use the Find method with Replace:=wdReplaceAll, as you had before :whistling:

Sub Macro1()

    Selection.HomeKey Unit:=wdStory

    With Selection.Find
        .Text = "a"
        .Replacement.Text = " a' "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub


For the replace text you could also use unicode characters.
Was This Post Helpful? 0
  • +
  • -

#51 hurremSultan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-January 13

Re: How to work with word document?

Posted 19 January 2013 - 08:18 AM

In my case I I don't want to use the Find.Execute, because as I stated earlier, in this case it will be searching the entire file again and again, first for letter a, then for letter b, etc. I want it to replace the characters instantly, by reading file just once. In my case, I don't need to search the document, I'm not looking for special letter, I just wnat to replace every met character in file with some other character, it's kind a encrytion... my own :)

View Postandrewsw, on 19 January 2013 - 08:10 AM, said:

You would use the Find method with Replace:=wdReplaceAll, as you had before :whistling:/>

Sub Macro1()

    Selection.HomeKey Unit:=wdStory

    With Selection.Find
        .Text = "a"
        .Replacement.Text = " a' "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub


For the replace text you could also use unicode characters.

Was This Post Helpful? 0
  • +
  • -

#52 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: How to work with word document?

Posted 19 January 2013 - 09:25 AM

Well if you can get the documents' contents into a StreamReader, as indicated by SkyDiver earlier, then you might consider using regular expressions to replace characters. I don't know what would happen to the formatting though, or how efficient this might be.

using System.Text.RegularExpressions;

            string testString = "Hello and abracadabra.";
            string testResult = "";
            testResult = Regex.Replace(testString, "[a-z]", delegate(Match match) {
                string sMatch = match.ToString();
                switch (sMatch) {
                    case "a":
                        return "w";
                    case "b":
                        return "x";
                    case "c":
                        return "y";
                    case "d":
                        return "z";
                    default:
                        return sMatch;
                }
            }, RegexOptions.Multiline );    // | RegexOptions.IgnoreCase
            Console.Write(testResult);
            Console.ReadKey();


I still think the native Word replace feature will be faster though :)
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4