Delete Page Break in word using VBA in excel

excelpunk

Board Regular
Joined
May 6, 2011
Messages
165
Hello All!

I have a word template which is of 2 pages, it has just one page break, I have vba code in Excel, which opens this template, manipulates something and then saves the template with a different name.

However, I have now realized that in some cases I need to delete that page break. What vba code should I include to make this happen.

My apologies if the post is not relevant.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,
Manual PageBreak in Word is actually CHR(12) symbol.
Find CHR(12) and replace it by empty string "" like this:
Rich (BB code):
Sub DeletePageBreak()
  
  Const MyFile = "C:\Temp\Test.dot"  ' <-- Change to suit
  
  Const wdReplaceOne = 1, wdReplaceAll = 2
  
  Dim objWord As Object, objDoc As Object
      
  ' Get/Create Word Application object using late binding method
  On Error Resume Next
  Set objWord = GetObject(, "Word.Application")
  If Err <> 0 Then Set objWord = CreateObject("Word.Application"): Err.Clear
  
  ' Open MyFile
  Set objDoc = objWord.Documents.Open(MyFile)
  If Err <> 0 Then MsgBox "Can't open: " & MyFile, vbExclamation: GoTo exit_
  On Error GoTo 0
  
  ' Activate Microsoft Word window
  objWord.Visible = True
  objWord.Tasks("Microsoft Word").Activate
  
  ' ... Do something else ...
  
  ' Delete PageBreak
  With objWord.ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = Chr(12)
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .Execute Replace:=wdReplaceOne  ' <-- Use wdReplaceAll if required
  End With
  
exit_:
  ' Release the memory
  Set objDoc = Nothing
  Set objWord = Nothing

End Sub
 
Upvote 0
This looks more strictly: With objDoc.Content.Find
than: With objWord.ActiveDocument.Content.Find
But the result is the same.
 
Upvote 0
Thank you! It worked like charm! It may be a very simple piece of code to you but it saved me a lot of time and energy. Thanks again!
 
Upvote 0
Thank you! It worked like charm! It may be a very simple piece of code to you but it saved me a lot of time and energy. Thanks again!
Glad it helped you!
(y)
 
Upvote 0
Hi ZVI!

I guess I have to trouble you again! :(

Can you please help me out how to sort a table in word using vba from Excel?

I recorded a macro in word and then tried to use it from Excel, I dont know why but it keep throwing error!

This is how my code looks:

with wordtemp.activedocument.tables(1).Select
'followed by the sort macro which I got after recording...

I was just wondering what is going wrong! Controling word from excel is a difficult task isint it? :(
 
Upvote 0
Hi excelpunk,

See below the sorting section in Red:
Rich (BB code):
Sub DeletePageBreak_SortTables1()
  
  Const MyFile = "C:\Temp\Test.dot"  ' <-- Change to suit
  
  Const wdReplaceOne = 1, wdReplaceAll = 2, wdSortOrderAscending = 0
  
  Dim objWord As Object, objDoc As Object
      
  ' Get/Create Word Application object using late binding method
  On Error Resume Next
  Set objWord = GetObject(, "Word.Application")
  If Err <> 0 Then Set objWord = CreateObject("Word.Application"): Err.Clear
  
  ' Open MyFile
  Set objDoc = objWord.Documents.Open(MyFile)
  If Err <> 0 Then MsgBox "Can't open: " & MyFile, vbExclamation: GoTo exit_
  On Error GoTo 0
  
  ' Activate Microsoft Word window
  objWord.Visible = True
  objWord.Tasks("Microsoft Word").Activate
  
  ' ... Do something else ...
  
  ' Delete PageBreak
   With objDoc.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = Chr(12)
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .Execute Replace:=wdReplaceOne  ' <-- Use wdReplaceAll if required
  End With
  
  ' Sort Tables(1)
  objDoc.Tables(1).Range.Sort ExcludeHeader:=True, FieldNumber:=1, SortOrder:=wdSortOrderAscending
  
exit_:
  ' Release the memory
  Set objDoc = Nothing
  Set objWord = Nothing

End Sub

Regards
 
Last edited:
Upvote 0
Hi! Thanks for the reply. I will test it now but a quick question. My table has 3 columns, so when I read your code it says fieldnumber =1. Does values in other columns get adjusted? I mean, does it sort only column1 or it sorts all the columns?
 
Upvote 0
Thanks again! This worked the way I wanted. Just amazing! Thanks.

Just a couple of queries:

Can you tell me what went wrong with the code I tried with:
with wordtemp.activedocument.tables(1).Select
'followed by recorded macro
Selection.Sort ExcludeHeader:=True, FieldNumber:="Column 1", SortFieldType _
:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending, FieldNumber2 _
:="", SortFieldType2:=wdSortFieldAlphanumeric, SortOrder2:= _
wdSortOrderAscending, FieldNumber3:="", SortFieldType3:= _
wdSortFieldAlphanumeric, SortOrder3:=wdSortOrderAscending, Separator:= _
wdSortSeparateByCommas, SortColumn:=False, CaseSensitive:=False, _
LanguageID:=wdEnglishUS, SubFieldNumber:="Paragraphs", SubFieldNumber2:= _
"Paragraphs", SubFieldNumber3:="Paragraphs"

Also, do you have links to any resources where controling word from Excel is explained?
I dont understand why the recorded macro throws error when I use it from Excel! The same code works fine when I use it in word modules. Strange!
 
Upvote 0
Hi! Thanks for the reply. I will test it now but a quick question. My table has 3 columns, so when I read your code it says fieldnumber =1. Does values in other columns get adjusted? I mean, does it sort only column1 or it sorts all the columns?
In my example all rows of Tables(1) are included in the sorted Range to be sorted by the order of Columns(1) like for the Range of Excel.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,091
Latest member
gaurav_7829

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