Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > C# .NET Create Word Document (Windows 7 and Office 2003 , Office Word 2007)




Clound SSD Virtual Server

C# .NET Create Word Document (Windows 7 and Office 2003 , Office Word 2007)

 
  C# .NET Create Word Document (Windows 7 and Office 2003 , Office Word 2007) ตัวอย่างการใช้ C# ในการสร้างเอกสาร Word (.doc) โดยใช้ Class Library ของ Microsoft.Office.Interop.Word สำหรับตัวอย่างนี้ได้ทำการทดสอบบน Windows 7 และ Office Word 2007 สามารถทำงานได้ปกติ

และสามารถใช้ได้ทั้ง Application ที่เป็น Windows Application (Win App) , Web Application (ASP.NET) และ Console App หรืออื่น ๆ

สำหรับบทความการใช้ ASP.NET กับ Word และอื่น ๆ ตัวอย่างการใช้ สามารถอ่านได้จากหัวข้อ ASP.NET กับ Word (VB.NET)


จากบทความลิ้งค์ก่อนหน้านี้จะเป็นภาษา VB.NET ซึ่งในตัวอย่างได้มีการเรียกใช้งาน Property หลายตัว ซึ่งสามารถนำมาดัดแปลงได้กับภาษา C# ได้อย่างไม่ยาก

C# กับ Word Document

เริ่มต้นด้วยการ Add Reference ตัว Library ชื่อ Microsoft Word 12.0 Object Library หรือ Version อื่น ๆ ที่ได้ติดตั้งลงในเครื่อง (Microsoft Office Word )

C# Office Word 2007 + Windows  7

คลิกขวาที่ Project เลือก Add Reference

C# Office Word 2007 + Windows  7


เลือก Microsoft Word 12.0 Object Library เหมือนในรูป

C# Office Word 2007 + Windows  7

Library ถูก Add ลงเรียบร้อยแล้ว

ตัวอย่าง Code ของภาษา C#

frmWord.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace WordC
{
    public partial class frmWord : Form
    {
        public frmWord()
        {
            InitializeComponent();
        }

        private void Generate_Click(object sender, EventArgs e)
        {

            object mis = Type.Missing;
            object strFileName = (Object)"C:\\WordC\\Doc\\GenWord.doc";
            object fileFormat = 1;

            Object oTrue = true;
            Object oFalse = false;
                   
            Word.Application oWord = new Word.Application();
            Word.Document oWordDoc = new Word.Document();


            oWord.Visible = true;

            oWordDoc = oWord.Documents.Add(ref mis, ref mis, ref mis, ref mis);


            oWord.Selection.TypeText("Welcome To www.ThaiCreate.Com");

            oWordDoc.SaveAs(ref strFileName, ref fileFormat, ref mis, ref mis,
             ref mis, ref mis, ref mis, ref mis, ref mis,
             ref mis, ref mis, ref mis, ref mis, ref mis,
             ref mis, ref mis);

            oWordDoc.Close(ref oFalse, ref mis, ref mis);
            oWord.Quit(ref mis, ref mis, ref mis);
            GC.Collect();
        }
    }
}


Screenshot

C# Office Word 2007 + Windows  7


C# Office Word 2007 + Windows  7








เพิ่มเติมเกี่ยวกับเรื่อง DCOM + Permission

กรณีที่ Error นี้
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005.


ถ้าเป็น Windows XP + Office Word 2003 อ่านได้ที่นี่


DCOM Config (Windows 7 + Office Word 2007)

Control Panel
Control Panel\All Control Panel Items\Administrative Tools\Event Viewer


Event Viewer

C# Office Word 2007 + Windows  7

สังเกตุว่ามี Event Viewer มี Error Log ชื่อว่า DistributedCOM

C# Office Word 2007 + Windows  7

รายละเอียดของ Error
The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID
{000209FF-0000-0000-C000-000000000046}
and APPID
{00020906-0000-0000-C000-000000000046}
to the user WEERACHAI-NB\WEERACHAI SID (S-1-5-21-1205256255-3473717987-3694456053-1001) from address LocalHost (Using LRPC). This security permission can be modified using the Component Services administrative tool.


จะมี User ชื่อว่า WEERACHAI ติด Permission อยู่ เพราะเป็น Windows Form Application จึงใช้ User ที่เป็น Logon User ในการรัน แต่ถ้าเป็น ASP.NET ที่รันด้วย IIS จะใช้ User ว่า IUSR_WEERACHAI และ IWAM_WEERACHAI

