关闭Word用户窗体

时间:2015-10-23 11:41:31

标签: excel vba userform

我有从某个目录循环的VBA,打开Word文件,复制一些文本,将复制的文本放入Excel,然后关闭Word。代码可以工作,但我有一些内部有一些UserForms的文件。当该类型的文档启动弹出时,UserForm会打开。在我关闭该表单之前,代码正在等待关闭。所以我正在尝试(没有成功)关闭弹出的UserForm。我的代码:

Sub copy_from_word_to_excel(ByVal aFilename As String)

   Dim objWord As Object
   Dim objDoc As Object
   Dim i As Integer
   Dim objLoop As Object


   Set objWord = CreateObject("Word.Application")
   Set objDoc = objWord.Documents.Open(aFilename, ReadOnly:=False)

    objWord.Visible = False




    For Each objLoop In objDoc.VBA.UserForms
        If TypeOf objLoop Is UserForm Then Unload objLoop
    Next objLoop

    objDoc.ReadOnlyRecommended = False
    objDoc.RemovePersonalInformation = True

    j = j + 1
    ThisWorkbook.ActiveSheet.Cells(j, 1).Value = aFilename
    ThisWorkbook.ActiveSheet.Hyperlinks.Add Anchor:=Cells(j, 1), Address:=aFilename, TextToDisplay:=aFilename
    ThisWorkbook.ActiveSheet.Cells(j, 2).Value = objDoc.Paragraphs(1).Range.Text
    ThisWorkbook.ActiveSheet.Cells(j, 3).Value = objDoc.Paragraphs(2).Range.Text
    ThisWorkbook.ActiveSheet.Cells(j, 4).Value = objDoc.Paragraphs(32).Range.Text
    ThisWorkbook.ActiveSheet.Cells(j, 5).Value = objDoc.Paragraphs(33).Range.Text
    ThisWorkbook.ActiveSheet.Cells(j, 6).Value = objDoc.Paragraphs(34).Range.Text


      objDoc.Close SaveChanges:=False
      Set objDoc = Nothing

      objWord.Quit
      Set objWord = Nothing

End Sub

我试图用这段代码从Excel关闭Word用户窗体(但它并不好):

For Each objLoop In objDoc.VBA.UserForms
    If TypeOf objLoop Is UserForm Then Unload objLoop
Next objLoop

可以做些什么?

0 个答案:

没有答案