简体   繁体   English

将表从 word 导出到 excel 时出现错误 432

[英]Error 432 while exporting tables from word to excel

I'm trying to export a word doc.我正在尝试导出 word 文档。 tables to an excel file.表到 excel 文件。 Expected result:预期结果:

  1. export only the first row for every column (avoiding the rest of the table content)每列只导出第一行(避免表格内容的rest)
  2. no space between tables表格之间没有空间

I tried with this but I get this 432 error on "Set wdDoc = GetObject(wdFileName)":我试过这个,但我在“Set wdDoc = GetObject(wdFileName)”上得到这个 432 错误:

432 error 432 错误

Can someone help me?有人能帮我吗?

Sub Trouble()

    Dim word_app As Object, word_doc As Object, word_table As Word.Table, wdFileName As Variant, wdDoc As Object

    Set word_app = CreateObject("Word.Application")
    ActiveSheet.Calculate

    wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
    "Browse for file containing table to be imported")

    If wdFileName = False Then Exit Sub 
    ActiveSheet.Range("A:A").ClearContents

    Set wdDoc = GetObject(wdFileName)
    word_app.Visible = True
    wdDoc.Activate

    excel_row = 1
 
    On Error Resume Next

    For Each word_table In wdDoc.Tables
        Err.Clear
        For i = 1 To word_table.Rows.Count
            
            For j = 1 To word_table.Columns.Count
                part = word_table.Rows(i).Cells(j).Range.Text
                part = Left(part, Len(part) - 1)
                part = Replace(part, vbNewLine, "")
                
                Sheet2.Cells(excel_row, j).Value = part
            Next j
            excel_row = excel_row + 1
        Next i
    Next word_table

    MsgBox "done"

    wdDoc.Save
    'word_app.Quit
    Set wdDoc = Nothing
    Set word_app = Nothing

End Sub

This works for me (just the word-related parts):这对我有用(只是与单词相关的部分):

Sub Trouble()

    Dim word_app As Object, wdFileName As Variant, wdDoc As Object

    Set word_app = CreateObject("Word.Application")
    

    wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
                             "Browse for file containing table to be imported")

    If wdFileName = False Then Exit Sub
    
    Set wdDoc = word_app.documents.Open(wdFileName)
    word_app.Visible = True
    wdDoc.Activate
    
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM