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 사이트의 다른 페이지)에 설명 된 매크로를 사용하는 방법을 알고 싶다면 유용한 정보가 포함 된 특별 페이지를 준비했습니다.

link : / wordribbon-WordTipsMacros [새 브라우저 탭에서 특별 페이지를 열려면 여기를 클릭하세요].

_WordTips_는 비용 효율적인 Microsoft Word 교육을위한 소스입니다.

(Microsoft Word는 세계에서 가장 널리 사용되는 워드 프로세싱 소프트웨어입니다.)이 팁 (12193)은 Microsoft Word 97, 2000, 2002 및 2003에 적용됩니다. Word의 리본 인터페이스에 대한이 팁 버전 (Word 2007)을 찾을 수 있습니다. 이후) 여기 :

link : / wordribbon-Importing_a_Text_File_and_Inserting_after_a_Bookmark [텍스트 파일 가져 오기 및 북마크 뒤에 삽입].