用词VBA查找以"Date:"开头的行.并将行复制到相应页面的顶部 [英] Word VBA to find line starting with "Date:" and copy line to top of respective page

查看:294
本文介绍了用词VBA查找以"Date:"开头的行.并将行复制到相应页面的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA for Word的新手(在Excel中已经使用了相当多的功能).我正在尝试整理包含复制和粘贴电子邮件的大型Word文档.我想查找每封电子邮件的日期/时间,然后将其复制并粘贴到页面顶部.所有包含日期​​的行都以"Date:"开头,因此很容易找到它们.我编写了一个代码,尝试将其复制到页面顶部,但是当前将所有日期行粘贴到文档顶部.我明白为什么,我只是想不出如何更改它.

I'm new to VBA for Word (have used it a fair bit in Excel). I am trying to organise large word documents that contain copied and pasted emails. I want to find the date/time of each email and copy and paste it to the top of the page. All lines containing the date start with "Date:" so it is easy enough to find them. I wrote a code to try and copy them to the tops of pages but it currently pastes all of the date lines to the top of the document. I can see why, I just can't work out how to change it.

然后我将能够做的是将每页的第一行变成一个标题,以便进行排序.

What I will then be able to do is make the first line of each page into a heading which I can sort by.

我的初始代码如下:

Sub Copy_Dates_to_Top()
If Selection.StoryType <> wdMainTextStory Then
    With ActiveDocument.ActiveWindow.View
        .Type = wdPrintView
        .SeekView = wdSeekMainDocument
    End With
End If

Selection.HomeKey Unit:=wdStory
With Selection.Find
    .Text = "Date: "
    .Format = False
    .Forward = True
    .MatchWildcards = False
    .Wrap = wdFindStop
    While .Execute
        Selection.Expand Unit:=wdLine
        Selection.Copy ' Unit:=wdLine
        Selection.GoTo What:=wdGoToBookmark, Name:="\Page"
        Selection.MoveLeft Unit:=wdCharacter, Count:=1
        Selection.Paste
    Wend
End With
End Sub

推荐答案

通过对日期应用唯一的样式,然后通过页面标题中的STYLEREF字段引用该样式,您可以轻松实现此目的.例如,下面的宏为此使用了Word的内置强"字符样式.

You can achieve this quite easily by applying a unique Style to the dates, then referencing that Style via a STYLEREF field in the page header. For example, the following macro employs Word's built-in 'Strong' character Style for this.

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument
  With .Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Date: [0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"
    .Replacement.Text = "^&"
    .Forward = True
    .Format = True
    .Replacement.Style = "Strong"
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
  .Fields.Add Range:=.Sections.First.Headers(wdHeaderFooterPrimary).Range, _
    Type:=wdFieldEmpty, Text:="STYLEREF Strong", PreserveFormatting:=False
End With
Application.ScreenUpdating = True
End Sub

Word的强"样式采用粗体格式,这使得日期在文档正文中也更加突出.

Word's 'Strong' Style applies bold formatting, which make the dates stand out more in the document body also.

注意:该宏假定您的日期采用d/m/y或m/d/y格式;可以更改查找表达式以匹配其他日期格式.

Note: The macro assumes your dates are in either a d/m/y or m/d/y format; the Find expression could be changed to match a different date format.

这篇关于用词VBA查找以"Date:"开头的行.并将行复制到相应页面的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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