通过C#检查MS Access报告中现有标签的宽度

时间:2013-09-12 17:16:02

标签: c# ms-access ms-access-2013

我在MS Access 2013中创建了报告,其中包含标签,文本框等控件 我想在C#.net中访问此控件以检查这些控件的宽度,颜色属性 我设法找到了报告,但我在该报告中找不到控件 我怎么能这样做?
评论的代码是我尝试但没有得到结果的代码。

public bool CheckReport(AccessEntity acEntity, ACCESS.Application app) 
{ 
     try 
     { 
    ACCESS.AllReports report =(ACCESS.AllReports)app.CurrentProject.AllReports; 
    ACCESS.Controls objcntrls=null; 
    ACCESS.Section DetailSec; string strval;


    //ACCESS.Report r = (ACCESS.Report)app.CurrentProject.AllReports[acEntity.Data];
                //string strCap = (string)((dynamic)app.Reports[0]).Controls[acEntity.Rows].Caption;
                //ACCESS.Report r = (ACCESS.Report)app.CurrentProject.AllReports[0];
                //DetailSec = (ACCESS.Section)app.Reports[acEntity.Data].Section[0];
                //objcntrls = (ACCESS.Controls)app.Reports[acEntity.Data].Controls;

    string strwidth = objcntrls[acEntity.Field].Width.ToString();
                strval = RemoveSpace(acEntity.PropertyValue);
                    if (strwidth == strval)
                    {
                           return true;
                    }
      } 
             catch (System.Exception ex)
     { }
   return false;
        }

1 个答案:

答案 0 :(得分:1)

我的Access数据库包含一个名为Report1的报告,该报告的标签名为Label0,其宽度定义为1"。当我运行以下C#代码时,它会显示

  报告“Report1”中的

控件“Label0”的.Width值为1440

代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AccessOleAuto
{
    class Program
    {
        static void Main(string[] args)
        {
            string reportName = "Report1";
            string controlName = "Label0";
            //
            // Note: This project has the following COM reference defined:
            //
            // Microsoft Access 14.0 Object Library
            //
            var accessApp = new Microsoft.Office.Interop.Access.Application();
            accessApp.OpenCurrentDatabase(@"C:\__tmp\testData.accdb");
            accessApp.DoCmd.OpenReport(reportName, Microsoft.Office.Interop.Access.AcView.acViewDesign);
            Microsoft.Office.Interop.Access.Report rpt = accessApp.Reports[reportName];

            int ctlWidth = rpt.Controls[controlName].Width;
            Console.WriteLine("control \"" + controlName + "\" in report \"" + reportName + "\" has a .Width value of " + ctlWidth);

            accessApp.DoCmd.Close(Microsoft.Office.Interop.Access.AcObjectType.acReport, reportName, Microsoft.Office.Interop.Access.AcCloseSave.acSaveNo);
            accessApp.Quit();

            // wait for a keypress before terminating
            Console.ReadKey();
        }
    }
}