51 Replies - 1074 Views - Last Post: 19 January 2013 - 09:25 AM
#16
Re: How to work with word document?
Posted 12 January 2013 - 05:42 PM
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?
#17
Re: How to work with word document?
Posted 12 January 2013 - 06:10 PM
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
#18
Re: How to work with word document?
Posted 12 January 2013 - 06:36 PM
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
}
#19
Re: How to work with word document?
Posted 12 January 2013 - 06:48 PM
if ("abcd".IndexOf("c") >= 0) {
Console.WriteLine("found");
}
#20
Re: How to work with word document?
Posted 13 January 2013 - 06:15 AM
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.");
}
andrewsw, on 12 January 2013 - 07:56 AM, said:
I can't really elaborate unless you describe what exactly you need to do with the document; you haven't yet made this clear
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?!
#21
Re: How to work with word document?
Posted 13 January 2013 - 06:20 AM
andrewsw, on 12 January 2013 - 06:10 PM, said:
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
#22
Re: How to work with word document?
Posted 13 January 2013 - 06:31 AM
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.
#23
Re: How to work with word document?
Posted 13 January 2013 - 06:56 AM
Quote
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.
asem0525, on 13 January 2013 - 06:31 AM, said:
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).
#24
Re: How to work with word document?
Posted 13 January 2013 - 07:54 AM
Sent from my T-Mobile G2 using Tapatalk 2
#25
Re: How to work with word document?
Posted 13 January 2013 - 07:54 AM
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?
#26
Re: How to work with word document?
Posted 13 January 2013 - 08:30 AM
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
#27
Re: How to work with word document?
Posted 13 January 2013 - 09:30 AM
Skydiver, on 13 January 2013 - 07:54 AM, said:
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?
andrewsw, on 13 January 2013 - 08:30 AM, said:
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.
#28
Re: How to work with word document?
Posted 13 January 2013 - 09:37 AM
http://www.microsoft...ls.aspx?id=1438
#29
Re: How to work with word document?
Posted 13 January 2013 - 10:55 AM
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
}
}
#30
Re: How to work with word document?
Posted 13 January 2013 - 12:35 PM
Why should I use regex? I was using regular FindAndReplace for every letter.. Or there is a better solution?
andrewsw, on 13 January 2013 - 08:30 AM, said:
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.
|
|

New Topic/Question
Reply





MultiQuote



|