Suman은 특정 문서의 10 페이지에 이미지를 삽입하기 위해 매크로를 사용해야합니다. 그는 매크로 내에서이 문제를 해결하는 가장 좋은 방법이 무엇인지 궁금합니다.

이미지 만 10 페이지에 표시하려면 작업이 매우 쉽습니다. 매크로를 사용하여 10 페이지를 찾고 이미지를 삽입 한 다음 이미지 뒤에 페이지 나누기를 삽입합니다. 그러면 이전 페이지 10이 11 페이지가되고 11 페이지가 12 페이지가되는 식으로 이미지 뒤의 모든 내용이 페이지 단위로 올라갑니다.

Sub InsertImage1()

Dim PicPath As String

PicPath = "C:\My Pictures\My Scans\scan0002.jpg"

ActiveDocument.GoTo(What:=wdGoToPage, Count:=10).Select     Selection.InlineShapes.AddPicture FileName:=PicPath, _       LinkToFile:=False, SaveWithDocument:=True     Selection.InsertBreak Type:=wdPageBreak End Sub

매크로는 이미지를 인라인 모양으로 삽입합니다. 원하는 경우 이미지를 플로팅 모양으로 삽입하여 텍스트를 이미지 주위로 감싸도록 할 수 있습니다. 다음 매크로는 페이지 중앙에 이미지를 중앙에 배치 할뿐만 아니라이를 수행합니다.

Sub InsertImage2()

Dim PicPath As String     Dim aShape As Shape

PicPath = "C:\My Pictures\My Scans\scan0002.jpg"

ActiveDocument.GoTo(What:=wdGoToPage, Count:=10).Select     Set aShape = Selection.InlineShapes.AddPicture(FileName:=PicPath, _       LinkToFile:=False, SaveWithDocument:=True).ConvertToShape     With aShape         .WrapFormat.Type = wdWrapTight         .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage         .RelativeVerticalPosition = wdRelativeVerticalPositionPage         .Top = wdShapeCenter         .Left = wdShapeCenter         .Select     End With End Sub

이미지가 기존 이미지 (업데이트 된 이미지로 대체 될 수 있음)를 대체하려면 좀 더 복잡한 매크로를 사용하면됩니다.

다음 매크로는 원하는 페이지에서 첫 번째 이미지를 찾아 인라인 모양으로 바꿉니다.

Sub ReplaceImage()

Dim r1 As Range     Dim r2 As Range     Dim nPages As Long     Dim nP As Long     Dim PicPath As String     Dim sTemp As String

PicPath = "C:\My Pictures\My Scans\scan0002.jpg"

nP = 10    ' Page number on which to replace the image

sTemp = ""

nPages = ActiveDocument.ComputeStatistics(statistic:=wdStatisticPages)

If nP > nPages Then         sTemp = "You are trying to go to a non-existent page"

Else         Set r1 = ActiveDocument.GoTo(What:=wdGoToPage, Count:=nP)

If nP = nPages Then             Set r2 = ActiveDocument.Range             r1.End = r2.End         Else             Set r2 = ActiveDocument.GoTo(What:=wdGoToPage, Count:=nP + 1)

r1.End = r2.Start - 2         End If         If r1.InlineShapes.Count = 0 Then             sTemp = "Page " & nP & " does not have an inline picture"

Else             r1.InlineShapes(1).Select             Selection.InlineShapes.AddPicture FileName:=PicPath, _               LinkToFile:=False, SaveWithDocument:=True         End If     End If     If sTemp > "" Then MsgBox sTemp End Sub

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

(Microsoft Word는 세계에서 가장 인기있는 워드 프로세싱 소프트웨어입니다.)이 팁 (13139)은 Microsoft Word 2007, 2010, 2013, 2016, 2019 및 Office 365의 Word에 적용됩니다.