简体   繁体   English

vba word 宏将字符串放入现有标题

[英]vba word macro to put a string to an existing Heading

i am attempting to write a macro that with find/replace a string and than move it to an existing heading.我正在尝试编写一个宏来查找/替换字符串,然后将其移动到现有标题。 The original text is like this:原文是这样的:

1. Heading 1 1. 标题 1

ID : abcd身份证号:abcd

1.1 Heading 2 1.1 标题 2

ID : abcd身份证号:abcd

And it should look like:它应该看起来像:

1.Heading 1 abcd 1.Heading 1 abcd

1.1 Heading 2 abcd 1.1 标题 2 abcd

I am having some problems with the code i tried to write, mostly because i am kinda new, but this is what i created so far:我尝试编写的代码有一些问题,主要是因为我有点新,但这是我迄今为止创建的:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Style = "Heading 2"
With Selection.Find
    .Text = "abcd"
    .Replacement.Text = "abcd^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False

End With
Selection.Find.Execute Replace:=wdReplaceAll

The text is not so important because i managed to replace with what i want but i don't know how to align it with the Heading style.. Thanks文本不是那么重要,因为我设法用我想要的替换了但我不知道如何将它与标题样式对齐..谢谢

EDIT: I hope i don't screw up again, sorry big :).编辑:我希望我不要再搞砸了,抱歉大:)。 So i have raw which is the raw text and i want to process it to look like this final .所以我有raw这是原始文本,我想处理它看起来像这个final I already found out, thanks to you how to replace the text, it's just that i stuck in the raw version.我已经发现了,感谢您如何替换文本,只是我停留在原始版本中。 Thanks, I kinda own you a beer, or two谢谢,我有点拥有你一两杯啤酒

LATER EDIT: So i have 5 types of Heading formats, 1. Heading 1, 1.1 Heading 2 etc till 5, and all of them have below them an ID, each with a specific number, but the name is the same, ID ASD_PC_AWP_[XXXX].后期编辑:所以我有 5 种类型的标题格式, 1. 标题 1、1.1 标题 2等等,直到 5,并且它们下面都有一个 ID,每个都有一个特定的编号,但名称相同, ID ASD_PC_AWP_[ XXXX]。 I just have to get rid of ID ASD_PC_ and put AWP_[xxxx] at same level of the Heading eg: 1.Heading 1 AWP_[xxxx1] ** , **2.我只需要摆脱ID ASD_PC_ 并将 AWP_[xxxx] 放在标题的同一级别,例如: 1.Heading 1 AWP_[xxxx1] ** , **2。 Heading 2 AWP_[xxx2] ...标题 2 AWP_[xxx2] ...

Do a wildcard find for any paragraph marker which is followed by ID:.对任何后跟 ID: 的段落标记进行通配符查找。

.Text = "^13ID:" .Text = "^13ID:"
.Replacement.Text = "" .Replacement.Text = ""

You will need to specify the style of the replacement text to the heading style because when you delete the paragraph marker at the end of the Heading paragraph you will also delete the style information for the heading paragraph.您需要将替换文本的样式指定为标题样式,因为当您删除标题段落末尾的段落标记时,您还将删除标题段落的样式信息。

You will need to do this with every style heading followed by the ID: text.您需要对每个样式标题后跟 ID: 文本执行此操作。

Updated 2018-11-01 2018-11-01 更新

The following code should work.以下代码应该可以工作。 I got some hints from Macropods ingeneous code.我从 Macropods 巧妙的代码中得到了一些提示。

Update 2 2018-11-01更新 2 2018-11-01

Revised to work with a list of styles defined by user at OPs request修改为使用用户根据 OP 请求定义的样式列表

Sub ConsolidateHeadingWithID()

Const HEADINGS                                   As String = "Heading 1,Heading 2,Heading 3,Heading 4,Heading 5,Other style,another style"

Dim my_headings                                 As Variant
Dim my_heading                                  As Variant
my_headings = Split(HEADINGS, ",")

For Each my_heading In my_headings

        With ActiveDocument.StoryRanges(wdMainTextStory)

            With .Find

                .ClearFormatting
                .format = True
                .Text = ""
                .Style = my_heading
                .MatchWildcards = True
                .Wrap = wdFindStop
                .Execute

            End With

            Do While .Find.Found

                If .Duplicate.Next(unit:=wdWord).Text = "ID" Then

                    .Duplicate.Next(unit:=wdParagraph).Style = my_heading

                End If

                .Collapse wdCollapseEnd
                .MoveStart unit:=wdCharacter, Count:=2
                .Find.Execute

            Loop

        End With

        With ActiveDocument.Range.Find

            .ClearFormatting
            .format = True
            .Text = "(^13)(ID:)(*)(AWP_)([0-9]{1,})"
            .Style = my_heading
            .Replacement.Text = " [\4\5]"
            .MatchWildcards = True
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll

        End With

    Next

End Sub

Try:尝试:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "ID:*^13"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    Set Rng = .Duplicate.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
    Rng.End = Rng.Paragraphs.First.Range.End - 1
    Rng.InsertAfter Split(Split(.Duplicate.Text, ":")(1), vbCr)(0)
    .Text = vbNullString
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM