Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-08-2017, 12:26 PM
nullLF nullLF is offline Remove pages from a .docx Windows 10 Remove pages from a .docx Office 2010 64bit
Novice
Remove pages from a .docx
 
Join Date: Aug 2017
Posts: 1
nullLF is on a distinguished road
Unhappy Remove pages from a .docx

Hi guys,


I am have a small problem which I was unable to solve for days now. I would be glad if you could give me some hints. I am somewhat experienced in VBA for Excel and Access. But this time I am struggling with Word.
Here is what I have:
Imagine I have a set of word documents where certain parts of the document need to be removed. I know the files and I know what needs removal. Luckily it is always a full page or multiple pages that need to go.
So I know how to parse through the files and so on. But I can not make a successful full page delete.
I try like this:
Code:
Public Sub test()
    Dim x As Document
    
    ActiveDocument.SaveAs "e:\temp\test.docm" ' please don't mind this, it is just here for testing
    Set x = Documents.Open("e:\temp\test.docm") ' this also
    ' imagine I want to everything before Page 3 and after Page 5
    DeleteBefore x, 3
    DeleteAfter x, 2
    x.Save
End Sub


Public Sub DeleteBefore(ByRef doc As Document, ByVal page_num As Long)
    Dim Rng As Range
    If page_num <= 1 Then _
        Exit Sub
    With doc
        Set Rng = .GoTo(wdGoToPage, , , 1)
        Set Rng = Rng.GoTo(wdGoToPage, , , page_num)
        Rng.Delete
    End With
End Sub

Public Sub DeleteAfter(ByRef doc As Document, ByVal page_num As Long)
    Dim Rng      As Range
    Dim max_page As Long
    max_page = doc.Content.ComputeStatistics(wdStatisticPages)
    If page_num >= max_page Then _
        Exit Sub
    With doc
        Set Rng = .GoTo(wdGoToPage, , , page_num + 1)
        Set Rng = Rng.GoTo(wdGoToPage, , , max_page)
        Rng.Delete
    End With
End Sub
Can anyone please give me some pointers how I can solve my riddle? I just need a few hints, I can do the digging myself then

Thanks O.o
nullLF
Reply With Quote
  #2  
Old 08-08-2017, 09:19 PM
gmayor's Avatar
gmayor gmayor is offline Remove pages from a .docx Windows 10 Remove pages from a .docx Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There are a number of issues here:

Word is not a page layout application. There are no 'pages' in a Word document. The displayed pages are achieved by text flow and manipulation those pages will alter the text flow.

You have started by saving the document then opened it (it was already open). Then you have processed the document, before saving it again, thus overwriting the original. You might as well just save at the end - unless you saveas with a different name.

You have set the range to a location, then set it again to another location, when what you need to do is set the range to a location then move the start and/or end of the range to another location as appropriate.

The following should be closer:

Code:
Option Explicit

Public Sub Test()
Dim x As Document
    Set x = ActiveDocument
    ' imagine I want to everything before Page 3 and after Page 5
    DeleteBefore x, 3
    DeleteAfter x, 2
    x.SaveAs "e:\temp\test.docm"    ' please don't mind this, it is just here for testing
End Sub


Public Sub DeleteBefore(ByRef doc As Document, ByVal page_num As Long)
Dim Rng As Range
    If page_num <= 1 Then _
       Exit Sub
    Set Rng = doc.Range 'set a range to the document body
    Rng.Collapse 1    'collapse the range to the start of doc
    Selection.GoTo wdGoToPage, , , page_num 'find the required page
    Rng.End = doc.Bookmarks("\page").Range.Start 'set the start of the range to the start of that page
    'Rng.Select 'for testing with only one of the processes
    Rng.Delete 'delete the range
End Sub

Public Sub DeleteAfter(ByRef doc As Document, ByVal page_num As Long)
Dim Rng As Range
Dim max_page As Long
    max_page = doc.Content.ComputeStatistics(wdStatisticPages)
    If page_num >= max_page Then _
       Exit Sub
    Set Rng = doc.Range
    Rng.Collapse 1    'the start of doc
    Selection.GoTo wdGoToPage, , , page_num + 1
    Rng.Start = doc.Bookmarks("\page").Range.Start
    Rng.End = doc.Range.End
    'Rng.Select
    Rng.Delete
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Numbering with heading is mixed up in odd pages in regard to even pages Baflla Word 0 09-11-2016 05:25 AM
Multiple Master Pages dont sync with content pages generatorjoe Publisher 0 07-28-2016 10:12 AM
Remove Compatibility Mode on DOCX files (batch) w64bit Word 17 02-01-2015 06:02 AM
Remove pages from a .docx Advanced page numbering: section pages in header, document pages in footer Albus Word 12 12-12-2014 01:36 PM
How to remove password from .docx file? erik2282 Word 3 05-29-2012 05:44 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:32 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft