以编程方式在Word中设置文本格式 [英] Formatting text in Word programmatically

查看:75
本文介绍了以编程方式在Word中设置文本格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将格式(粗体,斜体,下划线)应用于Word文档中的段落时遇到问题.当我阅读带有词对象范围类的para并在文本上应用字体样式时,在该文本中也有[某些文本(12.5)].所以对于打开方括号和圆形制动环遍历每个打开托架两次,我想知道原因和解决方法

例如

I am facing problem while applying formatting(bold,italics,underline) to the paragraph written in word document. when i am reading para with range class of word object and appling font-style to the text and in that text there are brakets also like [ some text (12.5)]. so for opening square bracket and round braket loop traverse for two times each opening bracket so i want know the reason and solution

e.g.

For iNum = 1 To myRange.Words.Count
     temp = aWord.Text   'aWord is a word object
Next


用于打开括号,我两次以aWord.Text的形式获取[[]文本,因此请帮助<​​br/>
谢谢

[edit]仅标记-即VB代码,而不是C#-OriginalGriff [/edit]
[edit]预标记中包含的代码块-losmac [/edit]


for openting bracket i am getting "[" text as aWord.Text for two times so pls help

Thanks

[edit]Tags only - that is VB code, not C# - OriginalGriff[/edit]
[edit]Code block included in pre tags - losmac[/edit]

推荐答案

实现此目标的简单方法是:
Simple way to achieve this is:
Option Explicit

Sub GetSomeBrackets()
Dim doc As Document, par As Paragraph, rng As Range
Dim bPos As Long, ePos As Long, sText As String

Set doc = ThisDocument
For Each par In doc.Paragraphs
    sText = par.Range.Text
    bPos = InStr(1, sText, "[")
    ePos = InStr(bPos + 1, sText, "]")
    If bPos > 0 And ePos > 0 Then
        Set rng = par.Range
        rng.MoveStart Unit:=wdCharacter, Count:=bPos - 1
        rng.End = rng.Characters(Int(ePos - bPos) + 1).End
        rng.Select
        rng.Bold = True
        MsgBox rng.Text, vbInformation, Len(rng.Text)
        'MsgBox Mid(sText, bPos, ePos - bPos)
        Set rng = Nothing
    End If
Next
Set doc = Nothing
Set par = Nothing


End Sub


这篇关于以编程方式在Word中设置文本格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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