Click here to Skip to main content
15,888,143 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have my application written using C#.net and open XML SDK(2.0).I have splitted the word file paragraph wise and section wise using open XML tags.But i could not find anything about split a word file page wise...

Pleas Guide me get out of this issue.
Posted
Updated 29-Mar-11 19:09pm
v2

1 solution

Here[^] is the VB way, you have to change it to C#.

VB
Dim WordApp As New Word.Application
       Dim BaseDoc As Word.Document
       Dim DestDoc As Word.Document
       Dim intNumberOfPages As Integer
       Dim intNumberOfChars As String
       Dim intPage As Integer

       'Word Constants
       Const wdGoToPage = 1
       Const wdStory = 6
       Const wdExtend = 1
       Const wdCharacter = 1

       'Show WordApp
       WordApp.ShowMe()

       'Load Base Document
       BaseDoc = WordApp.Documents.Open(Filename)
       BaseDoc.Repaginate()

       'Loop through pages
       intNumberOfPages = BaseDoc.BuiltInDocumentProperties("Number of Pages").value
       intNumberOfChars = BaseDoc.BuiltInDocumentProperties("Number of Characters").value

       For intPage = 1 To intNumberOfPages
           If intPage = intNumberOfPages Then
               WordApp.Selection.EndKey(wdStory)
           Else
               WordApp.Selection.GoTo(wdGoToPage, 2)
               Application.DoEvents()

               WordApp.Selection.MoveLeft(Unit:=wdCharacter, Count:=1)
           End If

           Application.DoEvents()

           WordApp.Selection.HomeKey(wdStory, wdExtend)
           Application.DoEvents()

           WordApp.Selection.Copy()
           Application.DoEvents()

           'Create New Document
           DestDoc = WordApp.Documents.Add
           DestDoc.Activate()
           WordApp.Selection.Paste()
           DestDoc.SaveAs(NewFileName & intPage.ToString & ".doc")
           DestDoc.Close()
           DestDoc = Nothing

           WordApp.Selection.GoTo(wdGoToPage, 2)
           Application.DoEvents()

           WordApp.Selection.HomeKey(wdStory, wdExtend)
           Application.DoEvents()

           WordApp.Selection.Delete()
           Application.DoEvents()
       Next

       BaseDoc.Close(False)
       BaseDoc = Nothing

       WordApp.Quit()
       WordApp = Nothing
   End Sub
 
Share this answer
 
v2
Comments
saravanan6 30-Mar-11 1:26am    
Thanks Prerak Patel, Please let me convert it into C# and check it...

It is greatful to me...

Saravanan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900