1
我写了一个VBA Word宏,它读取一个.txt
文件,将其复制并粘贴到Word文档中,设置一种新字体。Word VBA:查找行并替换字体
所有工作正常!现在我想强调一些bold + italic
字体的特定行,但我无法弄清楚一个工作解决方案。
特定行以特定词开头(例如Simulation Nr.xxx
),或者以某些词开头,但它们有很长的一系列空格(例如Turbine
)。
我该如何解决问题?
P.s .:这里是将.txt文件复制/粘贴到word文档中的工作代码。
Sub ACTUS_Table_Converter()
Dim pName As String
Dim bDoc As Document
Dim AppPath, ThisPath As String
Dim Rng As Range
ThisPath = ActiveDocument.Path
pName = ActiveDocument.Name
With Dialogs(wdDialogFileOpen)
If .Display Then
If .Name <> "" Then
Set bDoc = Documents.Open(.Name)
AppPath = bDoc.Path
End If
Else
MsgBox "No file selected"
End If
End With
Call ReplaceAllxSymbolsWithySymbols
Call ChangeFormat
Selection.Copy
Windows(pName).Activate
Selection.Paste
Selection.Collapse
bDoc.Close savechanges:=False
End Sub
Sub ChangeFormat()
Selection.WholeStory
With Selection.Font
.Name = "Courier New"
.Size = 6
End With
End Sub
Sub ReplaceAllxSymbolsWithySymbols()
'Call the main "ReplaceAllSymbols" macro (below),
'and tell it which character code and font to search for, and which to replace with
Call ReplaceAllSymbols(FindChar:=ChrW(-141), FindFont:="(normal text)", _
ReplaceChar:=ChrW(179), ReplaceFont:="(normal text)")
Call ReplaceAllSymbols(FindChar:=ChrW(-142), FindFont:="(normal text)", _
ReplaceChar:=ChrW(178), ReplaceFont:="(normal text)")
Call ReplaceAllSymbols(FindChar:=ChrW(-144), FindFont:="(normal text)", _
ReplaceChar:=ChrW(176), ReplaceFont:="(normal text)")
Call ReplaceAllSymbols(FindChar:="°", FindFont:="(normal text)", _
ReplaceChar:="", ReplaceFont:="(normal text)")
End Sub
Sub ReplaceAllSymbols(FindChar As String, FindFont As String, _
ReplaceChar As String, ReplaceFont As String)
Dim FoundFont As String, OriginalRange As Range, strFound As Boolean
Application.ScreenUpdating = False
Set OriginalRange = Selection.Range
'start at beginning of document
ActiveDocument.Range(0, 0).Select
strFound = False
If ReplaceChar = "" Then
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = FindChar
.Replacement.Text = ReplaceChar
.Replacement.Font.Name = "Courier New"
.Replacement.Font.Size = 6
.MatchCase = True
End With
If Selection.Find.Execute Then
Selection.Delete Unit:=wdCharacter, Count:=2
Selection.TypeText ("°C")
End If
Else
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = FindChar
.Replacement.Text = ReplaceChar
.Replacement.Font.Name = "Courier New"
.Replacement.Font.Size = 6
.MatchCase = True
.Execute Replace:=wdReplaceAll
End With
End If
OriginalRange.Select
Set OriginalRange = Nothing
Application.ScreenUpdating = True
Selection.Collapse
End Sub
谢谢!这对我有帮助,但不解决第二种情况,即随机字加上许多空格。我该如何解决它?我可能不得不使用某种通配符,但我不知道如何说“搜索未知的词+空格”。你可能知道吗?提前致谢。 MLC – user2699187
您应该清楚地定义您正在搜索的内容。什么被视为“未知词”?多少空间?有没有一种模式? – etaiso
我有以下问题: 带有一些标题的结构化文本,我想用粗体字突出显示。这些标题很多,我无法为每个标题定义一个查找和替换,但是我知道如果在该行中有一个标题,则该标题词后面至少有25个空格。我如何搜索并找到这些行并将其字体更改为粗体? 谢谢 – user2699187