1

我在 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;
        }
4

1 回答 1

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();
        }
    }
}
于 2013-09-13T02:13:48.580 回答