# c# 표 만들기 에러

**URL:** <https://forum.developer.hancom.com/t/c/675>\
**Category:** 한글 오토메이션\
**Created:** [5월 12, 2023, 5:09오전 UTC](https://forum.developer.hancom.com/t/c/675 "2023-05-12T05:09:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuristar](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nuristar](https://forum.developer.hancom.com/u/nuristar)\
**Post date:** [5월 12, 2023, 5:09오전 UTC](https://forum.developer.hancom.com/t/c/675/1 "2023-05-12T05:09:16Z")

</div>

c#을 이용해서 표 만들기를 하는데. 자꾸 에러가 나는데 어떻게 수정해야 하는지를 모르겠네요…

hwp.HAction.GetDefault(“TableCreate”, hwp.HParameterSet.HTableCreation.HSet);  
hwp.HParameterSet.HTableCreation.Rows = 2;  
hwp.HParameterSet.HTableCreation.Cols = 2;  
hwp.HParameterSet.HTableCreation.WidthType = 0;  
hwp.HParameterSet.HTableCreation.HeightType = 1;  
hwp.HParameterSet.HTableCreation.WidthValue = hwp.MiliToHwpUnit(width / 2);  
hwp.HParameterSet.HTableCreation.HeightValue = hwp.MiliToHwpUnit(height / 2);  
hwp.HParameterSet.HTableCreation.CreateItemArray(“ColWidth”, 2);  
hwp.HParameterSet.HTableCreation.ColWidth.SetItem(0, hwp.MiliToHwpUnit(70.4)); \<— 여기서 에러  
hwp.HParameterSet.HTableCreation.ColWidth.SetItem(1, hwp.MiliToHwpUnit(70.4));  
hwp.HParameterSet.HTableCreation.CreateItemArray(“RowHeight”, 1);  
hwp.HParameterSet.HTableCreation.RowHeight.SetItem(0, hwp.MiliToHwpUnit(9.0));  
hwp.HParameterSet.HTableCreation.TableProperties.Width = 41954;  
hwp.HAction.Execute(“TableCreate”, hwp.HParameterSet.HTableCreation.HSet);  
hwp.HAction.Run(“TBoxTableTemplateInit”);

어떻게 수정해야 할까요??

참조관리자에서 com 찾아보기 해서 HwpObject 1.0 Type Library 사용했습니다.

---

<div class="post-metadata">

**Author:** ![bhjung](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.developer.hancom.com/bhjung/32/259_2.png) [@bhjung](https://forum.developer.hancom.com/u/bhjung)\
**Post date:** [5월 12, 2023, 6:56오전 UTC](https://forum.developer.hancom.com/t/c/675/2 "2023-05-12T06:56:58Z")

</div>

안녕하세요 ^^ ;;  
코드 상으로 봤을땐 이상이 없어보이네요 …  
제가 비슷하게 예제를 만들어서 돌려봐도 정상 동작을 하고있습니다 ㅠ …!

그런데 c#을 사용하신다고 하셨는데, 사용 방식을 보시면 스크립트나 python 처럼 사용하고 계시는것 처럼 보입니다.  
혹시 지금 C#에서 오토메이션을 어떻게 사용하고 계시는지 알수있을까요?

---

<div class="post-metadata">

**Author:** ![nuristar](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nuristar](https://forum.developer.hancom.com/u/nuristar)\
**Post date:** [5월 12, 2023, 7:54오전 UTC](https://forum.developer.hancom.com/t/c/675/3 "2023-05-12T07:54:45Z")

</div>

네. 거의 스크립트 가져와서 파이썬에서 수정하는것과 같이 수정해서 사용하고 있습니다.

아래는. 핸재 테스트 중인 전체 코드 입니다.  
(폼 테두리 없이 실행하는 프로그램이라 일반적으로 필요하지 않은 코드들도 있습니다.  
아래한글에 글자 입력하는 typing 함수도 잘 작동하고  
여백 조정하는 함수도 잘 작동하는데…  
파이썬으로 크게 문제 없이 실행되던 표 만들기가 안되길래… 이유를 모르겠네요…  
)

using HwpObjectLib; // HWP 컨트롤 위한 참조 선언  
using System.Data.Common;  
using System.Diagnostics;  
using System.Runtime.InteropServices; // 윈도우 폼 이동 위한  
using System.Windows.Forms; // 윈도우 폼 이동 위한

namespace hwp\_test\_01  
{  
public partial class Form1 : Form  
{  
public HwpObject hwp = new HwpObject(); // HwpObject hwp 선언

```
    public Form1()
    {
        InitializeComponent();
        init();
    }

    // 윈도우 API 상수와 함수 선언
    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;

    public bool mouseDown = false;

    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    public void init()
    {
        hwp.XHwpWindows.Item(0).Visible = true; // 화면 보기
    }

    //여백 설정
    private void button3_Click(object sender, EventArgs e)
    {
        setMargin(hwp, 23, 23, 18, 18, 15, 15);
    }

    private void setMargin(HwpObject hwp, float left, float right, float top, float bottom, float header, float footter)
    {
        hwp.HAction.GetDefault("PageSetup", hwp.HParameterSet.HSecDef.HSet);
        hwp.HParameterSet.HSecDef.PageDef.LeftMargin = hwp.MiliToHwpUnit(left);
        hwp.HParameterSet.HSecDef.PageDef.RightMargin = hwp.MiliToHwpUnit(right);
        hwp.HParameterSet.HSecDef.PageDef.TopMargin = hwp.MiliToHwpUnit(top);
        hwp.HParameterSet.HSecDef.PageDef.BottomMargin = hwp.MiliToHwpUnit(bottom);
        hwp.HParameterSet.HSecDef.PageDef.HeaderLen = hwp.MiliToHwpUnit(header);
        hwp.HParameterSet.HSecDef.PageDef.FooterLen = hwp.MiliToHwpUnit(footter);
        hwp.HParameterSet.HSecDef.HSet.SetItem("ApplyClass", 24);
        hwp.HParameterSet.HSecDef.HSet.SetItem("ApplyTo", 3);
        hwp.HAction.Execute("PageSetup", hwp.HParameterSet.HSecDef.HSet);
    }

    // 현재 커서 위치 정보 확인
    private int[] cursorInfo(HwpObject hwp)
    {
        int seccnt = 0; // 총 구역
        int secno = 0; // 현재 구역
        int prnpageno = 0; // 쪽
        int colno = 0; //단
        int line = 0; //줄
        int pos = 0; //칸
        short over = 0; //삽입/ 겹침
        string ctrlname = ""; //컨트롤 종류
        hwp.KeyIndicator(out seccnt, out secno, out prnpageno, out colno, out line, out pos, out over, out ctrlname);
        Debug.WriteLine("총 구역 : " + seccnt);
        Debug.WriteLine("현재 구역 : " + secno);
        Debug.WriteLine("쪽 : " + prnpageno);
        Debug.WriteLine("단: " + colno);
        Debug.WriteLine("줄: " + line);
        Debug.WriteLine("칸: " + pos);
        Debug.WriteLine("컨트롤 종류: " + ctrlname);
        int[] cursor_info = new int[6];
        cursor_info[0] = seccnt;
        cursor_info[1] = secno;
        cursor_info[2] = prnpageno;
        cursor_info[3] = colno;
        cursor_info[4] = line;
        cursor_info[5] = pos;
        return cursor_info; // 배열로 리런 [총구역, 현재구역, 쪽, 단, 줄, 칸, 컨트롤 종류]
    }

    // 한글 문서에 문장 입력
    private void typing(HwpObject hwp, string sss)
    {
        var act = hwp.CreateAction("InsertText");
        var pset = act.CreateSet();
        hwp.Run("BreakPara"); //문단 나누기(Enter)
        pset.SetItem("Text", sss);
        act.Execute(pset);
        Debug.WriteLine(cursorInfo(hwp));
    }

    // textBox의 텍스트를 아래한글에 입력하기
    private void button1_Click(object sender, EventArgs e)
    {
        typing(hwp, textBox1.Text);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            mouseDown = true;
        }
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.Location = new Point(
                (this.Location.X) + e.X,
                (this.Location.Y) + e.Y);
            this.Update();
        }
    }

    // 마우스 좌 클릭을 떼었을때
    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        mouseDown = false;
    }

    protected override void WndProc(ref Message m)
    {
        const int RESIZE_HANDLE_SIZE = 10;
        switch (m.Msg)
        {
            case 0x0084/*NCHITTEST*/ :
                base.WndProc(ref m);
                if ((int)m.Result == 0x01/*HTCLIENT*/)
                {
                    Point screenPoint = new Point(m.LParam.ToInt32());
                    Point clientPoint = this.PointToClient(screenPoint);
                    if (clientPoint.Y <= RESIZE_HANDLE_SIZE)
                    {
                        if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                            m.Result = (IntPtr)13/*HTTOPLEFT*/ ;
                        else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                            m.Result = (IntPtr)12/*HTTOP*/ ;
                        else
                            m.Result = (IntPtr)14/*HTTOPRIGHT*/ ;
                    }
                    else if (clientPoint.Y <= (Size.Height - RESIZE_HANDLE_SIZE))
                    {
                        if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                            m.Result = (IntPtr)10/*HTLEFT*/ ;
                        else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                            m.Result = (IntPtr)2/*HTCAPTION*/ ;
                        else
                            m.Result = (IntPtr)11/*HTRIGHT*/ ;
                    }
                    else
                    {
                        if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                            m.Result = (IntPtr)16/*HTBOTTOMLEFT*/ ;
                        else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                            m.Result = (IntPtr)15/*HTBOTTOM*/ ;
                        else
                            m.Result = (IntPtr)17/*HTBOTTOMRIGHT*/ ;
                    }
                }
                return;
        }
        base.WndProc(ref m);
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        //if (e.Control && e.KeyCode == Keys.PageUp) // Ctrl-PageUp            
        if (e.Control && e.KeyCode == Keys.A) // Ctrl-A            
        {
            if (sender != null) ((TextBox)sender).SelectAll();
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        this.Opacity -= 0.1;
        if (this.Opacity < 0.1) { this.Opacity = 0.1; }
    }

    private void button5_Click(object sender, EventArgs e)
    {
        if (this.Opacity < 1)
        {
            this.Opacity += 0.1;
            if (this.Opacity > 1) { this.Opacity = 1; }
        }
    }

    // 종료시 아래한글도 종료하기
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        hwp.Quit();
    }

    // 표 만들기
    private void create_Table(HwpObject hwp, int row, int column, float width, float height)
    {
        hwp.HAction.GetDefault("TableCreate", hwp.HParameterSet.HTableCreation.HSet);
        hwp.HParameterSet.HTableCreation.Rows = 2;
        hwp.HParameterSet.HTableCreation.Cols = 2;
        hwp.HParameterSet.HTableCreation.WidthType = 0;
        hwp.HParameterSet.HTableCreation.HeightType = 1;
        hwp.HParameterSet.HTableCreation.WidthValue = hwp.MiliToHwpUnit(width / 2);
        hwp.HParameterSet.HTableCreation.HeightValue = hwp.MiliToHwpUnit(height / 2);
        hwp.HParameterSet.HTableCreation.CreateItemArray("ColWidth", 2);
        hwp.HParameterSet.HTableCreation.ColWidth.SetItem(0, hwp.MiliToHwpUnit(70.4));
        hwp.HParameterSet.HTableCreation.ColWidth.SetItem(1, hwp.MiliToHwpUnit(70.4));
        hwp.HParameterSet.HTableCreation.CreateItemArray("RowHeight", 1);
        hwp.HParameterSet.HTableCreation.RowHeight.SetItem(0, hwp.MiliToHwpUnit(9.0));
        hwp.HParameterSet.HTableCreation.TableProperties.Width = 41954;
        hwp.HAction.Execute("TableCreate", hwp.HParameterSet.HTableCreation.HSet);
        hwp.HAction.Run("TBoxTableTemplateInit");

        // 셀 속성 - 가로크기 15으로
        hwp.HAction.Run("TableCellBlock");
        hwp.HAction.GetDefault("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet);
        hwp.HParameterSet.HShapeObject.HSet.SetItem("ShapeType", 3);
        hwp.HParameterSet.HShapeObject.HSet.SetItem("ShapeCellSize", 1);
        hwp.HParameterSet.HShapeObject.ShapeTableCell.Width = hwp.MiliToHwpUnit(15.0);
        hwp.HAction.Execute("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet);
        hwp.HAction.Run("Cancel");

        // 표 속성 글자처럼 취급하기
        hwp.FindCtrl(); // 고치기
        hwp.HAction.GetDefault("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet);
        hwp.HParameterSet.HShapeObject.TreatAsChar = 1; // 글자처럼 취급
        hwp.HParameterSet.HShapeObject.HSet.SetItem("ShapeType", 3);
        hwp.HParameterSet.HShapeObject.HSet.SetItem("ShapeCellSize", 0);
        hwp.HAction.Execute("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet);
        hwp.HAction.Run("CloseEx");
    }

    private void button6_Click(object sender, EventArgs e)
    {
        create_Table(hwp, 1, 2, 150, 20);

    }

    // 현재 커서 위치 정보 확인(디버그용)
    private void button7_Click(object sender, EventArgs e)
    {
        cursorInfo(hwp);
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.Style |= 0x20000; // <--- use 0x20000
            return cp;
        }
    }
}

```

}

---

<div class="post-metadata">

**Author:** ![nuristar](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nuristar](https://forum.developer.hancom.com/u/nuristar)\
**Post date:** [5월 12, 2023, 11:32오전 UTC](https://forum.developer.hancom.com/t/c/675/4 "2023-05-12T11:32:02Z")

</div>

hwp.HParameterSet.HTableCreation.ColWidth.SetItem(0, hwp.MiliToHwpUnit(70.4)); \<— 여기서 에러

이 부분에서 SetItem을 아래처럼 바꾸니까 실행되네요…

hwp.HParameterSet.HTableCreation.ColWidth.item[0] = hwp.MiliToHwpUnit(width / 2);

setItem(0, … ) 대신에 item[0] = … 으로
