简体   繁体   English

保留Excel单元格格式的Excel VBA到Word书签

[英]Excel VBA to Word bookmark with Excel cell format retained

I have a VBA code snippet that extracts Excel cell data to a Word template, using bookmarks in the template to assign values to specific template locations. 我有一个VBA代码片段,可使用模板中的书签将值分配给特定模板位置,从而将Excel单元格数据提取到Word模板。 I am able to export Excel cell values in text format to the bookmarks, but cannot retain the native Excel formatting. 我能够将文本格式的Excel单元格值导出到书签,但是不能保留本机Excel格式。

The code uses the .Value property to extract data in text formatting. 该代码使用.Value属性以文本格式提取数据。 I'm trying to minimize manual formatting a second time in Word (converting a text exported value of 150001.22 from the DENT_90 Excel range to $150,001.22 for the DENT_90 bookmark in Word, for example). 我正在尝试最小化Word中的第二次手动格式化(例如,将Word中DENT_90书签的文本导出值150001.22从DENT_90 Excel范围转换为$ 150,001.22)。

Any idea on how this might be done, such that I can export to Word using the Excel currency cell formatting? 关于如何完成此操作的任何想法,以便可以使用Excel货币单元格格式导出到Word? I've looked at some explanations for FormatText, but can't seem to get it to work with the following snippet: 我看过有关FormatText的一些说明,但似乎无法使其与以下代码段一起使用:

Sub TestMemoGen()
Dim objWord As Object
Dim ws2 As Worksheet

Application.ScreenUpdating = False
Set ws2 = ThisWorkbook.Sheets("Word Export")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "C:\Users\ Tester\Word Test Sim.docm"

With objWord.ActiveDocument
.Bookmarks("DENT_90").Range.Text = ws2.Range("DENT_90").Value
End With
Set objWord = Nothing
Application.EnableEvents = True
End Sub

Thanks for any and all suggestions. 感谢您提出的所有建议。 I have about a hundred bookmarks linked to different templates, so this will be a huge help if formatting can be exported natively from Excel. 我大约有一百个书签链接到不同的模板,因此如果可以从Excel本地导出格式,这将是巨大的帮助。

.Value is only the "text" of the cell, in this case the number. .Value只是单元格的“文本”,在这种情况下为数字。 It doesn't carry formatting info with it. 它不带有格式信息。 You need NumberFormat. 您需要NumberFormat。

<Some number>  = ws2.Range("DENT_90").Value
<the format>   = ws2.Range("DENT_90").DisplayFormat.NumberFormat

A little test shows: 一点测试表明:

Sub junk()
    MsgBox "format=" & Application.Range("g2").DisplayFormat.NumberFormat    
End Sub

So, info from Excel cell is "format=#,##0". 因此,来自Excel单元格的信息是“ format =#,## 0”。

In the Word doc, you can then use format() as described above by Scott, or at: https://msdn.microsoft.com/en-us/library/office/gg251755.aspx 在Word文档中,然后可以使用Scott所描述的format(),或在以下位置使用: https//msdn.microsoft.com/zh-cn/library/office/gg251755.aspx

As a side note, did you know you can try things and record the functions in a macro as you go? 附带说明一下,您是否知道可以随时尝试并在宏中记录功能? Then, look at the recorded macro to see how to do things, like perhaps formatting text as entered, which gives you a little more control over precise formatting... 然后,查看记录的宏以了解操作方法,例如输入的文本格式可能会更精确地控制格式...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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