How to work with word document?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

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

#16 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 12 January 2013 - 05:42 PM

Could anybody help me to solve my problem, I don't know how can I get the "nextchat",
I mean, using word Find function I find a character that was searched for, and know I need to get the next character. The next character that stays right after the searched character.
How can I do this?
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3251
  • View blog
  • Posts: 10,902
  • Joined: 12-December 12

Re: How to work with word document?

Posted 12 January 2013 - 06:10 PM

The following is in Word VBA so would need to be translated to Word-Interop:

Sub Macro2()
    Dim nextChar As String
    
    With Selection.Find
        .Text = "the"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    If Selection.Find.Execute Then
        'was the Find successful?
        Selection.Collapse wdCollapseEnd
        nextChar = Selection.Range.Characters(1).Text
        MsgBox nextChar
    End If
End Sub

Was This Post Helpful? 1
  • +
  • -

#18 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 12 January 2013 - 06:36 PM

I'm just wondering, if there is a way to do this:
I have a group of letters, like [a, b,c].
then when I find my nextChar, can I just check if the nextchar is one of letters from the group, so that I don't need to use switch-Case expression like:

switch (nextChar)
{
   case "A":
      statement
      jump-statement
case "B":
      statement
      jump-statement
case "C":
      statement
      jump-statement
   
}

Was This Post Helpful? 0
  • +
  • -

#19 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3251
  • View blog
  • Posts: 10,902
  • Joined: 12-December 12

Re: How to work with word document?

Posted 12 January 2013 - 06:48 PM

if ("abcd".IndexOf("c") >= 0) {
    Console.WriteLine("found");
}

Was This Post Helpful? 0
  • +
  • -

#20 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 13 January 2013 - 06:15 AM

I don't want to open it, because in case I open it it takes a lot of memory, and my word files are very big!!! As I understand in case of using this Interop, it creats the instance of word, but what if the user doesn't have MS Word installed on his computer, the program will not work?
Now my code looks like this, and it works, but it is too slow.
 public static void Translit(object fileName, object saveAs)
        {
            object missing = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Application wordApp = 
                new Microsoft.Office.Interop.Word.Application();

           Document aDoc = null;

           if (File.Exists((string)fileName))
            {
                DateTime today = DateTime.Now;

                object readOnly = false;
                object isVisible = false;

                //Set Word to be not visible.
                wordApp.Visible = false;

                //Open the word document, this is I want to chage
                aDoc = wordApp.Documents.Open(ref fileName, ref missing, 
                    ref readOnly, ref missing, ref missing, ref missing, 
                    ref missing, ref missing, ref missing, ref missing, 
                    ref missing, ref isVisible, ref missing, ref missing, 
                    ref missing, ref missing);

                // Activate the document
                aDoc.Activate();

                
                //Translitting the letters
                FindAndReplace(wordApp, "б", "b");
                FindAndReplace(wordApp, "а", "a");
                FindAndReplace(wordApp, "в", "v");
                
            }
            else
            {
                MessageBox.Show("File dose not exist.");
                return;
            }

            //Save the document as the correct file name.
            aDoc.SaveAs(ref saveAs, ref missing, ref missing, ref missing, 
                    ref missing, ref missing, ref missing, ref missing, 
                    ref missing, ref missing, ref missing, ref missing, 
                    ref missing, ref missing, ref missing, ref missing);

            //Close the document - you have to do this.
            aDoc.Close(ref missing, ref missing, ref missing);

            MessageBox.Show("File created.");
}



View Postandrewsw, on 12 January 2013 - 07:56 AM, said:

I suppose it is possible to search the document without opening it (as a Word document). It might be possible to use standard File methods - treating it as a binary file(?). It's really a zipped set of XML documents though..

I can't really elaborate unless you describe what exactly you need to do with the document; you haven't yet made this clear :whistling:/>.

Nevertheless, I don't see what is wrong with opening the document (keeping it not visible), use the rich method-set from the Word Object Library, then silently close the document. But you must have your reasons?!

Was This Post Helpful? 0
  • +
  • -

#21 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 13 January 2013 - 06:20 AM

What does this mean: .Wrap = wdFindAsk?

View Postandrewsw, on 12 January 2013 - 06:10 PM, said:

The following is in Word VBA so would need to be translated to Word-Interop:

Sub Macro2()
    Dim nextChar As String
    
    With Selection.Find
        .Text = "the"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    If Selection.Find.Execute Then
        'was the Find successful?
        Selection.Collapse wdCollapseEnd
        nextChar = Selection.Range.Characters(1).Text
        MsgBox nextChar
    End If
End Sub

Was This Post Helpful? 0
  • +
  • -

#22 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 13 January 2013 - 06:31 AM

Now, I need to get the whole word with the "searchedLetter".
For example:
I'm searching for "b" in "I have some problems", so now I need to get that "s", which stays after "b". And then I need to get the whole word "problems".

Actually, first I need to get "pr", and search, if my database have the word "pr", then get "pro" and search for it in DB, then for "prob" and then "probl" and so on, until I find match or the word is ended and there is no match.
And I need to get the last match, if for example, I have "prob" and "problems" in my database I need to get the last match.

Could you help me to code this.
Was This Post Helpful? 0
  • +
  • -

#23 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3251
  • View blog
  • Posts: 10,902
  • Joined: 12-December 12

Re: How to work with word document?

Posted 13 January 2013 - 06:56 AM

Wrap

Quote

Returns or sets what happens if the search begins at a point other than the beginning of the document and the end of the document is reached (or vice versa if Forward is set to False) or if the search text isn't found in the specified selection or range.


You could set it to wdFindStop (the number 0).

If they don't have Word then I suppose it wouldn't work(?). Although, why would they have Word documents if they didn't have Word?

You could try working with a Range object, rather than Selection, but I haven't done this before and have no idea if it will make any difference - probably not :).

Sub Macro1()
    Dim rng As Range

    Set rng = Selection.Range
    rng.WholeStory
    
    MsgBox rng.Characters.Count
    
    With rng.Find
        'do stuff
    End With
End Sub


It may be possible to work with the document without opening it, treating it as a file as I mentioned previously, but I doubt that it is straight-forward.

View Postasem0525, on 13 January 2013 - 06:31 AM, said:

Now, I need to get the whole word with the "searchedLetter".
For example:
I'm searching for "b" in "I have some problems", so now I need to get that "s", which stays after "b". And then I need to get the whole word "problems".

Actually, first I need to get "pr", and search, if my database have the word "pr", then get "pro" and search for it in DB, then for "prob" and then "probl" and so on, until I find match or the word is ended and there is no match.
And I need to get the last match, if for example, I have "prob" and "problems" in my database I need to get the last match.


You have said that you have very large documents, and you are only searching for single letters or short phrases, so of course it will take a very long time. Opening the documents is not the problem. You need to narrow down your search somehow:

Search for longer phrases
Only search certain parts of the document(s).
Was This Post Helpful? 0
  • +
  • -

#24 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: How to work with word document?

Posted 13 January 2013 - 07:54 AM

They would have Word documents, but not Word if they use Open Office, or WordPerfect. Both can read and write Word files.

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

#25 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 13 January 2013 - 07:54 AM

I can't search for the part of the document, because I need to translit the whole document, so I'm translitting it letter by letter...

I'm thinking of treating the file as a binary file as you mentioned previously, and I hope it will work for all MS Words (Word 2003, 2007, 2010) and for Excel and PDF files, is it possible?
Was This Post Helpful? 0
  • +
  • -

#26 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3251
  • View blog
  • Posts: 10,902
  • Joined: 12-December 12

Re: How to work with word document?

Posted 13 January 2013 - 08:30 AM

Translating the whole (large) document? Letter-by-letter?! Good luck with that.. :whistling:/>

If you are not modifying the document (you haven't clarified..) you could open it, save it as a text-file, then close it. Then possibly open it via a StreamReader - even consider Regex for the searching. You can do the same with Excel files. This should (pretty much) work for Word/Excel 2003 files as well. You probably need a full, licensed, version of Adobe Acrobat to do this for PDFs.

Do you actually intend to translate it? That is, to a different language? Really? Letter-by-letter?? I would investigate an API such as Google Translate.

But good luck. Andy.

This post has been edited by andrewsw: 13 January 2013 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#27 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 13 January 2013 - 09:30 AM

View PostSkydiver, on 13 January 2013 - 07:54 AM, said:

They would have Word documents, but not Word if they use Open Office, or WordPerfect. Both can read and write Word files.

Sent from my T-Mobile G2 using Tapatalk 2


So, will my above program work in that case? in case that user doesn't have MS Word

No, I'm not translating, I need to translit, translit from cyrillic letters to latin letters.
and I need to make my program to work with as many documents as it possible, and huge document, may be a document with 500 pages....
So, I'm thinking different ways to implement it to work as efficient as possible...
any advices?

View Postandrewsw, on 13 January 2013 - 08:30 AM, said:

Translating the whole (large) document? Letter-by-letter?! Good luck with that.. :whistling:/>/>

If you are not modifying the document (you haven't clarified..) you could open it, save it as a text-file, then close it. Then possibly open it via a StreamReader - even consider Regex for the searching. You can do the same with Excel files. This should (pretty much) work for Word/Excel 2003 files as well. You probably need a full, licensed, version of Adobe Acrobat to do this for PDFs.

Do you actually intend to translate it? That is, to a different language? Really? Letter-by-letter?? I would investigate an API such as Google Translate.

But good luck. Andy.

Was This Post Helpful? 0
  • +
  • -

#28 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3251
  • View blog
  • Posts: 10,902
  • Joined: 12-December 12

Re: How to work with word document?

Posted 13 January 2013 - 09:37 AM

No, it won't work without Word. You could, I suppose, explore OpenOffice, etc., object models - if they have one.

http://www.microsoft...ls.aspx?id=1438
Was This Post Helpful? 0
  • +
  • -

#29 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: How to work with word document?

Posted 13 January 2013 - 10:55 AM

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
  • +
  • -

#30 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 13 January 2013 - 12:35 PM

So, you mean, that I can use Microsoft.Office.Interop to open and get the content of the file and save it as a new document and then using StreamReader to modify it?

Why should I use regex? I was using regular FindAndReplace for every letter.. Or there is a better solution?

View Postandrewsw, on 13 January 2013 - 08:30 AM, said:

Translating the whole (large) document? Letter-by-letter?! Good luck with that.. :whistling:/>/>

If you are not modifying the document (you haven't clarified..) you could open it, save it as a text-file, then close it. Then possibly open it via a StreamReader - even consider Regex for the searching. You can do the same with Excel files. This should (pretty much) work for Word/Excel 2003 files as well. You probably need a full, licensed, version of Adobe Acrobat to do this for PDFs.

Do you actually intend to translate it? That is, to a different language? Really? Letter-by-letter?? I would investigate an API such as Google Translate.

But good luck. Andy.

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4