我正在MS Word中使用“查找和替换”脚本/宏。对于以下两行,我如何将其调整为区分大小写?现在它将取代我们,巴士,等等。
Const strFind As String =“ US” const strRepl As String =“ USA”
Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Dim oStory As Range
Dim oRng As Range
Const strFind As String = "2017"
Const strRepl As String = "2018"
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1) & "\"
End With
strFileName = Dir$(strPath & "*.docx")
While Len(strFileName) <> 0
WordBasic.DisableAutoMacros 1
Set oDoc = Documents.Open(strPath & strFileName)
For Each oStory In ActiveDocument.StoryRanges
Set oRng = oStory
With oRng.Find
Do While .Execute(FindText:=strFind)
oRng.Text = strRepl
oRng.Collapse wdCollapseEnd
Loop
End With
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
Set oRng = oStory
With oRng.Find
Do While .Execute(FindText:=strFind)
oRng.Text = strRepl
oRng.Collapse wdCollapseEnd
Loop
End With
Wend
End If
Next oStory
oDoc.SaveAs FileName:=strPath & strFileName
oDoc.Close SaveChanges:=wdDoNotSaveChanges
strFileName = Dir$()
WordBasic.DisableAutoMacros 0
Wend
Set oDoc = Nothing
Set oStory = Nothing
Set oRng = Nothing
End Sub
针对以下帖子。我已经添加了整个代码。
答案 0 :(得分:0)
“查找和替换”方法具有布尔MatchCase属性。将其设置为True。
示例:在您的DoWhile代码中。在执行.Execute(FindText:= strFind,MatchCase:= True)
答案 1 :(得分:0)
如果您要搜索的整体单词也可能存在于较大的字符串中,则仅匹配大小写是不够的。试试:
from rpy2.robjects.packages import importr, data
# IMPORT R PACKAGES
base = importr('base')
utils = importr('utils')
datasets = importr('datasets')
stats = importr('stats', robject_translations={'as.formula': 'as_formula'})
graphics = importr('graphics')
grDevices = importr('grDevices')
brms = importr('brms')
# LOADING DATA
# WORKING EXAMPLE: mtcars = data(datasets).fetch('mtcars')['mtcars']
kidney_df = data(brms).fetch('kidney')['kidney']
print(utils.head(kidney_df, n = 3))
# MODELING
formula1 = stats.as_formula("time | cens(censored) ~ age + sex + disease")
fit1 = brms.brm(formula1, data = kidney_df, family = "weibull", inits = "0")
print(stats.summary(fit1))
formula2 = stats.as_formula("time | cens(censored) ~ age + sex + disease + (1|patient)")
fit2 <- brms.brm(formula2, data = kidney_df, family = "weibull", inits = "0",
prior = brms.set_prior("cauchy(0,2)", class = "sd"))
print(stats.summary(fit2))
# GRAPHING
grDevices.png('/path/to/plot1.png')
graphics.plot(fit1)
grDevices.dev_off()
grDevices.png('/path/to/plot2.png')
graphics.plot(fit2)
grDevices.dev_off()
请注意,我使用了通配符,并与Find表达式结合使用。这样可以保证只匹配整个大写单词。您可以使用以下方法实现相同的目标:
Sub BatchProcess()
Application.ScreenUpdating = False
Dim strFileName As String, strPath As String
Dim oDoc As Document, oStory As Range
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , "List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1) & "\"
End With
strFileName = Dir$(strPath & "*.docx")
WordBasic.DisableAutoMacros 1
While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
With oDoc
For Each oStory In .StoryRanges
While Not (oStory Is Nothing)
oStory.Find.Execute FindText:="<US>", Replacewith:="USA", Forward:=True, _
Wrap:=wdFindContinue, MatchWildcards:=True, Replace:=wdReplaceAll
Set oStory = oStory.NextStoryRange
Wend
Next oStory
.SaveAs FileName:=strPath & strFileName
.Close SaveChanges:=wdDoNotSaveChanges
End With
strFileName = Dir$()
Wend
WordBasic.DisableAutoMacros 0
Set oDoc = Nothing: Set oStory = Nothing
Application.ScreenUpdating = True
End Sub
还要注意,代码的整体简化。