1

次のコードを使用して、現在1つのMergeFieldだけで設定した単純な単語テンプレートにテキストを送信して、これを機能させることができるかどうかをテストしています。
私が使用しているコードは次のとおりです。

public static void ReplaceMailMergeField(string pWordDoc, string pMergeField, string pValue)
{
    object docName = pWordDoc;
    object missing = Missing.Value;
    Word.MailMerge mailMerge;
    Word._Document doc;
    Word.Application app = new Word.Application();
    app.Visible = false;
    doc = app.Documents.Open(ref docName, 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, ref missing);
    mailMerge = doc.MailMerge;
    foreach (Word.MailMergeField f in mailMerge.Fields)
    {
        if (f.Code.Text.IndexOf("MERGEFIELD  \"" + pMergeField + "\"") > -1)
        {
            f.Select();
            app.Selection.TypeText(pValue);
        }
    }
    object saveChanges = Word.WdSaveOptions.wdSaveChanges;
    doc.Close(ref saveChanges, ref missing, ref missing);
    app.Quit(ref missing, ref missing, ref missing);
}

これをアプリケーションから次のように呼び出します。

string pWordDoc = @"C:\Users\Pete-Laptop\Documents\CMS Document Mangement\Word Template.dotx";
cDocument.ReplaceMailMergeField(pWordDoc, "fieldAddress1", "Put address here!");

しかし、何も起こりません。コードをステップスルーすると、app.Documents.Openまで到達し、フリーズしているように見えます。これは、アプリケーションが私のWord文書を見つけられないためだと思います。ファイル名パラメータに完全なファイルパスを送信するのは正しいですか?そうでない場合、アプリケーションは他にどのように私のWordテンプレートを見つけますか?

4

2 に答える 2

4

私が使用し、私のために機能する最終的なコードは次のとおりです。

public static void TextToWord(string pWordDoc, string pMergeField, string pValue)
    {
        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 = pWordDoc;
        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 == pMergeField)
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(pValue);
                }
            }
        }
    }

このコードは元々ここからです。

于 2012-05-24T11:28:08.570 に答える
3

あなたはで試すことができます:

_doc = _app.Documents.Add(ref _docName , ref _missing , ref _missing , ref _missing );

それ以外の

_doc = _app.Documents.Open(.......);

次に、この行が正しいかどうかを確認します。

if (f.Code.Text.IndexOf("MERGEFIELD  \"" + pMergeField + "\"") > -1) 

このように機能するコードがあります

        mailMerge = doc.MailMerge;         
        foreach (Word.MailMergeField f in mailMerge.Fields)         
        {    
             // Extract the name of the MergeField starting from the 11 character
             // and looking for the first space after the name 
             // (this means that you avoid MergeFields names with embedded spaces)
             string fieldName = f.Code.Text.Substring(11).Trim();
             int  pos = fieldName.IndexOf(' ');
             if (pos >= 0) fieldName = fieldName.Substring(0, pos).Trim();

             if (fieldName == pMergeField) 
             {
                   f.Select();                  
                   app.Selection.TypeText(pValue);  
             }
于 2012-05-24T09:42:10.803 に答える