2016-07-07 1 views
0

MS Word의 북마크에 Excel의 콘텐츠를 복사하려고합니다. 하지만 424 런타임 오류가 발생합니다. 친절하게 도와주세요. 나는 시각 기초와 프로그래밍에있어서 아주 새로운 것이다. 내 코드를 첨부했습니다. 감사Excel에서 MS Word로 콘텐츠를 복사하려고합니다.

Sub WordDoc() 
Dim wrdApp As Object 
Dim Number As String 
Dim wrdDoc As Object 
Set wrdApp = CreateObject("Word.Application") 
wrdApp.Visible = True 
Set wrdDoc = wrdApp.Documents.Open("H:\IP Automation\createDoc.docx") 
Number = Worksheets("Sheet1").Range("A2") 
Call InsBookmark(ID, Number) 
End Sub 


Sub InsBookmark(strBMName, strVariable) 
If strVariable <> "" Then 
If ActiveDocument.Bookmarks.Exists(ID) Then 
ActiveDocument.Bookmarks(ID).Select 
Selection.Delete 
Selection.InsertAfter (strVariable) 

End If 
End If 
End Sub 
+0

어디에서 오류가 발생합니까? – RGA

답변

1

워드 문서 그래서 "ActiveDocument를"늘 작품들을 지속되지 것과 같이, 두 개의 서브 우퍼로이 문제를 분리해서 안된다. 두 번째 하위 코드를 첫 번째 코드로 복사하고 바꾸기 ActiveDocumentwrdDoc

0

으로 설정하면됩니다. 그것을 가지고 가서 어떻게 지내는지보십시오.

Sub Export_Table_Word() 

    'Name of the existing Word doc. 
    Const stWordReport As String = "Final Report.docx" 

    'Word objects. 
    Dim wdApp As Word.Application 
    Dim wdDoc As Word.Document 
    Dim wdbmRange As Word.Range 

    'Excel objects. 
    Dim wbBook As Workbook 
    Dim wsSheet As Worksheet 
    Dim rnReport As Range 

    'Initialize the Excel objects. 
    Set wbBook = ThisWorkbook 
    Set wsSheet = wbBook.Worksheets("PwC Contact Information") 
    Set rnReport = wsSheet.Range("Table1") 

    'Initialize the Word objets. 
    Set wdApp = New Word.Application 
    Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordReport) 
    Set wdbmRange = wdDoc.Bookmarks("Report").Range 


    Dim tbl As Table 
    For Each tbl In wdDoc.Tables 
     tbl.Delete 
    Next tbl 


    'If the macro has been run before, clean up any artifacts before trying to paste the table in again. 
    On Error Resume Next 
    With wdDoc.InlineShapes(1) 
     .Select 
     .Delete 
    End With 
    On Error GoTo 0 

    'Turn off screen updating. 
    Application.ScreenUpdating = False 

    'Copy the report to the clipboard. 
    rnReport.Copy 

    'Select the range defined by the "Report" bookmark and paste in the report from clipboard. 
    With wdbmRange 
     .Select 
     .Paste 
    End With 

    'Save and close the Word doc. 
    With wdDoc 
     .Save 
     .Close 
    End With 

    'Quit Word. 
    wdApp.Quit 

    'Null out your variables. 
    Set wdbmRange = Nothing 
    Set wdDoc = Nothing 
    Set wdApp = Nothing 

    'Clear out the clipboard, and turn screen updating back on. 
    With Application 
     .CutCopyMode = False 
     .ScreenUpdating = True 
    End With 

    MsgBox "The report has successfully been " & vbNewLine & _ 
      "transferred to " & stWordReport, vbInformation 

End Sub 
관련 문제