vba查找文本并导出

时间:2017-01-26 18:59:57

标签: vba powerpoint

Sub FindWordCopySentence() 
  Dim appExcel As Object 
  Dim objSheet As Object 
  Dim aRange As Range 
  Dim intRowCount As Integer 
  intRowCount = 1 
  Set aRange = ActiveDocument.Range 
  With aRange.Find 
    Do 
      .Text = "shall" ' the word I am looking for
      .Execute 
      If .Found Then 
        aRange.Expand Unit:=wdSentence 
        aRange.Copy 
        aRange.Collapse wdCollapseEnd 
        If objSheet Is Nothing Then 
          Set appExcel = CreateObject("Excel.Application") 
          'Change the file path to match the location of your test.xls
          Set objSheet = appExcel.workbooks.Open("C:\temp\test.xls").Sheets("Sheet1") 
          intRowCount = 1 
        End If 
        objSheet.Cells(intRowCount, 1).Select 
        objSheet.Paste 
        intRowCount = intRowCount + 1 
      End If 
    Loop While .Found 
  End With 
  If Not objSheet Is Nothing Then 
    appExcel.workbooks(1).Close True 
    appExcel.Quit 
    Set objSheet = Nothing 
    Set appExcel = Nothing 
  End If 
  Set aRange = Nothing 
End Sub 

我无法使用VBA在PPT上运行此代码!当我尝试调试时抛出一个错误,说Arange行是正确的。

1 个答案:

答案 0 :(得分:0)

生成错误是因为powerpoint中不存在“range”变量类型。如果你试图调用excel范围,你必须这样做:

  1. 转到tools =>引用,然后选择Microsoft excel X.XX对象库。 (x.xx响应您已安装的excelversión)

  2. 写道:

    Dim aRange As Excel.Range

  3. 而不是:

    Dim aRange As Range
    

    当您需要与VBA中的其他程序进行交互时,请记住首先将其分配给引用。

    我希望这对你有帮助。

    Humberto