Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, i able to convert single doc to htm from the link
How to convert DOC into other formats using C#[^]
But i seeking help to convert in batch, if there are 10 or more files in a source folder the programme should convert them to htm with same file names.

Kindly HELP
Posted

What about making a method of it and calling it in a loop? For parsing folder see this sample: http://www.dotnetperls.com/directory-getfiles[^]
 
Share this answer
 
Thanks to Zoltán Zörgő :), Done it !!!!!!.

Below is the code

C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.IO;


namespace DocToHtm
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            
            string dir = @"C:\CONVERT\";
            string dirTar = @"C:\CONVERT\DOC FILES\";
            int take = Directory.GetFiles(dir, "*.doc", SearchOption.TopDirectoryOnly).Length;

            MessageBox.Show(take.ToString());
            string[] array = Directory.GetFiles(dir, "*.doc");

            if (take > 0)
            {
                try
                {
                    foreach (string name in array)
                    {
                        Word._Application newApp = new Word.Application();
                        string filename = Path.GetFileName(name);
                        MessageBox.Show(filename);
                        string filenoext = Path.GetFileNameWithoutExtension(name);
                        object Sourcepath = name.ToString();
                        if (Directory.Exists(dirTar))
                        {
                            object TargetPath = @"C:\CONVERT\" + filenoext;

                            object Unknown = Type.Missing;

                            Word.Documents d = newApp.Documents;

                            Word.Document od = d.Open(ref Sourcepath, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                            object format = Word.WdSaveFormat.wdFormatHTML;

                            newApp.ActiveDocument.SaveAs(ref TargetPath, ref format,
                                        ref Unknown, ref Unknown, ref Unknown,
                                        ref Unknown, ref Unknown, ref Unknown,
                                        ref Unknown, ref Unknown, ref Unknown,
                                        ref Unknown, ref Unknown, ref Unknown,
                                        ref Unknown, ref Unknown);

                            newApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges);                            
                            newApp.Quit();

                            File.Move(Sourcepath.ToString(), dirTar + filename);

                            MessageBox.Show("Done ");

                        }

                        else
                        {
                            Directory.CreateDirectory(@"C:\CONVERT\DOC FILES");

                            object TargetPath = @"C:\CONVERT\DOC FILES\" + filenoext;


                            object Unknown = Type.Missing;

                            Word.Documents d = newApp.Documents;
                            Word.Document od = d.Open(ref Sourcepath, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown, ref Unknown);


                            object format = Word.WdSaveFormat.wdFormatHTML;

                            newApp.ActiveDocument.SaveAs(ref TargetPath, ref format,
                                        ref Unknown, ref Unknown, ref Unknown,
                                        ref Unknown, ref Unknown, ref Unknown,
                                        ref Unknown, ref Unknown, ref Unknown,
                                        ref Unknown, ref Unknown, ref Unknown,
                                        ref Unknown, ref Unknown);
                            newApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges);

                            newApp.Quit();

                            File.Move(Sourcepath.ToString(), dirTar + filename);

                            MessageBox.Show("Done ");
                        }
                    }
                }
                catch (Exception ConvertError)
                {
                    MessageBox.Show("Error Description : " + ConvertError);
                }
            }

            else
            {
                MessageBox.Show("Documents not found !");
            }
        }        
    }
}
 
Share this answer
 
Comments
Zoltán Zörgő 13-Jul-12 2:08am    
And you are thanking me by downvoting my answer? :(
jaipe 13-Jul-12 2:10am    
Sorry, updated it now :)

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