我有一个用 vba 完成的 word 模板,并且在特定的键盘事件中,我在文档的 BOOKMARKS 中进行用户所做的更改以在 DB 中更新它们。问题是一些数据必须与它们的样式(粗体、斜体、颜色、对齐)一起保存,但是当我尝试复制书签的内容并获取它的 rtf 内容时,添加了很多不必要的标签!我不想要纯文本,我想保留必要的样式标签,但我必须删除其余的,因为我不可能将所有这些数据保存在数据库中,太大了!!
这是我正在使用和获取的示例:在书签中,它用粗体写成“ *test1 ”,当我从富文本框中获取它时,我得到的是以下字符串:
"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\fnil\fcharset0 次新罗马;}{\f1\froman\fprq2\fcharset0 次新罗马;}} \viewkind4\uc1 \pard\ltrpar\b\f0\fs24 *test1 \b0\f1\par }"
但是在我使用我的一段代码(我必须使用它)之后,我得到的是:
} {\fbiminor\f31583\fbidi \froman\fcharset177\fprq2 Times New Roman (希伯来语);}{\fbiminor\f31584\fbidi \froman\fcharset178\fprq2 Times New Roman (阿拉伯语);}{\fbiminor\f31585\fbidi \froman\fcharset186\fprq2 Times New Roman Baltic;} {\fbiminor\f31586\fbidi \froman\fcharset163\fprq2 Times New Roman (越南语);}}{\colortbl;\red0\green0\blue0;\red0\green0\ blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0; \red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\红128\绿128\蓝128;\红192\绿192\蓝192; http://schemas.microsoft.com/office/word/2003/wordml}}\paperw12240\paperh15840\margl1440\margr1440\margt1440\margb1440\gutter0\ltrsect \widowctrl\ftnbj\aenddoc\trackmoves0\trackformatting1\donotembedsysfont1\relyonvml0\donotembedlingdata0\grfdocevents0\validatexml1\showplaceholdtext0\ignoremixedcontent0\saveinvalidxml0\showxmlerrors1\noxlattoyen \expshrtn \noultrlspc\dntblnsbdb\nospaceforul\formshade\horzdoc\dgmargin\dghspace180\dgvspace180\dghorigin1701\dgvorigin1984\dghshow1\dgvshow1 \jexpand\pgbrdrhead\pgbrdrfoot\splytwnine\ftnlytwnine\htmautsp\nolnhtadjtbl\useltbaln\alntblind\lytcalctblwd\lyttblrtgr\lnbrkrule\nobrkwrptbl \snaptogridincell\allowfieldendsel\wrppunct\asianbrkrule\rsidroot664789 \newtblstyruls\nogrowautofit\usenormstyforlist\noindnmbrts\felnbrelev\nocxsptable\indrlsweleven\noafcnsttbl\afelev\utinl\hwelev\spltpgpar\notcvasp\notbrkcnstfrctbl\notdvatcolbalxbx\\nouicompat \fet0{*\wgrffmtfilter 2450} \nofeaturethrottle1\ilfomacatclnup0\ltrpar \sectd \ltrsect\linex0\endnhere\sectlinegrid360\sectdefaultcl\sftnbj {*\pnseclvl1\pnucrm\pnstart1\pnindent720\pnhang {\pntxta .}}{* \pnseclvl2\pnucltr\pnstart1\pnindent720\pnhang {\pntxta .}}{*\pnseclvl3 \pndec\pnstart1\pnindent720\pnhang {\pntxta .}}{*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang {\pntxta )}}{*\pnseclvl5\pndec\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta)}}{*\pnseclvl6\pnlcltr\ pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta)}} {*\pnseclvl7\pnlcrm\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta)}}{*\pnseclvl8\pnlcltr\pnstart1\pnindent720\ pnhang {\pntxtb (}{\pntxta )}}{*\pnseclvl9\pnlcrm\pnstart1\pnindent720\pnhang {\pntxtb (}{\pntxta)}}\pard\plain \ltrpar \ql \li0\ri0\sa200\ sl276\slmult1\widctlpar\wrapdefault\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \rtlch\fcs1 \af31507\afs22\alang1025 \ltrch\fcs0 \fs22\lang1033\langfe1033\loch\af31506\hich\af31506\ dbch\af31505\cgrid\langnp1033\langfenp1033 { \rtlch\fcs1 \ab\af0\afs24 \ltrch\fcs0 \b\f0\fs24\insrsid664789\charrsid15300069 \hich\af0\dbch\af31505\loch\f0*test1 }{\rtlch\fcs1 \af31507 \ltrch\fcs0 \insrsid11549567 \par }{*\themedata 504b030414000600080000002100e9de0fbfff0000001c020000130000005b436f6e74656e745f54797065735d2e786d6cac91cb4ec3301045f748fc83e52d4a ..."
以及更多
如您所见,未使用的信息太多。
Sub Test()
Dim ad As Document
Set ad = ActiveDocument
Dim bm As Bookmark
dim rtf as String
For Each bm In ad.Bookmarks
Set rngread = bm.Range
rngread.End = rngread.End - 1 'for the 1 font space protecting the bookmark
rtf = GetRTFtext(rngread)
Next
End Sub
Function GetRTFtext(rngread As Range) As String
Dim RTFText As String
Dim TempFile As String
TempFile = "C:/Temp/tmp0859" & Day(Now) & Month(Now) & Year(Now) & Hour(Now) & Minute(Now) & Second(Now) & ".rtf"
rngread.ExportFragment TempFile, wdFormatRTF
Dim objFSO As New FileSystemObject
Dim objTextStream As TextStream
Set objTextStream = objFSO.OpenTextFile(TempFile, ForReading)
RTFText = objTextStream.ReadAll
objTextStream.Close
If objFSO.FileExists(TempFile) Then objFSO.DeleteFile TempFile
GetRTFtext = RTFText
End Function