Just MS Word

Mihail

Registered User.
Local time
Today, 06:55
Joined
Jan 22, 2011
Messages
2,373
Hello !

I know very well :) that this is a forum for Access questions. Or, at least, related to Access. But also I know that here are very skilled programers. Maybe someone will be able to help me with this problem.

@Moderators
No offense if you will decide to remove this thread.
But I hope to do this after few weeks. Thank you.

So, my problem(s).

I need to copy some (many) articles from the www and to turn it into Word document. Done. The Copy-Paste feature work great :)

I use (in Word) this code in order to "clean" the text:
Code:
Option Explicit

Sub UsualReplacements()
Dim LoopsCount As Long
    LoopsCount = 0

Dim FindWhat As String, ReplaceWith As String

'Manual line break -> Chr(13)
    FindWhat = "^l"
    ReplaceWith = Chr(13)
        Selection.HomeKey Unit:=wdStory
        Do
            LoopsCount = LoopsCount + 1
        Loop While Replacements(FindWhat, ReplaceWith)
    
    FindWhat = " " & Chr(13)
    ReplaceWith = Chr(13)
        Selection.HomeKey Unit:=wdStory
        Do
            LoopsCount = LoopsCount + 1
        Loop While Replacements(FindWhat, ReplaceWith)

    FindWhat = Chr(13) & " "
    ReplaceWith = Chr(13)
        Selection.HomeKey Unit:=wdStory
        Do
            LoopsCount = LoopsCount + 1
        Loop While Replacements(FindWhat, ReplaceWith)

    FindWhat = Chr(13) & Chr(13)
    ReplaceWith = Chr(13)
        Selection.HomeKey Unit:=wdStory
        Do
            DoEvents
            LoopsCount = LoopsCount + 1
        Loop While Replacements(FindWhat, ReplaceWith)
    
    MsgBox ("Done after " & LoopsCount & " loops")
End Sub

Private Function Replacements(FindWhat As String, ReplaceWith As String) As Boolean
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = FindWhat
        .Replacement.Text = ReplaceWith
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    
    With Selection
        If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseStart
        Else
            .Collapse Direction:=wdCollapseEnd
        End If
        Replacements = .Find.Execute(Replace:=wdReplaceOne)
    End With
End Function
The code do a good job but the last Do-Loop never end because at the end of document Word insert a Chr(13).
So my code will find forever the string Chr(13) & Chr(13)
I can survive with this (CTRL+Break) but the question remain:
How can I test that I am at the end of document ?

The main unsolved problem is that Word, after I run the code, still not understand where a paragraph start and where is ended (do a try with something copied from www).
So, I waste a lot of time in order to apply styles to certain paragraphs.

Can you help me ?
Thank you !
 
Thank you CJ.

I am waiting for some input about the other problem.
 
Thank you for helping me.
I solved this... somehow :)
I'm sure that the "Word" guys have better solution but for me is enough (for now)

Attached is a document (Word 2007) for test (in case that anyone wish to see the solution)

Thank you again, CJ.
 

Attachments

Users who are viewing this thread

Back
Top Bottom