Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I am converting word file to pdf by following code:
using System;

namespace DocConvert

{
      class DoctoRtf
      {
            static void Main()
            {

                //Creating the instance of Word Application
                Word.Application newApp = new Word.Application();

                // specifying the Source & Target file names
                object Source="c:\\abc\\Source.doc";
                object Target="c:\\abc\\Target.rtf";

                // Use for the parameter whose type are not known or
                // say Missing
                object Unknown =Type.Missing;

                // Source document open here
                // Additional Parameters are not known so that are
                // set as a missing type
                newApp.Documents.Open(ref Source,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);

        // Specifying the format in which you want the output file
                object format = Word.WdSaveFormat.wdFormatRTF;

        //Changing the format of the document
                newApp.ActiveDocument.SaveAs(ref Target,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);

                // for closing the application
                newApp.Quit(ref Unknown,ref Unknown,ref Unknown);

            }
      }
}

But at the point of:
newApp.ActiveDocument.SaveAs(ref Target,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);

I am getting an error saying value out of range. can any body help me?
Posted
Updated 8-May-11 21:49pm
v2
Comments
Sergey Alexandrovich Kryukov 9-May-11 3:49am    
You did not report exception, did not even call it exception...
--SA
CodeHawkz 9-May-11 3:55am    
@SA: did not even call it exception... I am angry with you << lol!

No offense, you just sounded as if you are a child. Helped me laugh a bit at work :)
[no name] 9-May-11 4:11am    
Ok I am saying it a COM Exeption. The code is working fine for rtf format. but for pdf it show an exeption. the message is "value out of range".

Now can you help me.

If you dont know please dont lough.
CodeHawkz 9-May-11 4:17am    
Lolz :) I am laughing at SAKryukov not you.

I don't laugh or make fun of people because, they don't know something. I am not that low. Anyways, my bad, I should have posted this as a reply to SA's comment, instead I just hit d "Add Comment" link.
Richard MacCutchan 9-May-11 4:32am    
Check the documentation for the SaveAs method and check that all your parameters are valid. Also your title and introduction says you are converting to PDF but your file type is set as RTF.

1 solution

You can't directly use SaveAs to convert to pdf. You can convert .doc to .pdf programatically using PDFCreator*.

*For this to work, you need to have PDFCreator installed on machine.

C#
private PDFCreator.clsPDFCreator m_PDFCreator;
private void CreatePDF(string filename)
{
	//Check that the file is printable
	if (!m_PDFCreator.cIsPrintable(filename)) {
		throw new Exception(filename + " is not printable.");
	}
	//Check that the file exists
	if (System.IO.File.Exists(filename)) {
		string strDefaultPrinter = null;
		strDefaultPrinter = m_PDFCreator.cDefaultPrinter;
		m_PDFCreator.cDefaultPrinter = "PDFCreator";
		if (m_PDFCreator.cPrinterStop)
			m_PDFCreator.cPrinterStop = false;
		m_PDFCreator.cPrintFile(filename);
		m_PDFCreator.cDefaultPrinter = strDefaultPrinter;
	} else {
		throw new Exception(filename + " does not exist.");
	}
}


in VB:
VB
  Private m_PDFCreator As PDFCreator.clsPDFCreator

  Private Sub CreatePDF(ByVal filename As String)
  'Check that the file is printable
  If Not m_PDFCreator.cIsPrintable(filename) Then
    Throw New Exception(filename & " is not printable.")
  End If
  'Check that the file exists
  If System.IO.File.Exists(filename) Then
    Dim strDefaultPrinter As String
    strDefaultPrinter = m_PDFCreator.cDefaultPrinter
    m_PDFCreator.cDefaultPrinter = "PDFCreator"
    If m_PDFCreator.cPrinterStop Then m_PDFCreator.cPrinterStop = False
    m_PDFCreator.cPrintFile(filename)
    m_PDFCreator.cDefaultPrinter = strDefaultPrinter
  Else
    Throw New Exception(filename & " does not exist.")
  End If
End Sub
 
Share this answer
 
v2
Comments
[no name] 11-May-11 0:31am    
I have installed pdfcreator. Now wich dll ishould add as refference?
Prerak Patel 11-May-11 23:48pm    
Add COM reference to PDFCreator
[no name] 11-May-11 23:56pm    
Sorry to ask,but is your code complet? I ahave added refference and added your code but i think some thing more to write.

I will be very happy if you can suggest complete solution.

Anywaya whatever you suggested already many many thanks for that.

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