Select several pages in Word and justify

THC

New Member
Joined
Sep 3, 2020
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello,

I need to “justify” the text in a Word document (called “wdDoc”) from page 3 to page 9.
I tried (adapted from code found on the web):

VBA Code:
'Select pages 3 to 9
Dim rgePages As Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3
Set rgePages = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9
rgePages.EndOf = Selection.Bookmarks("\Page").Range.End
rgePages.Select

'Justitfy pages 3 to 9
Selection.Paragraphs.Alignment = 3
Set Selection = Nothing

And received the error message: “run time error’438’: Object doesn’t support this property or method”

I also tried:
VBA Code:
Set rgePages = wdDoc.Range(Start:=wdDoc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3), End:=wdDoc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9))

With as a result the error message: “run time error’4218’: Method ‘Range of object’_ document failed

Could you help me to find where is the problem?

Thank you in advance

THC
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You were close. I'm guessing the runtime error was on the rgePages.EndOf line. Rather than EndOf, End is the right parameter. Also, you can't set Selection to Nothing. Something is always selected. Therefore, I "collapsed" the selection to the start of the selection which will be the start of page 3. Everything else you had is unchanged.
VBA Code:
'Select pages 3 to 9
Dim rgePages As Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3
Set rgePages = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9
rgePages.End = Selection.Bookmarks("\Page").Range.End
rgePages.Select

'Justitfy pages 3 to 9
Selection.Paragraphs.Alignment = 3
Selection.Collapse
 
Upvote 0
Thank you for this quick answer. I tried this solution but the Debug/Compile detect "Compile error: Argument not optional" for
VBA Code:
rgePages.End =
I tried:
VBA Code:
rgePages.End (Selection.Bookmarks("\Page").Range.End)
With the compile check answer: "Compile error: Invalid use of property"

I'm sure the solution is not far...

THC
 
Upvote 0
I don't know what the problem is. The code I pasted words perfectly for me. I don't know what the compiler would be complaining of for the argument not being optional. Can you copy and post your entire code?
 
