将运行(Word 2010)转换为SimpleField方法(Word 2007).... [英] Convert Runs (Word 2010) to SimpleField method (Word 2007) ....
问题描述
您好,我正在尝试从Word Office 2010生成一个.DOCX一个.DOTX模板(不兼容模式),我想使用我多年前编写的只能用于SimpleField的代码(Word 2007) )。
Hello, I am trying to generate a .DOCX from Word Office 2010 a .DOTX template (not compatibility mode) and I would like to use a code that I write years ago that only works with SimpleField (Word 2007).
有谁知道如何将新格式(运行)转换为旧格式(Simplefield)?
Anybody knows how to convert new format (run) to old format (Simplefield)?
非常感谢
我找到了这个,但是......这段代码有些不好,因为它没有转换所有字段,我找不到发生的事情。
I have find this one, but ... this code has something bad because it do not convert ALL Fields and I can not find what is happening.
/// <summary>
/// Since MS Word 2010 the SimpleField element is not longer used. It has been replaced by a combination of
/// Run elements and a FieldCode element. This method will convert the new format to the old SimpleField-compliant
/// format.
/// </summary>
/// <param name="mainElement"></param>
internal static void ConvertFieldCodes(OpenXmlElement mainElement)
{
// search for all the Run elements
Run[] runs = mainElement.Descendants<Run>().ToArray();
if (runs.Length == 0) return;
Dictionary<Run, Run[]> newfields = new Dictionary<Run, Run[]>();
int cursor = 0;
do
{
Run run = runs[cursor];
if (run.HasChildren && run.Descendants<FieldChar>().Count() > 0
&& (run.Descendants<FieldChar>().First().FieldCharType & FieldCharValues.Begin) == FieldCharValues.Begin)
{
List<Run> innerRuns = new List<Run>();
innerRuns.Add(run);
// loop until we find the 'end' FieldChar
bool found = false;
string instruction = null;
RunProperties runprop = null;
do
{
cursor++;
run = runs[cursor];
innerRuns.Add(run);
if (run.HasChildren && run.Descendants<FieldCode>().Count() > 0)
instruction += run.GetFirstChild<FieldCode>().Text;
if (run.HasChildren && run.Descendants<FieldChar>().Count() > 0
&& (run.Descendants<FieldChar>().First().FieldCharType & FieldCharValues.End) == FieldCharValues.End)
{
found = true;
}
if (run.HasChildren && run.Descendants<RunProperties>().Count() > 0)
runprop = run.GetFirstChild<RunProperties>();
} while (found == false && cursor < runs.Length);
// something went wrong : found Begin but no End. Throw exception
if (!found)
throw new Exception("Found a Begin FieldChar but no End !");
if (!string.IsNullOrEmpty(instruction))
{
// build new Run containing a SimpleField
Run newrun = new Run();
if (runprop != null)
newrun.AppendChild(runprop.CloneNode(true));
SimpleField simplefield = new SimpleField();
simplefield.Instruction = instruction;
newrun.AppendChild(simplefield);
newfields.Add(newrun, innerRuns.ToArray());
}
}
cursor++;
} while (cursor < runs.Length);
// replace all FieldCodes by old-style SimpleFields
foreach (KeyValuePair<Run, Run[]> kvp in newfields)
{
kvp.Value[0].Parent.ReplaceChild(kvp.Key, kvp.Value[0]);
for (int i = 1; i < kvp.Value.Length; i++)
kvp.Value[i].Remove();
}
}
}
万分感谢
fmorales
推荐答案
嗨fmorales,
Hi fmorales,
感谢您在MSDN论坛上发帖。
Thanks for posting in the MSDN Forum.
我会让一些专家参与此问题,看看他们是否可以帮助您。可能会有一些时间延迟,请耐心等待。
I would involve some experts into this issue to see whether they can help you out. There might be some time delay, appreciate for you patience.
祝你有个美好的一天,
Tom
这篇关于将运行(Word 2010)转换为SimpleField方法(Word 2007)....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!