从word .dot导出到.rtf后,RTF文本中添加了太多未使用的样式标记

时间:2013-11-07 10:25:56

标签: vba templates ms-word richtextbox rtf

我有一个用vba完成的单词模板,在特定的键盘事件中,我在文档的BOOKMARKS中对用户进行了更改,以便在DB中更新它们。 问题是一些数据必须用它们的样式(粗体,斜体,颜色,对齐)保存,但是当我尝试复制书签的内容并获取它的rtf内容时,会添加许多不必要的标签! 我不想要纯文本,我想保留必要的样式标签,但我必须删除其余的,因为我不可能将所有这些数据保存在DB中,太大了!!

这是我使用和获取的示例: 在书签中,它写成" * test1 "在Bold中,当我从富文本框中获取它时,我得到的是以下字符串:

  

" {\ RTF1 \ fbidis \ ANSI \ ansicpg1252 \ deff0 \ deflang1033 \ deflangfe1033 {\ fonttbl {\ F0 \ fnil \ fcharset0   Times New Roman;} {\ f1 \ froman \ fprq2 \ fcharset0 Times New Roman;}}   \ viewkind4 \ uc1 \ pard \ ltrpar \ b \ f0 \ fs24 * test1 \ b0 \ f1 \ par}"

但是在我使用我的代码之后(我必须使用它),我得到的是:

  

