2016-09-28 5 views
0

북마크가 포함 된 워드 파일 "템플릿"(원하는 레이아웃의 단어 문서)에 정보를 붙여 넣으려고하는 Excel 워크 시트가 있습니다. 내가 뭘하고 싶은 것입니다 :복사하여 붙여 넣기 INCLUDING 북마크 VBA

  1. 는 페이지를 삽입, 페이지 하단에 내 시트에
  2. 이동을 데이터로 책갈피를 교체 (북마크 포함) 워드 문서
  3. 복사 모든 휴식 및 복사 된 텍스트를 붙여 포인트를 통해 북마크
  4. 루프를 포함하여 내가 함께 몇 가지 코드를 패치 한

내 엑셀의 모든 행에 대한 2 & 3 파일,하지만 난 책을 얻을 수 없습니다 해요 책갈피를 그대로 붙여 텍스트를 붙여 넣으십시오. 여러분 중 누구라도 저를 도울 수 있습니까?

Sub ReplaceBookmarks 

'Select template 
PickFolder = "C:\Users\Folder" 
Set fdn = Application.FileDialog(msoFileDialogFilePicker) 
With fdn 
    .AllowMultiSelect = False 
    .Title = "Please select the file containing the Template" 
    .Filters.Clear 
    .InitialFileName = PickFolder 
    If .Show = True Then 
    Temp = fdn.SelectedItems(1) 
    End If 
End With 

'open the word document 
Set wdApp = CreateObject("Word.Application") 
Set wdDoc = wdApp.Documents.Open(Temp) 
'show the word document - put outside of loop for speed later 
wdApp.Visible = True 

'Copy everything in word document  
    wdDoc.Application.Selection.Wholestory 
    wdDoc.Application.Selection.Copy 

LastRow2 = 110 ' In real code this is counted on the sheet 
For i = 2 To LastRow2  
'Data that will replace bookmarks in ws2 (defined somewhere in real code) 
    Rf1 = ws2.Cells(i, 4).Value 
    Rf2 = ws2.Cells(i, 2).Value 
    Rf3 = ws2.Cells(i, 3).Value 

'replace the bookmarks with the variables - references sub "Fillbookmark" 
FillBookmark wdDoc, Rf1, "Rf1" 
FillBookmark wdDoc, Rf2, "Rf2" 
FillBookmark wdDoc, Rf3, "Rf3" 

' Jump to bottom of document, add page break and paste 
With wdDoc 
.Application.Selection.EndKey Unit:=wdStory 
.Application.Selection.InsertBreak Type:=wdPageBreak 
.Application.Selection.PasteAndFormat (wdFormatOriginalFormatting) 
End With 
Next i 
End Sub 

Sub FillBookmark(ByRef wdDoc As Object, _ 
ByVal vValue As Variant, _ 
ByVal sBmName As String, _ 
Optional sFormat As String) 

Dim wdRng As Object 

'store the bookmarks range 
Set wdRng = wdDoc.Bookmarks(sBmName).Range 
'if the optional format wasn’t supplied 
If Len(sFormat) = 0 Then 
'replace the bookmark text 
    wdRng.Text = vValue 
Else 
'replace the bookmark text with formatted text 
    wdRng.Text = Format(vValue, sFormat) 
End If 
End Sub 

답변

1

먼저 WordOpenXml을 사용하여 복사/붙여 넣기 대신 시도해보십시오. 복사/붙여 넣기보다 훨씬 안정적입니다. 이제 북마크는 명명 된 위치라는 것을 기억하십시오. 문서의 섹션을 복사하고 원래 북마크가 그대로 있으면 다른 위치에 다시 올려 놓으면 새 섹션에 복사 된 북마크가 표시되지 않습니다.

Sub Test() 

    ActiveDocument.Bookmarks.Add Name:="BM1", Range:=ActiveDocument.Paragraphs(1).Range 

    ActiveDocument.Application.Selection.WholeStory 

    Dim openxml As String 
    openxml = ActiveDocument.Application.Selection.wordopenxml 

    ActiveDocument.Bookmarks(1).Delete 

    With ActiveDocument 
     .Application.Selection.EndKey Unit:=wdStory 
     .Application.Selection.InsertBreak Type:=wdPageBreak 
     .Application.Selection.InsertXML xml:=openxml 
    End With 

'  ActiveDocument.Bookmarks(1).Delete 

    With ActiveDocument 
     .Application.Selection.EndKey Unit:=wdStory 
     .Application.Selection.InsertBreak Type:=wdPageBreak 
     .Application.Selection.InsertXML xml:=openxml 
    End With 
End Sub 

이제 새 문서가 문서에서 텍스트로 =Rand()를 입력하여 텍스트를 입력 열고 다음 코드를 실행을 Enter 키를 누르십시오 :

나는 당신이 보여 코드의 약간을 제공합니다 테스트 매크로에서.

ActiveDocument.Bookmarks(1).Delete을 사용하여 북마크를 삭제했기 때문에 처음 삽입 된 텍스트에 북마크가 포함되어 있고 두 번째 북마크에는 원래 북마크가 포함되어 있습니다.

' ActiveDocument.Bookmarks(1).Delete 행의 주석 처리를 제거하면 두 번째 섹션을 만들 때 책갈피가 더 이상 없기 때문에 두 번째 추가 된 텍스트 부분에 책갈피가 표시됩니다.

요약하면 책갈피를 복사해도 붙여 넣을 때 책갈피가 복제되지 않으므로 원래 책갈피를 삭제하거나 책갈피를 다시 고유하게 만들기 위해 이름을 변경해야합니다. 중복은 없어지지 않습니다.

+0

이것은 매우 통찰력이있었습니다, 감사합니다 !!! 모든 것을 복사 한 후에 책갈피를 삭제하기 시작했습니다. 북마크 작업을 처음 수행 한 것이므로 정보를 통해 많은 작업을 절약 할 수있었습니다. 너는 보스 야. @Maarten van Stam –