gpt4 book ai didi

vba - 在 Excel 中将 RTF(富文本格式)代码转换为纯文本

转载 作者:行者123 更新时间:2023-12-03 02:17:04 25 4
gpt4 key购买 nike

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

Here is a screenshot of the Excel

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

最佳答案

.Net Framework RichTextBox 类可以执行转换。幸运的是,此类具有 ComVisibleAttribute 集,因此可以轻松地从 VBA 使用它。

我必须创建一个 .tlb 文件以供引用。在

%SYSTEMROOT%\Microsoft.NET\Framework\currentver\

目录下,运行命令

regasm /codebase system.windows.forms.dll

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

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

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

Dim rtfSample As String
rtfSample = "{\rtf1\ansi\deflang1033\ftnbj\uc1 {\fonttbl{\f0 \froman \fcharset0 Times New Roman;}{\f1 \fswiss \fcharset0 Segoe UI;}} {\colortbl ;\red255\green255\blue255 ;} {\stylesheet{\fs22\cf0\cb1 Normal;}{\cs1\cf0\cb1 Default Paragraph Font;}} \paperw12240\paperh15840\margl1440\margr1440\margt1440\margb1440\headery720\footery720\deftab720\formshade\aendnotes\aftnnrlc\pgbrdrhead\pgbrdrfoot \sectd\pgwsxn12240\pghsxn15840\marglsxn1440\margrsxn1440\margtsxn1440\margbsxn1440\headery720\footery720\sbkpage\pgnstarts1\pgncont\pgndec \plain\plain\f1\fs22\lang1033\f1 hello question stem\plain\f1\fs22\par}"

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)

结果

hello question stem

我假设在具有 64 位 Office 的 64 位 Windows 上需要在\Framework64\目录中重新创建 .tlb 文件。我运行的是 64 位 Win10 和 32 位 Office 2013,因此我必须有一个 32 位 .tlb 文件。

关于vba - 在 Excel 中将 RTF(富文本格式)代码转换为纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42576291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com