Xin gỡ rối - VBA (Use-defined type not defined) (1 người xem)

  • Thread starter Thread starter KhoiSMC
  • Ngày gửi Ngày gửi
Liên hệ QC

Người dùng đang xem chủ đề này

KhoiSMC

Thành viên thường trực
Tham gia
19/6/09
Bài viết
248
Được thích
32
Xin các bạn xem giúp mình code sau mình muốn xuất comments từ word sang excel như sau:

Mã:
[COLOR=#333333][FONT=Segoe UI]Sub exportComments()[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]' Exports comments from a MS Word document to Excel and associates them with the heading paragraphs[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]' they are included in. Useful for outline numbered section, i.e. 3.2.1.5....[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]' Thanks to Graham Mayor, http://answers.microsoft.com/en-us/office/forum/office_2007-customize/export-word-review-comments-in-excel/54818c46-b7d2-416c-a4e3-3131ab68809c[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]' and Wade Tai, http://msdn.microsoft.com/en-us/library/aa140225(v=office.10).aspx[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]' Need to set a VBA reference to "Microsoft Excel 14.0 Object Library"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]
[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI][B]Dim xlApp As Excel.Application ' <-- [COLOR=#ff0000]Lỗi [FONT=Verdana]Use-defined type not defined tại đây[/FONT][/COLOR][/B][/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Dim xlWB As Excel.Workbook[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Dim i As Integer, HeadingRow As Integer[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Dim objPara As Paragraph[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Dim objComment As Comment[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Dim strSection As String[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Dim strTemp[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Dim myRange As Range[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]
[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Set xlApp = CreateObject("Excel.Application")[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]xlApp.Visible = True[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Set xlWB = xlApp.Workbooks.Add 'create a new workbook[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]With xlWB.Worksheets(1)[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]' Create Heading[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    HeadingRow = 1[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    .Cells(HeadingRow, 1).Formula = "Comment"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    .Cells(HeadingRow, 2).Formula = "Page"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    .Cells(HeadingRow, 3).Formula = "Paragraph"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    .Cells(HeadingRow, 4).Formula = "Comment"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    .Cells(HeadingRow, 5).Formula = "Reviewer"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    .Cells(HeadingRow, 6).Formula = "Date"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    [/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    strSection = "preamble" 'all sections before "1." will be labeled as "preamble"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    strTemp = "preamble"[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    If ActiveDocument.Comments.Count = 0 Then[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        MsgBox ("No comments")[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        Exit Sub[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    End If[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    [/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    For i = 1 To ActiveDocument.Comments.Count[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        Set myRange = ActiveDocument.Comments(i).Scope[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        strSection = ParentLevel(myRange.Paragraphs(1)) ' find the section heading for this comment[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        'MsgBox strSection[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        .Cells(i + HeadingRow, 1).Formula = ActiveDocument.Comments(i).Index[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        .Cells(i + HeadingRow, 2).Formula = ActiveDocument.Comments(i).Reference.Information(wdActiveEndAdjustedPageNumber)[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        .Cells(i + HeadingRow, 3).Value = strSection[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        .Cells(i + HeadingRow, 4).Formula = ActiveDocument.Comments(i).Range[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        .Cells(i + HeadingRow, 5).Formula = ActiveDocument.Comments(i).Initial[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        .Cells(i + HeadingRow, 6).Formula = Format(ActiveDocument.Comments(i).Date, "dd/MM/yyyy")[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        .Cells(i + HeadingRow, 7).Formula = ActiveDocument.Comments(i).Range.ListFormat.ListString[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    Next i[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]End With[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Set xlWB = Nothing[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Set xlApp = Nothing[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]End Sub[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]
[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]
[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Function ParentLevel(Para As Word.Paragraph) As String[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]'From Tony Jollans[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]' Finds the first outlined numbered paragraph above the given paragraph object[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    Dim ParaAbove As Word.Paragraph[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    Set ParaAbove = Para[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    sStyle = Para.Range.ParagraphStyle[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    sStyle = Left(sStyle, 4)[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    If sStyle = "Head" Then[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        GoTo Skip[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    End If[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    Do While ParaAbove.OutlineLevel = Para.OutlineLevel[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]        Set ParaAbove = ParaAbove.Previous[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    Loop[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]Skip:[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    strTitle = ParaAbove.Range.Text[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    strTitle = Left(strTitle, Len(strTitle) - 1)[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]    ParentLevel = ParaAbove.Range.ListFormat.ListString & " " & strTitle[/FONT][/COLOR]
[COLOR=#333333][FONT=Segoe UI]End Function[/FONT][/COLOR]

xin xem file đính kèm

Thanks
Khoi
 

File đính kèm

Xin các bạn xem giúp mình code sau mình muốn xuất comments từ word sang excel như sau:
Khoi
Trong của sổ VBA vào Tools chọn Reference check vào Microsoft Excel 14.0 Object Library nếu sửa dụng 2007,2010 còn 2003 thì chắc 12.0 hay gì đó trong code bạn người ta có ghi đó
 
Upvote 0
Trong của sổ VBA vào Tools chọn Reference check vào Microsoft Excel 14.0 Object Library nếu sửa dụng 2007,2010 còn 2003 thì chắc 12.0 hay gì đó trong code bạn người ta có ghi đó

Cám ơn bạn nmhung49,

Mình đã hiểu và đã chạy được code này, tuy nhiên kq lại không như yêu cầu. Mình muốn bạn phát triển nốt code tại http://www.giaiphapexcel.com/forum/...nts-trong-Word-sang-Excel&p=563699#post563699
sẽ là giải pháp thay thế cho đoạn code trên.

Thanks
Khoi
 
Upvote 0

Bài viết mới nhất

Back
Top Bottom