Automating word

I have a problem when i'm trying to automate Word.

I have a code like that:

        'Incrementing the number of processed lines
        iProcLines = iProcLines + 1
        ' Select a line.
        oWordApp.Selection.MoveDown unit:=wdLine, Count:=1
        'This is an alternative way to mark the line
        If iProcLines = 216 Then Stop
        If ((iProcLines > 0) And ((iProcLines Mod 24) = 0) And ((iTotalLines - (iBreaks - 1)) - (iBreaks - 1) * 24) > 24) Then
            'Add a line break
            'ActiveDocument.StoryRanges(0).InsertBreak WdBreakType.wdPageBreak
            'ActiveDocument.StoryRanges(wdMainTextStory).InsertBreak WdBreakType.wdSectionBreakNextPage
            oWordApp.Selection.InsertBreak wdSectionBreakNextPage
            iBreaks = iBreaks + 1

I have copy this code from a macro I have made in Word to move down through the lines of a document, however because of the Selection object, this code only works if the document is visible, if it's not the code runs but does the things in a completely differente way.

Is there a way to change Selection for another instruction?

Thanks in advance

Who is Participating?
Computer101Connect With a Mentor Commented:
PAQed with no points refunded (of 125) %%STAMP%%

EE Admin
instead of selecting and moving on...u cud navigate through lines using the object.

i think it is document.Sentences or document.Lines
Just to check that we’re all looking at the same thing: - when I run the code below, which is based on your snippet, the difference is that all the new section breaks are inserted at the beginning of the document if the application is invisible, instead of at 24-line separation if it is shown.

You probably understand that the root of the problem is that what constitutes a line is volatile, because it depends so much on formatting, which is done dynamically. That is why there is no Line object. The MoveDown, etc,  methods depend on the display.

Are your ‘lines’ limited by anything other than the fold-point, e.g. paragraph mark, full-stop, newline, or a table row? If so we could work on that.

If not , perhaps you need to re-appraise your approach. What is your objective?

Private Sub Command1_Click()
Dim oWordApp As Word.Application
Dim iProcLines As Integer
Dim iBreaks As Integer
Dim Doc As Word.Document
Dim rng As Word.Range
Dim i As Integer
Const strFile = "C:\Documents and Settings\Graham Skan\My Documents\Allwork\Experts\Selection_MoveDown\quick brown fox.doc"
Set oWordApp = New Word.Application
For i = 0 To 1
    iProcLines = 0
    iBreaks = 0
    oWordApp.Visible = -1 * i
    Set Doc = oWordApp.Documents.Add(strFile)
    Set rng = Doc.Content
    rng.Collapse wdCollapseStart
            'Incrementing the number of processed lines
            iProcLines = iProcLines + 1
            ' Select a line.
            oWordApp.Selection.MoveDown unit:=wdLine, Count:=1
            If iProcLines = 216 Then
                Exit Do
            End If
            If (iProcLines Mod 24) = 0 Then
                'Add a Section break
                oWordApp.Selection.InsertBreak wdSectionBreakNextPage
                iBreaks = iBreaks + 1
            End If
    Doc.SaveAs strFile & i
Next i
End Sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.