Upvote 0
This sub produce a contract in Word using a template, then insert all info from Excel (brought by SQL).
VBA Code:
Sub my_Dynamic_Document_Contract_Generation()
    Dim bCreatedWordInstance As Boolean
    Dim wdApp As Word.Application
    Dim wdSC_Generation As Word.Document
    Dim sDir As String
    Dim sNewFileName As String
    Dim wdDoc As Word.Document
    Dim strName As String
    
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim loTable As ListObject
    Dim sJobNo As String
    Dim sPath As String
    Dim wdbmRange As Word.Range
    Dim sBookmark As String
    
    'Stores the Project number
    sJobNo = ThisWorkbook.Worksheets(gsSC).Range("H1")

    Call myInit
    ' Setup directory
    sDir = gsDRIVE + gsDIR_DOC_TEMPLATE
    
    On Error Resume Next
    ' Create a Word Application instance
    bCreatedWordInstance = False
    Set wdApp = GetObject(, "Word.Application")
    
    If wdApp Is Nothing Then
      Err.Clear
      Set wdApp = CreateObject("Word.Application")
      bCreatedWordInstance = True
    End If
    
    If wdApp Is Nothing Then
        MsgBox "Could not start Word"
        Err.Clear
        On Error GoTo 0
        Exit Sub
    End If
    
    'Let Word trap the errors
    On Error GoTo 0
    ' Set to True if you want to see the Word Doc flash past during construction
    wdApp.Visible = False
      
    'Open Word Template
    Set wdSC_Generation = wdApp.Documents.Open(sDir + gsWTempl_SC_Generation)
    wdSC_Generation.Activate
    '"\\etsi\fileservices\Units\RES\FPS\CONTRACTS\NAV_Documents\Final_versions\MOCKUP_SC_TEMPLATE.dotx"
    
    Set wdDoc = wdApp.Documents.Add(wdSC_Generation.FullName)
    
    'Insert Sentences
    Call Insert_at_Bookmark("Head1", wdDoc)

    Call Insert_at_Bookmark("Head2", wdDoc)
    
    Call Insert_at_Bookmark("Date", wdDoc)
    
    Call Insert_at_Bookmark("Definition", wdDoc)
    
    'insert Type "Plugtests" before Service Contract when needed
    If ThisWorkbook.Worksheets("SC_Active").Range("D2").Value Like "Plugtests Service Contract - Lump sum" Then
    Call Insert_at_Bookmark("Type", wdDoc)
    End If

    Call Insert_at_Bookmark("Ref", wdDoc)
    
    Call Insert_at_Bookmark("Ref_2", wdDoc)
    
    Call Insert_at_Bookmark("Title", wdDoc)
    
    Call Insert_at_Bookmark("Vendor_Presentation", wdDoc)
    
    Call Insert_at_Bookmark("Vendor_Registration", wdDoc)
    
    Call Insert_at_Bookmark("Vendor_Headquarter", wdDoc)
    
    Call Insert_at_Bookmark("Vendor_Signatory", wdDoc)
    
    Call Insert_at_Bookmark("Role", wdDoc)
    
    Call Insert_at_Bookmark("Presentation1", wdDoc)
    
    Call Insert_at_Bookmark("Presentation2", wdDoc)
    
    Call Insert_at_Bookmark("Purpose", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Start", wdDoc)
    
    Call Insert_at_Bookmark("Contract_End", wdDoc)
    
    Call Insert_at_Bookmark("Directives", wdDoc)
    
    Call Insert_at_Bookmark("Insurance", wdDoc)
    
    Call Insert_at_Bookmark("Compliance", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Financial", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Travel", wdDoc)
    
    Call Insert_at_Bookmark("Invoicing", wdDoc)
    
    'insert support index when supported
    If ThisWorkbook.Worksheets("_TABLES").Range("AH10").Value Like "None" Then
    Call Insert_at_Bookmark("Index_Annexes", wdDoc)
    Else
    Call Insert_at_Bookmark("Index_Support", wdDoc)
    End If
        
    Call Insert_at_Bookmark("Bank_Account", wdDoc)
    
    Call Insert_at_Bookmark("Confidentiality", wdDoc)
    
    Call Insert_at_Bookmark("Owner", wdDoc)
    
    Call Insert_at_Bookmark("Use", wdDoc)
    
    Call Insert_at_Bookmark("Annex_1", wdDoc)
    
    Call Insert_at_Bookmark("Allocation", wdDoc)
    
    Call Insert_at_Bookmark("Manager_Title", wdDoc)
    
    Call Insert_at_Bookmark("Tech_Contact_ETSI", wdDoc)
    
    Call Insert_at_Bookmark("Admin_Contact_ETSI", wdDoc)
    
    Call Insert_at_Bookmark("Contract_PM_Name", wdDoc)
    
    Call Insert_at_Bookmark("Contract_PM_Role", wdDoc)
    
    Call Insert_at_Bookmark("Contract_PM_Email", wdDoc)
    
    Call Insert_at_Bookmark("Contract_PM_Tel", wdDoc)
    
    'Call Insert_at_Bookmark("Contract_PM_Mobile", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Admin_Name", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Admin_Role", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Admin_Email", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Admin_Tel", wdDoc)
    
    'Call Insert_at_Bookmark("Contract_Admin_Mobile", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Admin_Address", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Admin_City", wdDoc)
    
    Call Insert_at_Bookmark("Contract_Admin_Country", wdDoc)
    
    'Select pages 3 to 9
    Dim rgePages As Range
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3
    Set rgePages = Selection.Range
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9
    rgePages.End = Selection.Bookmarks("\Page").Range.End
    rgePages.Select
    
    'Dim rgePages As Range
    Selection.wdDoc.Bookmark(Presentation1).Range
    Set rgePages = Selection.Range
    Selection.wdDoc.Bookmark(Use).Range
    rgePages.End (Selection.Bookmarks("\Page").Range.End)
    rgePages.Select
    
    'Justitfy pages 3 to 9
    Selection.Paragraphs.Alignment = 3
    Selection.Collapse
    
    'Set rgePages = wdDoc.Range(Start:=wdDoc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3), End:=wdDoc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9))
    'wdDoc.PageSetup(8).Range.Select
    'ActiveDocument.PageSetup
    
   'insert page breaks before support annexe only when supported
    If ThisWorkbook.Worksheets("_TABLES").Range("AH10").Value <> "None" Then
    wdDoc.Bookmarks("NextPage1").Range.InsertBreak Type:=wdPageBreak
    End If
    
    'insert support title only when supported
    If ThisWorkbook.Worksheets("_TABLES").Range("AH10").Value <> "None" Then
    Call Insert_at_Bookmark("Support", wdDoc)
    End If
    
    'Initialize table Milestones
    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets("_TABLES")
    Set loTable = wsSheet.ListObjects("IG_TABLE_MIL_BY_CONTRACT")
    sBookmark = "Table_Milestones"
    'Copy Paste Table
    Call Export_Table_Word(wdDoc, loTable, "*", sBookmark)
    
    'insert ToR
    Call insert_Docs("TOR", wdDoc, "Contract")
    
    ' Close without saving the Template Document
    wdSC_Generation.Close savechanges:=wdDoNotSaveChanges
    Set wdSC_Generation = Nothing
    
    'Define the name of the file to be saved
    With ThisWorkbook.Worksheets("_TABLES").ListObjects("IG_TABLE_CONTRACT_INFO")
        strName = .DataBodyRange.Cells(1, .ListColumns("Contract_No").Index) & "_"
        strName = Replace(strName, "/", "_")
        strName = strName & .DataBodyRange.Cells(1, .ListColumns("Contract_Version").Index) & "_"
        strName = strName & .DataBodyRange.Cells(1, .ListColumns("Contract_Vendor_Name").Index)
        strName = Replace(strName, ".", "")
        strName = Replace(strName, "/", " ")
    End With
    
    If Len(Dir(gsDRIVE & gsDIR_WORD_TEMPLATE & strName & ".docx")) = 0 Then
        wdDoc.SaveAs2 gsDRIVE & gsDIR_WORD_TEMPLATE & strName
    Else
        wdDoc.SaveAs2 gsDRIVE & gsDIR_WORD_TEMPLATE & strName & "_" & Format(Now, "dd-mm-yy hh mm ss")
    End If
    
    Application.StatusBar = ""
    MsgBox "Document Generated & Saved!"
    
    Application.StatusBar = ""
    
0:
    Set wdApp = Nothing

End Sub
(I didn't copy Option Explicite)

THC
 
Upvote 0
Ah. I didn't realize you were in Excel trying to control Word. (sometimes that happens in this Excel forum where Word vba questions are asked)
VBA Code:
'Select pages 3 to 9
Dim rgePages As Word.Range
wdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3
Set rgePages = wdApp.Selection.Range
wdApp.Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9
rgePages.End = wdApp.Selection.Bookmarks("\Page").Range.End
rgePages.Select

'Justitfy pages 3 to 9
wdApp.Selection.Paragraphs.Alignment = 3
wdApp.Selection.Collapse
I'm not sure if adding wdApp in front of Selection is required, but it might help. Anyway, adding Word in front of Range for rgePages is required. Otherwise, Excel thinks it's an Excel range.
 
Upvote 0
Solution
Sorry for that: I type the VBA in Excel and supposed it was "Excel VBA".
Thank you, I don't have any error message anymore.
But it seems the formatting doesn't stop page 9 and continue to the end of the document.
How to be sure to stop it page 9?

Thank you in advance,

THC
 
Upvote 0
I don't know how your document is set up. If you manually select pages 3-9 and apply justification, does it work?

There can be a scenario where the formatting continues because the paragraphs are separated by a soft return (i.e., Shift + Enter) rather than a hard return (Enter only).
 
Upvote 0
Hello,

I couldn't find what is the problem with the Word document.
Finally, I made the selection from one bookmark to another.
I used "Word.range" and "selection.collapse" corrections to make it work.
Thank you for your help,

THC
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top