" {\ RTF1 \ adeflang1025 \ ANSI \ ansicpg1252 \ UC1 \ adeff31507 \ deff0 \ stshfdbch31505 \ stshfloch31506 \ stshfhich31506 \ stshfbi31507 \ deflang1033 \ deflangfe1033 \ themelang1033 \ themelangfe0 \ themelangcs0 {\ fonttbl {\ F0 \ fbidi   \ froman \ fcharset0 \ fprq2 {* \ panose 02020603050405020304}时代新   罗马;} {\ f34 \ fbidi \ froman \ fcharset0 \ fprq2 {* \ panose   02040503050406030204} Cambria Math;} {\ f37 \ fbidi   \ fswiss \ fcharset0 \ fprq2 {* \潘   020f0502020204030204}宋体;} {\ flomajor \ f31500 \ fbidi   \ froman \ fcharset0 \ fprq2 {* \ panose 02020603050405020304}时代新   罗马;} {\ fdbmajor \ f31501 \ fbidi \ froman \ fcharset0 \ fprq2 {* \ panose   02020603050405020304} Times New Roman;} {\ fhimajor \ f31502 \ fbidi   \ froman \ fcharset0 \ fprq2 {* \ panose 02040503050406030204}坎布里亚;}   {\ fbimajor \ f31503 \ fbidi \ froman \ fcharset0 \ fprq2 {* \ panose   02020603050405020304} Times New Roman;} {\ flominor \ f31504 \ fbidi   \ froman \ fcharset0 \ fprq2 {* \ panose 02020603050405020304}时代新   罗马;} {\ fdbminor \ f31505 \ fbidi \ froman \ fcharset0 \ fprq2 {* \ panose   02020603050405020304} Times New Roman;} {\ fhiminor \ f31506 \ fbidi   \ fswiss \ fcharset0 \ fprq2 {* \ panose 020f0502020204030204} Calibri;}   {\ fbiminor \ f31507 \ fbidi \ froman \ fcharset0 \ fprq2 {* \ panose   02020603050405020304} Times New Roman;} {\ f41 \ fbidi   \ froman \ fcharset238 \ fprq2 Times New Roman CE;} {\ f42 \ fbidi   \ froman \ fcharset204 \ fprq2 Times New Roman Cyr;} {\ f44 \ fbidi   \ froman \ fcharset161 \ fprq2 Times New Roman Greek;} {\ f45 \ fbidi   \ froman \ fcharset162 \ fprq2 Times New Roman Tur;} {\ f46 \ fbidi   \ froman \ fcharset177 \ fprq2 Times New Roman(希伯来语);} {\ f47 \ fbidi   \ froman \ fcharset178 \ fprq2 Times New Roman(Arabic);} {\ f48 \ fbidi   \ froman \ fcharset186 \ fprq2 Times New Roman Baltic;} {\ f49 \ fbidi   \ froman \ fcharset163 \ fprq2 Times New Roman(越南语);} {\ f381 \ fbidi   \ froman \ fcharset238 \ fprq2 Cambria Math CE;} {\ f382 \ fbidi   \ froman \ fcharset204 \ fprq2 Cambria Math Cyr;} {\ f384 \ fbidi   \ froman \ fcharset161 \ fprq2 Cambria Math Greek;} {\ f385 \ fbidi   \ froman \ fcharset162 \ fprq2 Cambria Math Tur;} {\ f388 \ fbidi   \ froman \ fcharset186 \ fprq2 Cambria Math Baltic;} {\ f389 \ fbidi   \ froman \ fcharset163 \ fprq2 Cambria Math(越南语);} {\ f411 \ fbidi   \ fswiss \ fcharset238 \ fprq2 Calibri CE;} {\ f412 \ fbidi   \ fswiss \ fcharset204 \ fprq2 Calibri Cyr;} {\ f414 \ fbidi   \ fswiss \ fcharset161 \ fprq2 Calibri Greek;} {\ f415 \ fbidi   \ fswiss \ fcharset162 \ fprq2 Calibri Tur;} {\ f418 \ fbidi   \ fswiss \ fcharset186 \ fprq2 Calibri Baltic;} {\ f419 \ fbidi   \ fswiss \ fcharset163 \ fprq2 Calibri   (越南语);} {\ flomajor \ f31508 \ fbidi \ froman \ fcharset238 \ fprq2次   新罗马CE;} {\ flomajor \ f31509 \ fbidi \ froman \ fcharset204 \ fprq2次   New Roman Cyr;} {\ flomajor \ f31511 \ fbidi \ froman \ fcharset161 \ fprq2 Times   新罗马希腊语;} {\ flomajor \ f31512 \ fbidi \ froman \ fcharset162 \ fprq2   Times New Roman Tur;} {\ flomajor \ f31513 \ fbidi   \ froman \ fcharset177 \ fprq2 Times New Roman   (希伯来语);} {\ flomajor \ f31514 \ fbidi \ froman \ fcharset178 \ fprq2 Times New   罗马(阿拉伯语);} {\ flomajor \ f31515 \ fbidi \ froman \ fcharset186 \ fprq2   Times New Roman Baltic;} {\ flomajor \ f31516 \ fbidi   \ froman \ fcharset163 \ fprq2 Times New Roman   (越南语);} {\ fdbmajor \ f31518 \ fbidi \ froman \ fcharset238 \ fprq2次   新罗马CE;} {\ fdbmajor \ f31519 \ fbidi \ froman \ fcharset204 \ fprq2次   新罗马Cyr;} {\ fdbmajor \ f31521 \ fbidi \ froman \ fcharset161 \ fprq2   Times New Roman Greek;} {\ fdbmajor \ f31522 \ fbidi   \ froman \ fcharset162 \ fprq2 Times New Roman Tur;} {\ fdbmajor \ f31523 \ fbidi   \ froman \ fcharset177 \ fprq2 Times New Roman(希伯来语);}   {\ fdbmajor \ f31524 \ fbidi \ froman \ fcharset178 \ fprq2 Times New Roman   (阿拉伯语);} {\ fdbmajor \ f31525 \ fbidi \ froman \ fcharset186 \ fprq2 Times New   罗马波罗的海;} {\ fdbmajor \ f31526 \ fbidi \ froman \ fcharset163 \ fprq2次   新罗马(越南语);} {\ fhimajor \ f31528 \ fbidi   \ froman \ fcharset238 \ fprq2 Cambria CE;} {\ fhimajor \ f31529 \ fbidi   \ froman \ fcharset204 \ fprq2 Cambria Cyr;} {\ fhimajor \ f31531 \ fbidi   \ froman \ fcharset161 \ fprq2 Cambria Greek;} {\ fhimajor \ f31532 \ fbidi   \ froman \ fcharset162 \ fprq2 Cambria Tur;} {\ fhimajor \ f31535 \ fbidi   \ froman \ fcharset186 \ fprq2 Cambria Baltic;} {\ fhimajor \ f31536 \ fbidi   \ froman \ fcharset163 \ fprq2坎布里亚   (越南语);} {\ fbimajor \ f31538 \ fbidi \ froman \ fcharset238 \ fprq2次   新罗马CE;} {\ fbimajor \ f31539 \ fbidi \ froman \ fcharset204 \ fprq2次   New Roman Cyr;} {\ fbimajor \ f31541 \ fbidi \ froman \ fcharset161 \ fprq2 Times   新罗马希腊语;} {\ fbimajor \ f31542 \ fbidi \ froman \ fcharset162 \ fprq2   Times New Roman Tur;} {\ fbimajor \ f31543 \ fbidi   \ froman \ fcharset177 \ fprq2 Times New Roman   (希伯来语);} {\ fbimajor \ f31544 \ fbidi \ froman \ fcharset178 \ fprq2 Times New   罗马(阿拉伯语);} {\ fbimajor \ f31545 \ fbidi \ froman \ fcharset186 \ fprq2   Times New Roman Baltic;} {\ fbimajor \ f31546 \ fbidi   \ froman \ fcharset163 \ fprq2 Times New Roman   (越南语);} {\ flominor \ f31548 \ fbidi \ froman \ fcharset238 \ fprq2 Times   新罗马CE;} {\ flominor \ f31549 \ fbidi \ froman \ fcharset204 \ fprq2次   新罗马Cyr;} {\ flominor \ f31551 \ fbidi \ froman \ fcharset161 \ fprq2   Times New Roman Greek;} {\ flominor \ f31552 \ fbidi   \ froman \ fcharset162 \ fprq2 Times New Roman Tur;} {\ flominor \ f31553 \ fbidi   \ froman \ fcharset177 \ fprq2 Times New Roman(希伯来语);}   {\ flominor \ f31554 \ fbidi \ froman \ fcharset178 \ fprq2 Times New Roman   (阿拉伯语);} {\ flominor \ f31555 \ fbidi \ froman \ fcharset186 \ fprq2 Times New   罗马波罗的海;} {\ flominor \ f31556 \ fbidi \ froman \ fcharset163 \ fprq2 Times   新罗马(越南语);} {\ fdbminor \ f31558 \ fbidi   \ froman \ fcharset238 \ fprq2 Times New Roman CE;} {\ fdbminor \ f31559 \ fbidi   \ froman \ fcharset204 \ fprq2 Times New Roman Cyr;} {\ fdbminor \ f31561 \ fbidi   \ froman \ fcharset161 \ fprq2 Times New Roman Greek;}   {\ fdbminor \ f31562 \ fbidi \ froman \ fcharset162 \ fprq2 Times New Roman   Tur;} {\ fdbminor \ f31563 \ fbidi \ froman \ fcharset177 \ fprq2 Times New Roman   (希伯来语);} {\ fdbminor \ f31564 \ fbidi \ froman \ fcharset178 \ fprq2 Times New   罗马(阿拉伯语);} {\ fdbminor \ f31565 \ fbidi \ froman \ fcharset186 \ fprq2   Times New Roman Baltic;} {\ fdbminor \ f31566 \ fbidi   \ froman \ fcharset163 \ fprq2 Times New Roman   (越南语);} {\ fhiminor \ f31568 \ fbidi \ fswiss \ fcharset238 \ fprq2   Calibri CE;} {\ fhiminor \ f31569 \ fbidi \ fswiss \ fcharset204 \ fprq2 Calibri   Cyr;} {\ fhiminor \ f31571 \ fbidi \ fswiss \ fcharset161 \ fprq2 Calibri   希腊语;} {\ fhiminor \ f31572 \ fbidi \ fswiss \ fcharset162 \ fprq2 Calibri Tur;}   {\ fhiminor \ f31575 \ fbidi \ fswiss \ fcharset186 \ fprq2 Calibri   波罗的海;} {\ fhiminor \ f31576 \ fbidi \ fswiss \ fcharset163 \ fprq2 Calibri   (越南语);} {\ fbiminor \ f31578 \ fbidi \ froman \ fcharset238 \ fprq2次   新罗马CE;} {\ fbiminor \ f31579 \ fbidi \ froman \ fcharset204 \ fprq2次   New Roman Cyr;} {\ fbiminor \ f31581 \ fbidi \ froman \ fcharset161 \ fprq2 Times   新罗马希腊语;} {\ fbiminor \ f31582 \ fbidi \ froman \ fcharset162 \ fprq2   Times New Roman Tur;} {\ fbiminor \ f31583 \ fbidi   \ froman \ fcharset177 \ fprq2 Times New Roman   (希伯来语);} {\ fbiminor \ f31584 \ fbidi \ froman \ fcharset178 \ fprq2 Times New   罗马(阿拉伯语);} {\ 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; \ red128 \ green128 \ blue128; \ red192 \ green192 \ blue192;} {* \ defchp   \ fs22 \ loch \ af31506 \ hich \ af31506 \ dbch \ af31505} {* \ defpap \ ql   \ li0 \ RI0 \ SA200 \ sl276 \ slmult1 \ widctlpar \ wrapdefault \ aspalpha \ aspnum \ faauto \ adjustright \ rin0 \ LIN0 \ itap0   } \ noqfpromote {\ stylesheet {\ 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 \湖\ f31506 \ HICH \ af31506 \ DBCH \ af31505 \ CGRID \ langnp1033 \ langfenp1033   \ snext0 \ sqformat \ spriority0正常;} {* \ cs10 \ additive \ ssemihidden   \ sunhideused \ spriority1默认段落字体;} {*   \ TS11 \ tsrowd \ trftsWidthB3 \ trpaddl108 \ trpaddr108 \ trpaddfl3 \ trpaddft3 \ trpaddfb3 \ trpaddfr3 \ tblind0 \ tblindtype3 \ tsvertalt \ tsbrdrt \ tsbrdrl \ tsbrdrb \ tsbrdrr \ tsbrdrdgl \ tsbrdrdgr \ tsbrdrh \ tsbrdrv   \ 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 \湖\ f31506 \ HICH \ af31506 \ DBCH \ af31505 \ CGRID \ langnp1033 \ langfenp1033   \ snext11 \ ssemihidden \ sunhideused Normal Table;}} {* \ rsidtbl   \ rsid664789 \ rsid11549567} {\ mmathPr \ mmathFont34 \ mbrkBin0 \ mbrkBinSub0 \ msmallFrac0 \ mdispDef1 \ mlMargin0 \ mrMargin0 \ mdefJc1 \ mwrapIndent1440 \ mintLim0 \ mnaryLim1} {\信息{\ version0} {\ edmins0} {\ nofpages1} {\ nofwords0} {\ nofchars6}   {* \ company OSD} {\ nofcharsws6} {\ vern49167}} {* \ xmlnstbl {\ xmlns1   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 \ notvatxbx \ krnprsnet \ cachedcolbal   \ 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 \湖\ 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

1 个答案:

答案 0 :(得分:0)

Word添加了许多无用的数据。要最小化rtf,请将其加载到temp richtext框中,然后阅读其RTF符号。

rtbTemp.RTF ="..."

然后阅读rtbTEmp.RTF