Open File Dialog and Save File as XHTML

possible without listing whole save path?

Page 1 of 1

4 Replies - 5450 Views - Last Post: 22 October 2010 - 03:35 AM Rate Topic: -----

#1 Guest_jojo_90*


Reputation:

Open File Dialog and Save File as XHTML

Posted 21 October 2010 - 05:24 AM

Hey, I'm wondering if you can help me. I have a c# program where the user selects a file (MS Word doc) from a FileOpen Dialog. Then clicks a button to open the file and convert it automatically to XHTML and save the file in the specified output location. I've got this to work if I specify the full save path like:

 fileName = @"D:\test.html"; 


but if I do not include the filename 'test.html' it won't save the file. As the program is based on user input I want to extract the file path from the file open dialog and save the file respectively. Is there any possible way to do this? Also, I want to modify the code so the user can select multiple files to convert and save automatically, without having to list the filename in the source code.

Here is my file open code:
public void btnOpenFile_Click(object sender, System.EventArgs e)
		{
			this.fDialog = new OpenFileDialog();
			fDialog.Filter = Properties.Resources.WordDoc;
			fDialog.InitialDirectory = @"D:\input\";
			fDialog.CheckFileExists = true;
			fDialog.AddExtension = true;
			fDialog.DefaultExt = "doc";
			if (fDialog.ShowDialog() == DialogResult.OK)
			{
				sfileName = fDialog.FileName; 
sPath = Path.GetFileName(fDialog.FileName);

				if (sPath == String.Empty)
					return;
			}
		}



Here is the problem code:
                else if (result == DialogResult.Yes)
                {
                    object fileName = sfileName;.
                    object readOnly = false;
                    object isVisible = true;
                    object missing = System.Reflection.Missing.Value;
                    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                    Microsoft.Office.Interop.Word.Document aDoc = new Microsoft.Office.Interop.Word.Document();
                    aDoc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                    app.Visible = true;
                    try
                    {
                        aDoc = app.Documents.Open(ref fileName, ref readOnly, ref missing, ref missing,
                                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
                        aDoc.Activate();
                        fileName = @"D:\test.html";
                        object fileformat = Word.WdSaveFormat.wdFormatFilteredHTML;
                            aDoc.SaveAs(ref fileName, ref fileformat, 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);
                            aDoc.Close(ref missing, ref missing, ref missing);


Is This A Good Question/Topic? 0

Replies To: Open File Dialog and Save File as XHTML

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: Open File Dialog and Save File as XHTML

Posted 21 October 2010 - 06:44 AM

Have you tried using a SaveFileDialog instead?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_jojo_90*


Reputation:

Re: Open File Dialog and Save File as XHTML

Posted 21 October 2010 - 07:04 AM

Yes, but if I'm trying to save multiple files (with different filenames) as XHTML files at the same time a save file dialog is not much use to me.
Was This Post Helpful? 0

#4 Guest_jojo_90*


Reputation:

Re: Open File Dialog and Save File as XHTML

Posted 22 October 2010 - 03:31 AM

Oh never mind, I got it to work when I changed this line
object fileformat = Word.WdSaveFormat.wdFormatFilteredHTML;

to
object fileformat = Word.WdSaveFormat.wdFormatHTML;

Was This Post Helpful? 0

#5 Guest_jojo_90*


Reputation:

Re: Open File Dialog and Save File as XHTML

Posted 22 October 2010 - 03:35 AM

Is there a way to use something like this for fileopendialog? Or would I have to use fileupload?

public static FileInfo saveFileWithoutOverwrite(OpenFileDialog fDialog, string saveTo)
        {
            int count = 1;
            string fileName = fDialog.FileName;
            string[] fileNameSplit = fileName.Split(new char[] { '.' });
            string ext = "." + fileNameSplit[fileNameSplit.Length - 1];
            string prefix = fileName.Substring(0, fileName.Length - ext.Length);
            while (File.Exists(saveTo + fileName))
            {
                fileName = prefix + "[" + count.ToString() + "]" + ext;
                count++;
            }
//obviously can't save it here as it's a fileopen?
            fDialog. (saveTo + fileName);
            return new FileInfo(saveTo + fileName);

Was This Post Helpful? 0

Page 1 of 1