[英]Excel to Word: Manipulating Field Codes and switches
I am building a Userform (In Excel) that gives the user the following options on Excel to Word automation:我正在构建一个用户表单(在 Excel 中),它为用户提供 Excel 到 Word 自动化的以下选项:
All these options are inspired by this Microsoft article .所有这些选项都受到这篇 Microsoft 文章的启发。
I know I can record a Macro to send each option to Word and then interpret the results, but does anyone know how I can control the syntax sent (maybe as a built string) to Word instead of heavy coding and research for each option above?我知道我可以录制一个宏来将每个选项发送到 Word,然后解释结果,但是有谁知道我可以如何控制发送到 Word 的语法(可能作为内置字符串),而不是对上面的每个选项进行繁重的编码和研究?
Also, does anyone know which options cannot be used with which?另外,有谁知道哪些选项不能与哪个选项一起使用? For example I can't send as bitmap and as a picture.
例如,我不能作为 bitmap 和图片发送。
The syntax I want to send is:我要发送的语法是:
{ LINK ClassName "FileName" [PlaceReference ] [Switches ] }
For example:例如:
{ LINK Excel.Sheet.8 "C:\\My Documents\\Profits.xls" "Sheet1!R1C1:R4C4" \a \p }
Any advice or assistance would be appreciated.任何建议或帮助将不胜感激。
Thanks.谢谢。
R R
Yes, you can use a "built string".是的,您可以使用“内置字符串”。 It's probably the simplest way to do it.
这可能是最简单的方法。
The safest way to insert a field in Word is to insert an "empty" field, ie one that has type wdFieldEmpty
rather than the specific type wdFieldLink
, but provide the complete text of the field.在 Word 中插入字段的最安全方法是插入“空”字段,即类型为
wdFieldEmpty
而不是特定类型wdFieldLink
的字段,但提供字段的完整文本。 Then update it (I have no memory for details so I tend to experiment to see if that is necessary of not).然后更新它(我没有 memory 了解详细信息,所以我倾向于尝试看看是否有必要)。
eg, assuming you have a reference to a Word.Range
object called rng
in the word document, and you have a reference to the Word object library, you might use something like this:例如,假设您在 word 文档中引用了一个名为
rng
的Word.Range
object,并且您引用了 Word object 库,您可能会使用如下内容:
Dim fld As Word.Field
Dim fldText As String
fldText = "LINK Excel.Sheet.12 ""C:\\test\\xl sources\\Book1.xlsx"" ""Sheet1!R3C1:R5C3"" \a \f 4 \r"
' "False" = don't insert \*Mergeformat
Set fld = rng.Fields.Add(rng, Word.wdFieldType.wdFieldEmpty, fldtext, False)
fld.Update
If you aren't referencing the Word object library, you could use如果您没有引用 Word object 库,您可以使用
Dim fld As Object
Dim fldText As String
fldText = "LINK Excel.Sheet.12 ""C:\\test\\xl sources\\Book1.xlsx"" ""Sheet1!R3C1:R5C3"" \a \f 4 \r"
Set fld = rng.Fields.Add(rng, -1, fldtext, False)
fld.Update
Some things to consider:需要考虑的一些事项:
Sheet1:R3C1:R5C3
should either be sheetname!RangeInRCFormat
or rangename
. You may need sheetname.rangename for a range that has sheet scope rather than workbook scope and is not in the first sheet. Sheet1:R3C1:R5C3
应该是sheetname!RangeInRCFormat
或rangename
。对于具有工作表 scope 而不是工作簿 scope 且不在第一张表中的范围,您可能需要 sheetname.rangename 。 As for the "what you can use with what"至于“你可以用什么”
\a switch - it's either \a or not \a. You can't insert *and* break the link using the field
\d switch - Off the top of my head, I'm not sure what \d does these days. You probably can't avoid storing graphical data in .docx
\b, \p (basically a Word wmf/emf format file, \h, \r, \t, \u are mutually exclusive
\f - you can only have one \f option ( e.g. your 4 or 5)
\*Mergeformat tries to preserve the formatting used when you last inserted, and/or that you applied after that. But for complex formatting such as a table layout, you will need to discover whether it does what you need or not.
If you need to update an existing LINK field in Word, you as long as you can locate the field, eg you know it's the 3rs field in the document, you should be able to replace its field code and update the field again, eg如果您需要更新 Word 中已有的LINK 字段,只要您能找到该字段,例如您知道它是文档中的 3rs 字段,您应该可以替换其字段代码并再次更新该字段,例如
'Let's say the variable doc contains a reference to the Word Document
Dim newFldText As String
newFldText = "LINK Excel.Sheet.12 ""C:\\test\\xl sources\\Book1.xlsx"" ""Sheet1!R3C1:R5C3"" \a \f 4 \r \*MergeFormat"
With doc.Fields(3)
.Code.Text = newFldText
.Update
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.