การแก้ปัญหา

Component Services
Control Panel\All Control Panel Items\Administrative Tools\Component Services


C# Office Word 2007 + Windows  7

เปิดโปรแกรม Component Services และคลิกเลือกที่ DCOM Config

C# Office Word 2007 + Windows  7

คลิกขวาที่ Microsoft Word 07 - 2003 Document -> Properties

C# Office Word 2007 + Windows  7

ตรง Tab Security และ Launch and Activation Permission ให้เลือก Customize -> Edit ตามรูป

C# Office Word 2007 + Windows  7

คลิกที่ Add

C# Office Word 2007 + Windows  7

คลิกที่ Advanced

C# Office Word 2007 + Windows  7

Find Now และเลือก User ที่ติด Permission ซึ่งในตอนแรกชื่อว่า WEERACHAI

C# Office Word 2007 + Windows  7

เลือก OK

C# Office Word 2007 + Windows  7

กำหนดค่าดังรูป

C# Office Word 2007 + Windows  7

Tab Identity ให้เลือก The interactive user


หลังจากเรียบร้อยแล้ว ก็ให้ทำการทดสอบ Run โปรแกรมใหม่อีกครั้ง และถ้ายังไม่ได้ ให้ลองสังเกตุดูที่ Event Viewer ว่ามี Log แจ้ง Error หรือไม่









       
Bookmark.   
       

 

  By : TC Admin
  Score Rating : -
  Create Date : 2011-09-08 21:55:11
  Download : No files
     

Clound SSD Virtual Server
-->
Related Links
Install .NET Framework 2.0 & Visual Studio 2005
Install .NET Framework 2.0 & Visual Studio 2005
Install .NET Framework 2.0 & Visual Studio 2005 ติดตั้งและใช้งาน .NET Framework 2.0 และ VS 2005
Rating :
Update :
2009-01-11 09:44:53 View : 8,887
ASP.NET เรียก PHP กับฐานข้อมูล MySQL ผ่าน Web Service และการรับส่งค่าผ่านเว็บเซอร์วิส
ASP.NET เรียก PHP กับฐานข้อมูล MySQL ผ่าน Web Service และการรับส่งค่าผ่านเว็บเซอร์วิส
การสร้างเว็บเซอร์วิสด้วย php กับฐานข้อมูล mysql และทำการเรียก web service ด้วย asp.net ซึ่งอยู่ในฝั่ง client โดยทำการรับส่งข้อมูลระหว่าง php กับ asp.net
Rating :
Update :
2017-03-17 21:21:37 View : 17,033
Basic jQuery for AJAX in ASP.NET Web Site พื้นฐาน Ajax กับ ASP.NET ด้วย jQuery
Basic jQuery for AJAX in ASP.NET Web Site พื้นฐาน Ajax กับ ASP.NET ด้วย jQuery
บทความ jQuery กับการใช้งาน Ajax บน ASP.NET Framework ทำความเข้าใจพื้นฐานการทำงานกับ Ajax ด้วย jQuery กับ ASP.NET แบบง่าย ๆ เบสิก
Rating :
Update :
2017-03-24 21:23:54 View : 21,713
ADO.NET and Oracle  (VB.NET/C#) เขียนโปรแกรมติดต่อกับฐานข้อมูล Oracle Database
ADO.NET and Oracle (VB.NET/C#) เขียนโปรแกรมติดต่อกับฐานข้อมูล Oracle Database
ADO.NETกับฐานข้อมูล Oracle การสร้าง Connection String และการเชื่อมต่อผ่าน TNS Name
Rating :
Update :
2017-03-24 21:28:07 View : 22,089
Generating Word Document in .NET Framework
Generating Word Document in .NET Framework
สร้างไฟล์เอกสาร Word Document บน Windows Application และ Console Application ด้วย Framework
Rating :
Update :
2017-03-24 21:20:55 View : 6,549
สร้าง Crystal Report บน Visual Studio (VB.NET , C#) Step by Step
สร้าง Crystal Report บน Visual Studio (VB.NET , C#) Step by Step
บทความการสร้างรายงานแบบง่าย ๆ บน Crystal Report ซึ่งเป็น Report Tools ที่ได้รับความนิยมมากที่สุด ทำงานร่วมกับ VB.NET และ C#
Rating :
Update :
2017-03-24 21:25:06 View : 114,077
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่

Inline