2017-10-29 142 views
0

在我的MS-Word模板,我的客户端发送模板与头部段错误,如下面,文本替换

佛蒙特州,ROBIN S.日期:2017年10月21日 文件编号: 312335出生日期:05/02/1967Claim#:RE155B53452 DOI:06/21/2017

错误是'Claim#'出现在DOB值的旁边,而它应该出现在下一行,如下:

VERMONT,ROBIN S.日期:10/21/2017 文件编号:312335出生日期:05/02/1967 索赔号: RE155B53452 DOI:2017年6月21日

而且,这个错误偶尔出现在一些文件,而不是全部,所以要解决这个问题,我创建了一个Word宏,如下:

Sub ShiftClaimNumber2NextLine() 

    Dim rngStory As Word.Range 
    Dim lngJunk As Long 
    'Fix the skipped blank Header/Footer problem as provided by Peter Hewett 
    lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType 
    'Iterate through all story types in the current document 
    For Each rngStory In ActiveDocument.StoryRanges 
    'Iterate through all linked stories 
    Do 
     With rngStory.Find 
     .text = "Claim#:" 
     .Replacement.text = "^pClaim#:" 
     .Wrap = wdFindContinue 
     .Execute Replace:=wdReplaceAll 
     End With 
     'Get next linked story (if any) 
     Set rngStory = rngStory.NextStoryRange 
    Loop Until rngStory Is Nothing 
    Next 

End Sub 

我跑上面的宏通过组宏,如下:

Sub ProcessAllDocumentsInFolder() 

Dim file 
Dim path As String 

' Path to folder 
path = "D:\Test\" 
file = Dir(path & "*.doc") 
Do While file <> "" 
Documents.Open FileName:=path & file 

' Call the macro to run on each file in the folder 
Call ShiftClaimNumber2NextLine 

' Saves the file 
ActiveDocument.Save 
ActiveDocument.Close 

' set file to next in Dir 
file = Dir() 
Loop 

End Sub 

当运行宏ProcessAllDocumentsInFolder(),用于与这样的错误中的所有文件,它改变了“权利要求#”到下一行。

然而,问题是,它也确实所以没有这样的问题的文件,从而增加一个输入线DOB下方,如下(按黄线如图所示):

enter image description here

我应当怎​​样更改我的宏ShiftClaimNumber2NextLine(),所以它不会进行任何更改文件,这DO NOT HAVE的“声明#”的问题?

回答

0

这是最适合于正则表达式的情况见下面的链接,类似的问题,请参阅VBA解决方案

https://superuser.com/questions/846646/is-there-a-way-to-search-for-a-pattern-in-a-ms-word-document

Sub RegexReplace() 

    Dim RegEx As Object 
    Set RegEx = CreateObject("VBScript.RegExp")  
    On Error Resume Next 

    RegEx.Global = True 
    RegEx.Pattern = "[0-9]Claim#:" 
    ActiveDocument.Range = _ 
    RegEx.Replace(ActiveDocument.Range, "\rClaim#:")   

End Sub 
+0

谢谢。但不工作。 – vicki