James在Word外部有一个程序,该程序会定期自动创建一个小的文本文件。 (文本文件始终具有相同的名称。)James认为,拥有一个宏可以将文本文件导入到Word文档中并在他在文档中定义的书签之后立即插入宏,这将是很好的。

有两种方法可以解决此问题。如果目标是简单地包含文本文件的当前内容,则不需要宏,只需使用INCLUDETEXT字段来引用要包含的文件。每次您更新文档中的字段时,Word都会消失并获取文本文件的当前内容,并将其包含在文档中。

但是,如果要继续将文本文件的当前内容添加到文档中,则需要使用宏。一种简单的方法是以这种方式使用宏本身内的INCLUDETEXT字段:

Sub InsertTextFileAfterBookmark1()

With Selection         .GoTo what:=wdGoToBookmark, Name:="mybmk"

.Fields.Add Range:=Selection.Range, _           Type:=wdFieldIncludeText, Text:="c:\\myfile.txt \c" _           & Chr(32) & "plaintext" & Chr(32) & ""

.MoveLeft , 1         .Fields.Unlink         .MoveEnd     End With End Sub

宏跳转到书签的位置,插入INCLUDETEXT字段,选择该字段,然后取消链接。结果是将文本文件的内容插入到文档中。取消链接该字段的目的实质上是摆脱INCLUDETEXT字段,将其替换为该字段的结果(文件内容)。

要使用该宏,只需更改代码以反映书签的名称以及要插入的文本文件的完整路径。另外,请确保在路径规范内使用双反斜杠;这是域代码正常工作所必需的。

另一种方法是完全放弃INCLUDETEXT字段,而只是插入文件的内容。以下宏版本可以做到这一点:

Sub InsertTextFileAfterBookmark2()

If ActiveDocument.Bookmarks.Exists("mybmk") = True Then         ActiveDocument.Bookmarks("mybmk").Select         Selection.InsertFile FileName:="c:\myfile.txt"

Else         MsgBox "Bookmark ""mybmk"" does not exist!"

End If End Sub

宏检查是否存在名为mybmk的书签(您可以并且应该更改它),然后使用InsertFile方法插入文件的内容。您应该意识到,如所写,宏将覆盖书签。如果要确保书签保持不变,则需要在插入文件之前添加一行代码以将书签折叠到其端点:

Selection.Collapse Direction:=wdCollapseEnd

当然,您的宏可以随心所欲。下面的示例显示了一个功能更全的宏,它使您可以选择指定在书签和要插入的文件内容之间放置多少空间。您需要做的就是确保在点(1),(2)和(3)处调整宏以反映其操作方式。 (宏中的注释解释了期望和选项。)

Sub InsertTextFileAfterBookmark3()

' This macro reads the contents of a specified text file     ' and inserts the text after a particular bookmark in     ' the active document.



Dim InsertSpacer As Integer     Dim FileContent As String

' (1) Pick a number to insert something between the     '     bookmark and the inserted text as spacing:

'     0 = No space. Text is inserted immediately     '         after the bookmark     '     1 = Insert one space between bookmark and text     '     2 = Insert paragraph mark between bookmark and text     '     3 = Insert 2 paragraph marks between bookmark     '         and text

InsertSpacer = 1

' (2) Set a constant for the name of the file to import.

'     Change the file name inside the quotes below to     '     the full path and file name of the text file to     '     import:



Const TextFile As String = "c:\myfile.txt"

' (3) Change the file name in the quotes below to the     '     name of the bookmark after which you want to     '     insert the text:



Const BookmarkName As String = "mybmk"



' Handle errors     On Error GoTo Oops

' Open and grab contents of the file     Open TextFile For Input As #1     FileContent = Input(LOF(1), #1)

Close #1

' Find the bookmark in the active document     Selection.GoTo What:=wdGoToBookmark, Name:="MyBookmark"



' Move the cursor to the end of the bookmark     Selection.MoveRight Unit:=wdCharacter, Count:=1

Select Case InsertSpacer         Case 0             ' Do nothing. Text inserted immediately         Case 1             ' Insert a space             Selection.TypeText Text:=" "

Case 2             'Insert a paragraph mark             Selection.TypeText Text:=vbCrLf         Case 3             'Insert two paragraph marks             Selection.TypeText Text:=vbCrLf & vbCrLf     End Select

' Insert the text file:

Selection.TypeText Text:=FileContent     Exit Sub

Oops:

Select Case Err.Number         Case 55 ' File already open             ' Close the file             Close #1             ' Try again             Resume         Case 53 ' File not found             NotFound = "Could not find the file named: " _             & Chr(34) & TextFile & Chr(34) & vbCrLf _             & vbCrLf & "Verify the file name and path " _             & "in the macro code after " _             & Chr(34) & "Const TextFile As String =" & Chr(34)

MsgBox NotFound, vbOKOnly         Case Else             MsgBox "Error number: " & Err.Number & ", " _             & Err.Description, vbOKOnly     End Select End Sub

注意:

如果您想知道如何使用此页面(或_WordTips_网站上的任何其他页面)中描述的宏,我准备了一个包含有用信息的特殊页面。

链接:/ wordribbon-WordTipsMacros [点击此处在新的浏览器标签中打开该特殊页面]。

_WordTips_是您进行经济有效的Microsoft Word培训的来源。

(Microsoft Word是世界上最流行的文字处理软件。)本技巧(5908)适用于Microsoft Word 2007、2010、2013、2016、2019和Office 365中的Word。 Word的旧菜单界面在这里:

链接:/ word-Importing_a_Text_File_and_Inserting_after_a_Bookmark [导入文本文件并在书签后插入]。