1

Visual Studio 2010 で最初のアプリケーションを開発していますが、この問題の解決策が見つからないようです。Word テンプレートを変更して、独自のカスタム テキストを挿入しようとしています。そのため、以下のコードを使用してそうしています。

しかし、デバッガーを試すたびに、次の行で停止します。oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing).

「COMException が処理されませんでした。Word はこのドキュメントを読み取ることができませんでした。破損している可能性があります。」

必要な名前空間をすべて入力し、Word への参照を追加しました。Visual Studio 2010 と Microsoft Word 2007 を使用しています。

private void fnGenerateDoc() 
{
    Object oMissing = System.Reflection.Missing.Value;

    Object oTrue = true;
    Object oFalse = false;

    Word.Application oWord = new Word.Application();
    Word.Document oWordDoc = new Word.Document();


    oWord.Visible = true;

    Object oTemplatePath = "Test1.dot";
    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

    foreach (Word.Field myMergeField in oWordDoc.Fields)
    {
        Word.Range rngFieldCode = myMergeField.Code;
        String fieldText = rngFieldCode.Text;

        if (fieldText.StartsWith(" MERGEFIELD"))
        {

            Int32 endMerge = fieldText.IndexOf("\\");
            Int32 fieldNameLength = fieldText.Length - endMerge;
            String fieldName = fieldText.Substring(11, endMerge - 11);

            fieldName = fieldName.Trim();

            if (fieldName == "NaslovFirma")
            {
                myMergeField.Select();
                oWord.Selection.TypeText("Nesto");
            }
        }
4

0 に答える 0