将字符串转换为 WdPaperSize 对象 - Word VSTO

Convert string to WdPaperSize object - Word VSTO

我正在开发 Word 加载项 (VSTO)。

我有一个包含页面大小值的 xml 文件,如下所示:

<entry name="Size">wdPaperA3</entry>

我正在尝试获取这些值并将它们设置在 WdPaperSize 对象中,如下所示:

WdPaperSize val = this.getFromXML("Size");
foreach (Section i in doc.Sections)
   {
      i.PageSetup.PaperSize = val;
   }

但我收到这些错误:

Cannot implicitly convert type 'string' to 'Microsoft.Office.Interop.Word.WdPaperSize'

'WdPaperSize' is a type, which is not valid in the given context

如何将字符串转换为 WdPaperSize 对象?

WdPaperSize 是一个枚举。尝试:

i.PageSetup.PaperSize = (WdPaperSize) Enum.Parse(typeof(WdPaperSize), val);