Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:03:24+00:00 2026-06-01T12:03:24+00:00

Share your ideas please! I have issue to check the folder and convert a

  • 0

Share your ideas please! I have issue to check the folder and convert a set of documents with different extensions in the PDFs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {

            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object oMissing = System.Reflection.Missing.Value;
            word.Visible = false;
            word.ScreenUpdating = false;


            object aa = WdOpenFormat.wdOpenFormatAuto;
            string errorMessage = null;


            word.DisplayAlerts = WdAlertLevel.wdAlertsNone;

            //selection extension 

            var allExtentionGroupFiles = Directory.GetFiles(@"C:\path", "*.*").
                Where(s=>!s.Contains("~$") && (s.EndsWith(".docx") 
                || s.EndsWith(".doc")
                || s.EndsWith(".docm")
                || s.EndsWith(".dotx")
                || s.EndsWith(".dotm")
                || s.EndsWith(".dot")
                || s.EndsWith(".mht")
                || s.EndsWith(".mhtml")
                || s.EndsWith(".rtf")
                || s.EndsWith(".txt")
                || s.EndsWith(".xml")
                || s.EndsWith(".odt")
                || s.EndsWith(".wps"))).
                GroupBy(s=>s.Substring(s.LastIndexOf('.'))).OrderBy(s=>s.Key);

            foreach (var currentExtentionGroup in allExtentionGroupFiles)
            {

                Console.WriteLine("-->>{0}", currentExtentionGroup.Key);
                foreach (var currentDoc in currentExtentionGroup)
                {

                    Object filename = (Object)currentDoc;



                    try
                    {
                        //open current document

                        Document document = word.Documents.Open(filename,ConfirmConversions:aa,OpenAndRepair:true,Revert:true);

                        document.Activate();


                        object outputFileName = currentDoc.Replace(currentExtentionGroup.Key, ".pdf").Insert(10, "TEST");
                        object fileFormat = WdSaveFormat.wdFormatPDF;


                        document.SaveAs(ref outputFileName,
                        ref fileFormat, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);



                        document.Close();

                    }
                    catch (Exception e1)
                    {
                        errorMessage = e1.ToString();

                    }
                }
            }

word.Quit();

}

    }
}

code is working, the problem is that when i open a doc, or any permitted extentions all work right, but let’s say someone changed the extension for an example DoSomething.exe DoSomething.doc on or in the folder c: \ path is corrupted document Word to stop responding and when I try to open this file manually appear a modal window File Conversion. What to do in this case

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:03:25+00:00Added an answer on June 1, 2026 at 12:03 pm

    I had similar situations – one solution was to create a subprocess with 2 threads, one interacting with Word and the other one being a “watchdog”… the “watchdog” thread would repeatedly check whether some modal window came up and whether a pre-defined timeout was… in any of these cases it would kill the word process, then wait for the other thread to end – if the other thread did not end within a pre-defined time it would kill the other thread…

    This worked ok BUT I observed in same situations that killing word the hard way lead to some unpleasent side-effect ranging from temporary files not being cleaned to some word settings getting trashed…

    I ended up using a 3rd-party library for this conversion which did not need Word being installed at all.. I am very happy with the solution, it performs much better and in case of problematic documents I get an exception which I can handle accordingly… the library I am using is a commercial one… if that is an option for you I can provide a link…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please, share your favorite links on this language where one can learn the best
Could you share your frequently used tricks to search the web for programming related
Is it ok to share your Trello developer API key that is generated at
Can you share the way you setup your unit test projects within your .net
In addition to the datastore for your specific site, can you also share one
I use tumblr to share videos and, unfortunately, when re-posting a video to your
Please share how you do version control for Lotus Notes/Domino development. I want to
Can someone share their experiences with s#arp architecture. we have decided to follow mvp
I have a one String which will be access by four different threads in
at our company we have some drupal projects that share some custom developed modules,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.