无法从c#编辑word文件

时间:2016-11-14 05:11:20

标签: c# visual-studio-2015

我已经使用下面的代码创建了编辑word文件的简单代码。 我使用 Visual Studio 2015 Word 2007 office interop 12.0

    private void inputdata_Load(object sender, EventArgs e)
    {
        lblTA.Text = getTA;
        lblPeriode.Text = getPeriode;
    }

    public void CariandGanti(Microsoft.Office.Interop.Word.Application WordApp, object CariKata, object GantiKata)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildcards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;
        object matchPrefix = true;
        object matchSuffix = true;
        object matchPhrase = true;
        object ignoreSpace = true;
        object ignorePunct = true;

        WordApp.Selection.Find.Execute2007(ref CariKata, ref matchCase, ref matchWholeWord,
            ref matchWildcards, ref matchSoundsLike, ref nmatchAllWordForms,
            ref forward, ref wrap, ref format, ref GantiKata, ref replace,
            ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl,
            ref matchPrefix, ref matchSuffix, ref matchPhrase, ref ignoreSpace, ref ignorePunct);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //double totalKuantitasBarkon = Convert.ToDouble(txtSAKuaBarKon.Text) + Convert.ToDouble(txtMutTamKuaBarkon.Text) - Convert.ToDouble(txtMutKurKuaBarKon.Text);
        double totalMutasiBarkon = Convert.ToDouble(txtMutTamNiBarKon.Text) - Convert.ToDouble(txtMutKurniBarkon.Text);
        double totalNilaiBarkon = Convert.ToDouble(txtSANilBarKon.Text) + totalMutasiBarkon;
        //txtTotalnilaiKuaBarKon.Text = totalKuantitasBarkon.ToString();
        //txttotalNilaiBarKon.Text = totalNilaiBarkon.ToString();
        object missing = Missing.Value;
        Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
        Document aDoc = null;
        sPathSource = Directory.GetCurrentDirectory() + "\\CaLBMN.docx";

        if (File.Exists((string)sPathSource))
        {
            DateTime today = DateTime.Now;
            object readOnly = false;
            object isVisible = true;
            oWord.Visible = true;
            aDoc = oWord.Documents.Open(sPathSource, ref missing,ref readOnly, 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();
            this.CariandGanti(oWord, "<117111SA>", txtSANilBarKon.Text);
            this.CariandGanti(oWord, "<117111Mu>", totalMutasiBarkon.ToString());
            this.CariandGanti(oWord, "<117111SAk>", totalNilaiBarkon.ToString());

        }
        else
        {
            MessageBox.Show("File Tidak ada !!!");
            return;
        }
        releaseObject(oWord);
        releaseObject(aDoc);
    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Unable to release the Object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }
}

但是在跑步之后,它会显示出来 类型

的未处理异常
  

&#39; System.AccessViolationException&#39;

发生在otomasiLapKeu.exe

其他信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

the warning

任何人都可以帮忙解决这个问题吗?

对不起我的英语不太好。

0 个答案:

没有答案