Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
there is a ten pages word file. I want to split this word into ten small words file?

is there any method to do this? it is required to use c# . not VBA!

I find some vba solutions, but i am not familiar with vba. is there any method to translate vba to c#????

VBA

Use:
VB
' splitter Macro 
' Macro created 16-08-98 by Doug Robbins to save each page of a document 
' as a separate file with the name Page#.DOC 
' modified 28-09-2010 to use filename of the last 9 digits of the first paragraph
Dim Counter As Long, Source As Document, Target As Document 
Dim Docname As Range 
Set Source = ActiveDocument 
Selection.HomeKey Unit:=wdStory 
Pages = Source.BuiltInDocumentProperties(wdPropertyPages) 
Counter = 0 
While Counter < Pages 
    Counter = Counter + 1 
    Docname = "Page" & Format(Counter) 
    Source.Bookmarks("\Page").Range.Cut 
    Set Target = Documents.Add 
    Target.Range.Paste 
    Set Docname = Target.Range.Paragraphs(1).Range 
    Docname.Start = Docname.End - 10 
    Docname.End = Docname.End - 1 
    Target.SaveAs filename:=Docname.Text 
    Target.Close 
Wend
Posted
Updated 15-May-14 19:59pm
v2

method to translate from VBA to C# ? yeah, right, its called good hard work (its not that hard really)

Here's a link to a similar question on SO that may help you get started with the Object Model etc for document manipulation in Word - some things are easy, ymmv on splitting by page

http://stackoverflow.com/questions/11754608/how-to-split-pages-of-a-word-document-into-separate-files-in-c-sharp[^]
 
Share this answer
 
Comments
wangerpang 16-May-14 2:55am    
first,thank for your help!

I copy the codes of website which you provide above,but these codes do not work! (in fact,these codes can not be complied!!!)

how to use these codes correctly? may you create a entirely project which can be run correctly for me? thanks a lot!
Garth J Lancaster 16-May-14 6:23am    
Im sorry, that's not what we do at CP
wangerpang 16-May-14 7:45am    
the codes below can not be complied. are these codes comply with c#? I do not know how to use them???

using Microsoft.Office.Interop.Word;
//for older versions of Word use:
//using Word;

namespace WordSplitter {
class Program {
static void Main(string[] args) {
//Create a new instance of Word
var app = new Application();

//Show the Word instance.
//If the code runs too slowly, you can show the application at the end of the program
//Make sure it works properly first; otherwise, you'll get an error in a hidden window
//(If it still runs too slowly, there are a few other ways to reduce screen updating)
app.Visible = true;

//We need a reference to the source document
//It should be possible to get a reference to an open Word document, but I haven't tried it
var doc = app.Documents.Open(@"path\to\file.doc");
//(Can also use .docx)

int pageCount = doc.Range().Information[WdInformation.wdNumberOfPagesInDocument];

//We'll hold the start position of each page here
int pageStart = 0;

for (int currentPageIndex = 1; currentPageIndex <= pageCount; currentPageIndex++) {
//This Range object will contain each page.
var page = doc.Range(pageStart);

//Generally, the end of the current page is 1 character before the start of the next.
//However, we need to handle the last page -- since there is no next page, the
//GoTo method will move to the *start* of the last page.
if (currentPageIndex < pageCount) {
//page.GoTo returns a new Range object, leaving the page object unaffected
page.End = page.GoTo(
What: WdGoToItem.wdGoToPage,
Which: WdGoToDirection.wdGoToAbsolute,
Count: currentPageIndex + 1
).Start - 1;
} else {
page.End = doc.Range().End;
}
pageStart = page.End + 1;

//Copy and paste the contents of the Range into a new document
page.Copy();
var doc2 = app.Documents.Add();
doc2.Range().Paste();
}
}
}
}
Garth J Lancaster 16-May-14 19:59pm    
>> "the codes below can not be complied"

what does that mean - what/where is the error you get ? we cant see your screen, so that statement is useless to us

How have you started your project - I suggest you generate a fresh C# console mode project - for the moment, leave out all the code except getting the pagecount - once you can get the pagecount, then continue
also have a look at this as a starter to at least get the number of pages - if you can at least do that, you know your project is set up correctly and you're talking to word - which is half the battle


[^]
 
Share this answer
 
hi,

As far as I know,it can;t be convert VBA to C# directly.But it is possible converting VBA to VB.NET, then to C#.
This way seems a little bit complicated.
 
Share this answer
 

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