简体   繁体   English

Excel 转字:操作域代码和开关

[英]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 文章的启发。

  • Update links automatically / manually / break link (\a switch)自动/手动更新链接/断开链接(\a 开关)
  • Store graphical data (\d switch)存储图形数据(\d 开关)
  • Insert as bitmap (\b switch)插入为 bitmap(\b 开关)
  • Insert as picture (\p switch)作为图片插入(\p 开关)
  • Maintain formatting of source file (\f 4 switch)保持源文件的格式(\f 4 开关)
  • Match the formatting of the destination document (\f 5 switch)匹配目标文档的格式(\f 5 开关)
  • Insert as HTML (\h switch)插入为 HTML(\h 开关)
  • Insert as RTF (\r switch)插入为 RTF(\r 开关)
  • Insert in text-only format (\t switch)以纯文本格式插入(\t 开关)
  • Insert as Unicode text (\u switch)插入为 Unicode 文本(\u 开关)
  • Preserve formatting after update (* MERGEFORMAT switch)更新后保留格式(* MERGEFORMAT 开关)

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 文档中引用了一个名为rngWord.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:需要考虑的一些事项:

  • in the.docx format, only some Paste Special formats will insert a field code into the document.在 .docx 格式中,只有某些特殊粘贴格式会在文档中插入域代码。 Others will insert a linked object.其他人会插入一个链接的 object。 So inserting a { LINK } field is not necessarily going to have exactly the same effect as a user who does Paste Special因此插入 { LINK } 字段不一定会与执行选择性粘贴的用户具有完全相同的效果
  • the COM ProgId (Excel.Sheet.12 does not need to be quoted, but you do need to use the correct ProgId COM ProgId(Excel.Sheet.12 不需要引用,但您确实需要使用正确的 ProgId
  • The path/file name may need to be quoted if it contains special characters such as space, and will need to have backslashes doubled up如果路径/文件名包含空格等特殊字符,则可能需要用引号引起来,并且需要将反斜杠加倍
  • The "subset" (in my example, 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!RangeInRCFormatrangename 。对于具有工作表 scope 而不是工作簿 scope 且不在第一张表中的范围,您可能需要 sheetname.rangename 。
  • You cannot guarantee that whatever string you provide to Word will survive a field update.您不能保证您提供给 Word 的任何字符串都将在字段更新后继续存在。 Word may replace the LINK code with its own representation of the link. Word 可以用它自己的链接表示形式替换 LINK 代码。 In fact, it is arguably a bonus that Word interprets COM LINK strings in the way you might hope.事实上,Word 以您希望的方式解释 COM LINK 字符串可以说是一个好处。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM