none
question in word automation RRS feed

  • Question

  •  

    Q: i have file (***.docx) and i want separate it based on title.



    how can i do it by using word automation.

    I mean
     In my document i have more than one title I'need to extract each title in tO new document by word automation

    example:
    ...........................................
    title 1

    title 2

    title 3
    ........................................... in one document


    in need to extract each title in to new document by using word automation
    ..........
    title 1
    ..........doc1

    ..........
    title 2
    ..........doc2

    ..........
    title 3
    ..........doc3



    thanks

    Thursday, December 4, 2008 9:36 PM

All replies

  • Hi,

    What makes a title a title in your documents? Are titles formatted using a certain style?

    You can then use following VBA macro function to find the first occurence of a style in a given range:

    Public Function FindStyleInRange(oRange As Range, varStyle As VariantOptional bFindReverse As Boolean = FalseAs Range 
         
        Dim oRangeCpy As Range 
         
        On Error GoTo errHandler 
         
        Set oRangeCpy = oRange.Duplicate 
        With oRangeCpy.Find 
            .ClearFormatting 
            .Style = ActiveDocument.Styles(varStyle) 
            .Text = "" 
            .Forward = Not bFindReverse 
            .Format = True 
             
            .Execute 
            If .Found Then 
                Set FindStyleInRange = oRangeCpy 
            Else 
                Set FindStyleInRange = Nothing 
            End If 
        End With 
     
        Exit Function 
     
    errHandler: 
        Set FindStyleInRange = Nothing 
         
    End Function 
     
    Public Sub Main() 
     
        ' find the first title 
        Dim oTitle As Range 
        Set oTitle = FindStyleInRange(ActiveDocument.Range, "Title"
         
        ' find the next title 
        Dim oNextTitle As Range 
        Set oNextTitle = oTitle.Duplicate 
        oNextTitle.Collapse wdCollapseEnd 
        oNextTitle.MoveEnd wdStory 
        Set oNextTitle = FindStyleInRange(oNextTitle, "Title"
     
        ' copy the first article 
        Dim oFirstArticle As Range 
        Set oFirstArticle = oTitle.Duplicate 
        oFirstArticle.End = oNextTitle.Start 
        oFirstArticle.Copy 
     
        ' paste to new document 
        Dim oDoc As Document 
        Set oDoc = Documents.Add 
        oDoc.Range.Paste 
     
    End Sub 
         
     

    Friday, December 5, 2008 9:55 AM
  •             There is another way to find titles:

        object missing = Type.Missing;
                object objVisible = false;
                object objReadOnly = false;
                object objFalse = false;
                object objTrue = true;
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;

                Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
                application.Visible = false;
                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;

        _Document document = application.Documents.Open(ref fileName, ref missing, ref objReadOnly, ref missing,
                           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                           ref missing, ref objVisible, ref missing, ref missing, ref missing, ref missing);

        foreach (Field f in document.Fields)
                        if (f.Type == WdFieldType.wdFieldTitle)
    DO SOMETHING....

                document.SaveAs(ref newFileName, ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                ref missing, ref missing, ref missing, ref missing, ref missing);
                    
               document.Close(ref missing, ref missing, ref missing);


      object saveOptions = WdSaveOptions.wdDoNotSaveChanges; 
              application.Quit(ref saveOptions, ref missing, ref missing);
    Friday, December 5, 2008 10:52 AM