在 Excel 中将 RTF(富文本格式)代码转换为纯文本 [英] Convert RTF (Rich Text Format) code into plain text in Excel

查看:91
本文介绍了在 Excel 中将 RTF(富文本格式)代码转换为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据库查询导出为 Excel,并且我正在获取具有 RTF 格式的行.

I exporting a database query as Excel and I am getting rows with RTF formatting.

如何将这些字段转换为纯文本?我找到了很老的答案,所以我想知道是否有人知道方法.

How can I convert these fields into plain text? I've found answers that are pretty old, so I was wondering if anyone knows a way.

推荐答案

.Net Framework RichTextBox 类可以执行转换.幸运的是,该类设置了 ComVisibleAttribute,因此可以从 VBA 中轻松使用它.

The .Net Framework RichTextBox class can perform the conversion. Fortunately, this class has the ComVisibleAttribute set, so it can be used from VBA without much difficulty.

我必须创建一个 .tlb 文件以供参考.在

I had to create a .tlb file to Reference. In the

%SYSTEMROOT%Microsoft.NETFrameworkcurrentver

%SYSTEMROOT%Microsoft.NETFrameworkcurrentver

目录,运行命令

regasm /codebase system.windows.forms.dll

创建 system.windows.forms.tlb 文件.我的系统上已经有了这个 .tlb 文件,但我必须使用这个命令重新创建它才能在 VBA 中成功创建一个 .Net System.Windows.Forms RichTextBox 对象.

to create the system.windows.forms.tlb file. I already had this .tlb file on my system, but I had to recreate it using this command to be able to create a .Net System.Windows.Forms RichTextBox object successfully in VBA.

创建新的 .tlb 文件后,在 VBA 中通过工具-> VBA IDE 中的引用将其链接到您的项目.

With the new .tlb file created, in VBA link it to your project via Tools->References in the VBA IDE.

我在 Access 中编写了此测试代码来演示解决方案.

I wrote this test code in Access to demonstrate the solution.

Dim rtfSample As String
rtfSample = "{
tf1ansideflang1033ftnbjuc1 {fonttbl{f0 froman fcharset0 Times New Roman;}{f1 fswiss fcharset0 Segoe UI;}} {colortbl ;
ed255green255lue255 ;} {stylesheet{fs22cf0cb1 Normal;}{cs1cf0cb1 Default Paragraph Font;}} paperw12240paperh15840margl1440margr1440margt1440margb1440headery720footery720deftab720formshadeaendnotesaftnnrlcpgbrdrheadpgbrdrfoot sectdpgwsxn12240pghsxn15840marglsxn1440margrsxn1440margtsxn1440margbsxn1440headery720footery720sbkpagepgnstarts1pgncontpgndec plainplainf1fs22lang1033f1 hello question stemplainf1fs22par}"

Dim miracle As System_Windows_Forms.RichTextBox
Set miracle = New System_Windows_Forms.RichTextBox
With miracle
    .RTF = rtfSample 
    RTFExtractPlainText = .TEXT
End With

MsgBox RTFExtractPlainText(rtfSample)

结果

你好题干

我假设在带有 64 位 Office 的 64 位 Windows 上需要在 Framework64 目录中重新创建 .tlb 文件.我在 32 位 Office 2013 上运行 64 位 Win10,所以我必须有一个 32 位 .tlb 文件.

I'd assume re-creating the .tlb file in the Framework64 directory would be needed on 64-bit Windows with 64-bit Office. I am running 64-bit Win10 with 32-bit Office 2013, so I had to have a 32-bit .tlb file.

这篇关于在 Excel 中将 RTF(富文本格式)代码转换为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