VBA中的错误462:使用Excel填充MS Word

时间:2016-06-16 00:35:27

标签: excel-vba vba excel

我正在尝试使用电子表格中的输入填充单词内容控件。我的代码要么不起作用,要么一直工作一次。我得到错误462.

有人可以帮我弄清问题是什么吗?

谢谢!

Private Sub Accept_Click()

Dim directory As String
Dim wrdApp As Word.Application
Dim doc As Word.Document
Dim fd As Office.FileDialog
Dim dt As String

Set wrdApp = CreateObject("Word.Application")

directory = Application.ActiveWorkbook.Path

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .InitialFileName = directory
    .AllowMultiSelect = False
    .Title = "Select doc letter"
    .Filters.Add "All", "*.*"

    If .Show = True Then
        txtfilename = .SelectedItems(1)
    End If
End With

wrdApp.Visible = True
On Error GoTo Handler

'i get error on the next line: 
Set doc = wrdApp.Documents.Open(txtfilename, , False, , , , , , , , , True) 

Documents(txtfilename).Activate

For Each cc In ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ContentControls
    If cc.Tag = "uptitle" Then cc.Range.Text = mill_box.Text
Next

For Each cc In ActiveDocument.StoryRanges(wdMainTextStory).ContentControls
    If cc.Tag = "client" Then cc.Range.Text = TextBox1.Text & Chr(10) & TextBox2.Text _
 & ", " & TextBox3 & Chr(10) & TextBox4 & " , " & TextBox5 & Chr(10) & TextBox6
    If cc.Tag = "mill" Then cc.Range.Text = mill_box.Text


Next
ActiveDocument.Windows.Application.WindowState = wdWindowStateMaximize

Unload Me

ActiveDocument.Activate



Exit Sub
Handler:
Set wrdApp = Nothing
Set doc = Nothing
Unload Me
MsgBox "error"

End Sub

3 个答案:

答案 0 :(得分:1)

有趣的错误!

阅读this page(Google为我提供的#2错误“错误462” - LMGTFY :)中的信息指向Microsoft knowledge base article中讨论的答案:

  • 您可以通过代码创建Word应用程序对象,并将其分配给变量。
  • 当你引用任何Word的应用程序成员(如ActiveDocument)而没有合格的Application变量时,VBA会为它创建一个隐藏变量。
  • 一切似乎都很好,因为VBA可以通过你自己的变量和隐藏的变量调用Word。
  • 当你现在将自己的变量设置为Nothing时,隐藏的变量仍然会保持Word的存活。
  • 当你第二次来到时,隐含的隐藏变量会让事情变得混乱。

您的代码也会访问ActiveDocumentDocuments(txtfilename),而不是您自己的docwrdApp

我不知道这是否是您问题的解决方案(没时间检查),但似乎非常适用。

答案 1 :(得分:0)

当它没有错误时,你正在卸载我,然后退出子。当出现错误时,您不必将wrdApp和doc设置为Nothing。如果您修复了那个逻辑,那么在成功运行时它们会被设置为空,它会继续发生吗?

答案 2 :(得分:0)

经过几天的试验和错误,这里是最终完成任务的代码。感谢大家花时间帮我解决这个问题。

Private Sub Accept_Click()

Dim directory As String
Dim wrdApp As Word.Application
Dim doc As Word.Document
Dim fd As Office.FileDialog

On Error Resume Next

'this part is not necessary, but end users will know they can't have other word
'docs open when working with this file.  
Set wrdApp = GetObject(, "Word.Application")
Set doc = wrdApp.Documents("Welcome letter.docx")
If Error <> 0 Then
    Call Killword
End If

On Error GoTo Error_Handler

Set wrdApp = CreateObject("Word.Application")
directory = Application.ActiveWorkbook.Path
Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .InitialFileName = directory
    .AllowMultiSelect = False
    .Title = "Select welcome letter"
    .Filters.Add "All", "*.*"

    If .Show = True Then
        txtfilename = .SelectedItems(1)
    End If
End With

'here is the thing: i had to instantiate the doc before making wrdapp visible. 
Set doc = wrdApp.Documents.Open(txtfilename, , False, , , , , , , , , True)
  wrdApp.Visible = True
doc.Activate

'from now on I have no choice than to refer to doc (not documents(1) or activedocuments! neither work)
For Each cc In doc.StoryRanges(wdPrimaryHeaderStory).ContentControls
    If cc.Tag = "uptitle" Then cc.Range.Text = mill_box.Text
Next

For Each cc In doc.StoryRanges(wdMainTextStory).ContentControls
'here goes the code i need to execute on my word document. can be anything.

Next
doc.Windows.Application.WindowState = wdWindowStateMaximize

Unload Me


Exit Sub

Error_Handler:
    Select Case Err.Number
        Case 429, 91
            Err = 0
            Resume Next
        Case Else
            MsgBox ("An unexpected error has occured." & vbCrLf & vbCrLf & _
            "Error number: " & Err.Number & vbCrLf & "Error description: " & Err.Description)
            Resume Next
    End Select


'Set wrdApp = Nothing
'Set doc = Nothing
Unload Me


End Sub