Solved

Automating word

Posted on 2004-08-30
5
1,398 Views
Last Modified: 2012-08-14
I have a problem when i'm trying to automate Word.

I have a code like that:

    Do
        '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
        'oWordApp.ActiveDocument.Bookmarks("\LINE").Select
       
        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

0
Comment
Question by:GustavoValdes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Expert Comment

by:_agj_
ID: 11939416
instead of selecting and moving on...u cud navigate through lines using the object.

i think it is document.Sentences or document.Lines
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 11940177
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
    rng.Select
      Do
            '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
        Loop
    Doc.SaveAs strFile & i
    Doc.Close
Next i
oWordApp.Quit
End Sub
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12561045
PAQed with no points refunded (of 125) %%STAMP%%

Computer101
EE Admin
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question