从c#.net代码访问MS Word 2007的VBA控件 [英] Accessing VBA Controls of MS word 2007 from c#.net code

查看:127
本文介绍了从c#.net代码访问MS Word 2007的VBA控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个要求,我需要编写/填写一个通过开发人员工具包添加到MS Word 2007中的文本框.我已经编写了以下代码

Hi

I am having a requirement where i need to write/ fill up a textbox which has been added in the MS word 2007 through developer tool kit. I have written the following code

private void CreateWordDocument(object filename, object saveAs)
    {
        object missing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application wordapp = new ApplicationClass();
        Microsoft.Office.Interop.Word.Document aDoc = null;
        if (File.Exists((string)filename))
        {
            DateTime today = DateTime.Now;
            object readOnly = false;
            object isVisible = false;
            wordapp.Visible = false;
            aDoc = wordapp.Documents.Open(ref filename, 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.FindAndReplace(wordapp, "Health", "HEALTH1");
       
            aDoc.Content.InsertBefore("this is at the begin\r\n\r\n");
            if(aDoc.Content.ContentControls.Count > 0)
            {

               //I am not able to enter in to this loop because the   
               // above conditon is not able to find out what are a            // all the controls i have added to the word document
            }

            aDoc.Content.InsertAfter("\r\n\r\nThis is at the end");
            aDoc.SaveAs(ref saveAs, 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);
        }
        else
        {
            Response.Write("file doesnot exist");
            return;
        }

    }
    private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordapp, object findtext, object replaceWithtext)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchwildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordFormds = 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;
        wordapp.Selection.Find.Execute(ref findtext, ref matchCase, ref matchWholeWord, ref matchwildCards, ref matchSoundsLike,
                                           ref nmatchAllWordFormds, ref forward, ref wrap, ref format, ref replaceWithtext,
                                           ref replace, ref matchkashida, ref matchDiacritics, ref matchAlefHamza,
                                           ref matchControl);

    }



上面的代码可以很好地用于编写新文本并替换MS Word文档中的现有文本.但是我没有得到如何编写代码的方法,因此我可以以编程方式传递值以填充文本框,该文本框是我通过Word文档中的开发人员工具箱添加的.请尽快提供帮助.

问候,



The Above code is working fine for writing the new text and replacing the existing text which in the MS word document. But i am not getting how to write the code so that i can programtically pass the value to fill up the Text Box what i have added through developer tool box in the word document. Please kindly help ASAP.

Regards,

推荐答案

为什么要以这种方式传递变量:
Why are you passed variables in this way:
private void CreateWordDocument(object filename, object saveAs)


然后您使用了:


and then you used:

(string)filename)



正确的是:



the correct is:

private void CreateWordDocument(String filename, String saveAs)



如果要获取TextBox,请尝试遍历InlineShapes集合.



If you want to get TextBox, try to walk through InlineShapes collection.


这篇关于从c#.net代码访问MS Word 2007的VBA控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