Question Blank pages

Erjola6

New member
Joined
Feb 16, 2022
Messages
4
Programming Experience
Beginner
Hi everyone!
this is the code that I have applied for deleting blank pages into the word.
The problem is that this code only works for *docx documents ,and I want the code to be used also for the *doc documents but it do not work for them .

CAn someone help me ?

Thank you,
Erjola
C#:
String wordPath = @Path;

Microsoft.Office.Interop.Word.Application wordapp = null;
Microsoft.Office.Interop.Word.Document doc = null;
Microsoft.Office.Interop.Word.Paragraphs paragraphs = null;

try
{
    // Start Word APllication and set it be invisible
    wordapp = new Microsoft.Office.Interop.Word.Application();
    wordapp.Visible = false;
    doc = wordapp.Documents.Open(wordPath);
    paragraphs = doc.Paragraphs;

    object miss = System.Reflection.Missing.Value;

    WdStatistic stat1 = WdStatistic.wdStatisticPages;
    int numberOfPages = doc.ComputeStatistics(stat1, ref miss);

    var document1 = wordapp.ActiveDocument;

    for (int i = 1; i < numberOfPages; i++)
    {
        //Console.WriteLine(i);

        document1.ActiveWindow.Selection.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, i, miss);

        //Console.WriteLine("-> TEXT : " + document1.ActiveWindow.Selection.GoTo(WdGoToItem.wdGoToBookmark, miss, miss, "\\page").Text);
        //Console.WriteLine("-> longitud : " + document1.ActiveWindow.Selection.Text.Trim().Length);

        if (document1.ActiveWindow.Selection.Text.Trim().Length <= 1)
        {
            foreach (Paragraph paragraph in document1.ActiveWindow.Selection.Paragraphs)
            {
                if (paragraph.Range.Text.Trim() == string.Empty)
                {
                    paragraph.Range.Select();
                    wordapp.Selection.Delete();
                }
            }
        }
    }

    // Save the document and close document
    doc.Save();
    ((Microsoft.Office.Interop.Word._Document)doc).Close();

    // Quit the word application
    ((Microsoft.Office.Interop.Word._Application)wordapp).Quit();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    // Clean up the unmanaged Word COM resources by explicitly
    // call Marshal.FinalReleaseComObject on all accessor objects
    if (paragraphs != null)
    {
        Marshal.FinalReleaseComObject(paragraphs);
        paragraphs = null;
    }
    if (doc != null)
    {
        Marshal.FinalReleaseComObject(doc);
        doc = null;
    }
    if (wordapp != null)
    {
        Marshal.FinalReleaseComObject(wordapp);
        wordapp = null;
    }
}
 
Last edited by a moderator:
What error are you getting when working with .doc files?
 
And did you step through the code you see what the difference in behavior is? Is an exception being thrown? Is the number of pages not being counted?
 
We normally don't delete threads in this forum. People may claim GDPR but that is only to have the accounts removed/anonymized, but the threads will still remain.
 
If the code above is supposedly copyrighted by BluePrism, international law dictates that it should have an appropriate copyright notice on it. There is no such copyright notice on post #1.
 
Back
Top Bottom