Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Code for mail merge
C#
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

wordApp.Visible = false;
wordApp.ScreenUpdating = false;//Will speed up the process

object filename = path; // input

// file to
object newFileName = @"~\Quotation\Quotation\document_to_read_tomorrow1.pdf"; // output
object missing = System.Type.Missing;

Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Add(Template: filename);

foreach (Microsoft.Office.Interop.Word.Field field in doc.Fields)
{

	string fieldName =GetFieldName( field.Code.Text);
	if (dataToWrite.ContainsKey(fieldName))
	{
		field.Select();
		string value = dataToWrite[fieldName].ToString();
		wordApp.Selection.TypeText(value);
	}
}

// formt to save the file, this case PDF
object formatoArquivo = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

// changes in paper size

doc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;

// changes orietation paper
doc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;

// other changes
//doc.PageSetup.LeftMargin = 20;
//doc.PageSetup.RightMargin = 0;


// save file

doc.SaveAs(ref newFileName, ref formatoArquivo, 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, ref missing);

//doc.Close(ref missing, ref missing, ref missing);

object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChanges, ref missing, ref missing);
doc = null;


// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)wordApp).Quit(ref missing, ref missing, ref missing);
wordApp = null;
return newFileName.ToString();

when i run in my local system its fine but when i run in server it throwing error
C#
Could not load file or assembly 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.


Then I loaded the assembly to bin folder then I got an error
C#
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154.



[Edit member="Tadit"]
Added pre tags.
[/Edit]
Posted
v2
Comments
[no name] 29-Apr-14 3:51am    
are you sure that the COM object is installed in your server?

1.Check in Visual Studio the target platform of your project to be Win32 or
Win64 based on your testing system!

2.So you have to modify the "project properties -> Build tab -> Platform target" to be the same like your test machine (in your case Win64), then rebuild and reinstall.
 
Share this answer
 
v2
Comments
dineshkumar51 29-Apr-14 6:31am    
any easy way to check it
Raul Iloc 29-Apr-14 6:57am    
Check in the project propertiis -> "Build" tab -> Platform target
dineshkumar51 29-Apr-14 7:13am    
its x86
Raul Iloc 29-Apr-14 7:15am    
OK, and the Windows used on your test machine is Win64 version ?
dineshkumar51 29-Apr-14 10:27am    
its is win 64
For this Error
"Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154."

Seems your COM component is not installed and you need to register it using Regsvr-32 -i "path of the file" (in the RUN program - Window + R) or command prompt of the server.
 
Share this answer
 
v3
Comments
dineshkumar51 29-Apr-14 6:35am    
I dont have access to do that in server. I host my website in 3rd part server
You have to have Microsoft Word installed on the Server running your Application.
 
Share this answer
 
v2
Comments
dineshkumar51 29-Apr-14 6:30am    
The server is installed with office 2008
As you are using Version=14.0.0.0, so you need to install Office 2010.
dineshkumar51 29-Apr-14 6:53am    
yes I am using version 14.0.0.0 but in server I have only office 2008. I host the server so i don't have permission to upgrade it
dineshkumar51 29-Apr-14 8:33am    
I tried to find out library which I can use but I couldn't find clear answer.
So can you help me please?
Sorry my mistake. There is no such 2008 versions. Are you referring to 2007 version?

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