【问题标题】:VBA Inserting Comments from Excel to WordVBA将注释从Excel插入Word
【发布时间】:2020-02-15 15:23:34
【问题描述】:

我是 VBA 新手,尝试将 Excel 中的数据中的 cmets 插入 Word 文档时遇到了困难。我正在尝试在 Word 中编写 VBA,并希望它从单独的电子表格中提取数据

Sub ConvertCelltoWordComment()

Dim Rng As Range
Dim wApp As Object
Dim strValue As String
Dim xlapp As Object
Dim xlsheet As Object
Dim xlbook As Object

'Opens Excel'

    Set xlapp = GetObject("C:\Users\eugenechang\Desktop\...xlsx")

If Err Then
     Set xlapp = CreateObject("Excel.Application")
End If

On Error GoTo 0

Dim i As Integer

For i = 1 To 5
    With xlsheet
        strValue = ActiveSheet.Cells(i, 1).Offset(1, 0)
    End With
    'Insert comment into document'

    ActiveDocument.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="15"
    ActiveDocument.Selection.GoTo What:=wdGoToLine, Which:=wdGoToRelative, Count:=5
    ActiveDocument.Comments.Add Range:=Selection.Range, Text:=strValue
Next i

End Sub

我正试图让它工作,但它给了我一个错误“对象未定义”。我尝试在“With xlsheet”下方的 strValue 行中设置一个对象,但我碰壁了。有什么帮助吗??

【问题讨论】:

  • 你必须记住Set所有对象变量指向它们所指的事物。你不能只使用With xl sheet 而不先将其设置为正确的工作表。同上xlbook,虽然这里的代码没有使用它。
  • 您得到的答案是否有助于解决问题?如果是,请单击最能回答问题的选项左侧的复选标记。如果没有,请回复有关这些答案如何没有帮助的更多信息。

标签: excel vba ms-word


【解决方案1】:

您没有为xlsheet 分配任何东西 - 所以这(默认情况下)等同于Nothing

尝试将xlSheet 设置为有意义的值。以下仅为示例:

For i = 1 To 5
    Set xlsheet = xlbook.Worksheets(i) ' <--- example here
    With xlsheet
        strValue = .Cells(i, 1).Offset(1, 0) '<-- don't use ActiveSheet
    End With
    'Insert comment into document'

    ActiveDocument.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="15"
    ActiveDocument.Selection.GoTo What:=wdGoToLine, Which:=wdGoToRelative, Count:=5
    ActiveDocument.Comments.Add Range:=Selection.Range, Text:=strValue
Next I

这里需要注意的是,您还没有设置 xlbook - 您必须还为 xlbook 分配有意义的内容。

【讨论】:

    【解决方案2】:

    将几个 DocVariables 添加到您的 Word 文件并从 Excel 运行下面的脚本。

    Sub PushToWord()
    
    Dim objWord As New Word.Application
    Dim doc As Word.Document
    Dim bkmk As Word.Bookmark
    sWdFileName = Application.GetOpenFilename(, , , , False)
    Set doc = objWord.Documents.Open(sWdFileName)
    'On Error Resume Next
    
    objWord.ActiveDocument.variables("FirstName").Value = Range("FirstName").Value
    objWord.ActiveDocument.variables("LastName").Value = Range("LastName").Value
    ' etc., etc., etc.
    
    objWord.ActiveDocument.Fields.Update
    
    'On Error Resume Next
    objWord.Visible = True
    
    End Sub
    

    【讨论】:

    • 当我尝试运行它时,我收到一条错误消息,提示“未定义用户定义的类型”。它突出显示了 objWord 对象。我错过了什么吗?
    • 在 VBA 编辑器窗口中...工具 > 参考 > Microsoft Word xx.x 对象库。选中该框并重新运行代码。您将 DocVariables 添加到 Word 并适当地命名它们,对。
    • 对不起,如果我看起来很无知,但是如何在脚本中添加 DocVariables?什么是正确的语法?
    • 这看起来也不错:youtube.com/watch?v=bqYoOvywjWs
    【解决方案3】:

    这最终从 Excel 文件中写入 cmets。显然,出于隐私原因,名称已更改。如果我能更好地简化这个,请告诉我。

    Sub ConvertExceltoWordComment()
    
    Dim wApp As Word.Application
    Dim xlApp As Excel.Application
    Dim PgNum As Integer
    Dim LineNum As Integer
    Dim objSelection As Word.Document
    Dim strpgSearch As Long
    Dim strlinSearch As Long
    Dim myRange As Range
    Dim XlLog As Excel.Worksheet
    Dim RowCount As Long
    
    'Opens Copied Word document'
    
        Set xlApp = CreateObject("Excel.Application")
        xlApp.Visible = True
    If Err Then
        Set xlApp = CreateObject("Excel.Application")
    End If
    
    On Error GoTo 0
    
    Dim SaveDoc As Excel.Workbook
    Set SaveDoc = xlApp.Workbooks.Open("FilePath.xlsm") 'Type filepath of document here'
    
    Set XlLog = SaveDoc.Sheets("Worksheet_Name") 'Type Sheetname here'
    RowCount = XlLog.Range("A1048576").End(xlUp).Row
    If RowCount > 0 Then
    
        Dim iTotalRows As Long
        iTotalRows = XlLog.Rows.Count      'Get total rows in the table'
    
        Dim txt As Variant
        Dim iRows As Long
    End If
    Dim i As Integer
    
    'Insert comment into Word document'
        Set wApp = CreateObject("Word.Application")
        wApp.Visible = True
    If Err Then
        Set wApp = CreateObject("Word.Application")
    End If
    Set objSelection = ActiveDocument
    
    For iRows = 3 To iTotalRows
            txt = XlLog.Cells(iRows, 8).Text 'Grabs appropriate comment text'
            objSelection.Activate
            objSelection.SelectAllEditableRanges
            strpgSearch = XlLog.Cells(iRows, 2) 'Grabs appropriate Page number'
            strlinSearch = XlLog.Cells(iRows, 3) 'Grabs appropriate Line number'
            objSelection.ActiveWindow.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, 
    Name:=strpgSearch
            objSelection.ActiveWindow.Selection.GoTo What:=wdGoToLine, Which:=wdGoToRelative, 
    Count:=strlinSearch
            Set myRange = ActiveWindow.Selection.Range
            ActiveDocument.Comments.Add Range:=myRange, Text:=txt
    Next iRows
    
    Set xlApp = Nothing
    Set SaveDoc = Nothing
    Set XlLog = Nothing
    Set objSelection = Nothing
    Set myRange = Nothing
    Set wApp = Nothing
    SaveDoc.Close
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2017-06-12
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      相关资源
      最近更新 更多