1. Visio屬性值的轉換問題
做過Visio開發的人知道,Visio中的屬性值也就是Cell.Formula的值通常包含兩對雙引號的(如""XX""), 如果要將屬性的值轉換正常的字符串值,那麼需要去除雙引號。因此從Visio的Cell的Formula值中得到的字符串需要經過下面方法處理一下:
public static string FormulaStringToString(string formula)
{
const string OneQuote = "\"";
const string TwoQuotes = "\"\"";
string convertedFormula = "";
try
{
convertedFormula = formula;
if (convertedFormula.StartsWith(OneQuote) && convertedFormula.EndsWith(OneQuote))
{
convertedFormula = convertedFormula.Substring(1, (convertedFormula.Length - 2));
convertedFormula = convertedFormula.Replace(TwoQuotes, OneQuote);
}
}
catch (Exception err)
{
convertedFormula = "";
}
return convertedFormula;
}
如果是寫入到Visio的Cell的Formula中,那麼要經過反過程,如下所示:
public static string StringToFormulaForString(string input)
{
const string quote = "\"";
string result = "";
if (input == null)
{
return null;
}
result = input.Replace(quote, (quote + quote));
result = quote + result + quote;
return result;
}
2、獲取指定形狀指定Cell的值。除了方法1,還有下面一種方法可以獲取Cell的Value值。這種方法比使用Formula獲取字符串的方式要好,是因為在Visio2007中下拉列表“資產歸屬”.對應的Cell的Value可能是INDEX(0,Prop.資產歸屬.Format),但是如果使用下面的方法就可以正常獲取到它具體的值了。
public static string GetShapeCellValue(Shape shapeTarget, string strCellType)
{
const string CUST_PROP_PREFIX = "Prop.";
string shapeCellValue = string.Empty;
if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + strCellType, (short)VisExistsFlags.visExistsAnywhere) != 0)
{
shapeCellValue = FormulaStringToString(shapeTarget.get_CellsU(CUST_PROP_PREFIX + strCellType).get_ResultStr(VisUnitCodes.visNoCast));
}
return shapeCellValue;
}
3、給指定的Shape賦值。方法2是讀取,當然還需要寫入到指定Shape,指定Cell的值
public static bool SetShapeCellValue(Shape shapeTarget, string strCellType, string cellValue)
{
const string CUST_PROP_PREFIX = "Prop.";
bool breturn = false;
if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + strCellType, (short)VisExistsFlags.visExistsAnywhere) != 0)
{
shapeTarget.get_CellsU(CUST_PROP_PREFIX + strCellType + ".Value").Formula = StringToFormulaForString(cellValue);
}
return true;
}
4、判斷形狀某個屬性是否存在。有時候在做一些操作前,需要判斷某個屬性是否存在,以免訪問指定的Cell不存在而拋出異常。
public static bool ShapeCellExist(Shape shapeTarget, string strCellType)
{
const string CUST_PROP_PREFIX = "Prop.";
bool breturn = false;
if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + strCellType, (short)VisExistsFlags.visExistsAnywhere) != 0)
{
breturn = true;
}
return breturn;
}
5、取當前操作屬性所在的行。Cell的行號有時候非常重要,因此有必要提供一個函數獲取對應Cell在ShapeData中的行號。
public static int GetCustomPropRow(Shape shapeTarget, string propName)
{
const string CUST_PROP_PREFIX = "Prop.";
int intCustomRow = -1;
if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + propName, (short)VisExistsFlags.visExistsAnywhere) != 0)
{
intCustomRow = shapeTarget.get_CellsRowIndexU(CUST_PROP_PREFIX + propName);
}
return intCustomRow;
6、判斷Visio圖紙上是否有形狀圖元存在。如果圖紙上沒有形狀圖元,你進行操作的時候可能會拋出“請求被禁用”的異常,因此可以操作前先判斷有設備在圖紙上為妙。
public static bool HasShapeInWindow(Window window)
{
bool result = false;
try
{
window.SelectAll();
result = (window.Selection.Count > 0);
window.DeselectAll();
}
catch
{ ;}
return result;
}
7、其他的一些功能設置
//Visio2007的形狀窗口中去除搜索形狀功能
VisApplication.Settings.ShowShapeSearchPane = false;
Visio2003的ShowShapeSearchPane實現方式#region Visio2003的ShowShapeSearchPane實現方式
//Window searchWindow = wndVisio.Windows.get_ItemFromID(Convert.ToInt16(VisWinTypes.visWinIDShapeSearch));
//if (searchWindow != null)
//{
// searchWindow.Visible = false;
//}
#endregion
//屏蔽Visio2007中的動態連接的功能(默認有)
VisApplication.Settings.EnableAutoConnect = false;
VisApplication.Settings.StencilBackgroundColor = 10070188;
8、Name和NameU屬性的差別
Visio中很多屬性都有一個同名+U的屬性名稱,一般情況下最好使用這個名稱如NameU,因此這個是一個唯一的名字,有時候你會發現Name相同,但他們就是不一樣,因為他們的NameU名稱不一樣的。
9、遇到不明白的操作或者屬性,多用Visio文檔的宏記錄功能,然後對VBA代碼進行分析和調試。