2017-06-06 70 views
0

我正在尋找一種方法將PowerPoint幻燈片的第一個元素的文本複製到Excel文件中。我得到了下面的代碼打印出第一個框的文本:使用VBA從Excel文件中的powerpoint打印內容

Sub getText 

Dim sld As Slide 
Set sld = Application.ActiveWindow.View.Slide 
For Each sld In ActivePresentation.Slides 
With sld.Shapes(1) 

    myInput = .TextFrame.TextRange.Text 
    MsgBox (myInput) 

End With 
Next 
End sub 

現在,下一步我要帶是將數據添加到Excel文件。因此,我儘量做到以下幾點:

Sub getText() 

Dim xlApp As Object 
Dim xlWorkBook As Object 
Set xlApp = CreateObject("Excel.Application") 
xlApp.Visible = True 
Set xlWorkBook = xlApp.Workbooks.Open("~\PROJECTEN\Lopend\office_VA\macroStore.xlsx", True, False) 
xlWorkBook.sheets(1).Range("A2").Select 


Dim sld As Slide 
Set sld = Application.ActiveWindow.View.Slide 

For Each sld In ActivePresentation.Slides 
With sld.Shapes(1) 

myInput = .TextFrame.TextRange.Text 
ActiveCell.Text = myInput 

End With 
Next 

End Sub 

然而,當我現在嘗試它,它得到的錯誤:「所需的對象」。有關如何更改我的代碼的任何想法?

回答

0

您的問題是,你引用ActiveCell.Text但VBA沒有線索那是什麼。此外,您還沒有聲明您的myInput變量。

試試這個宏,而不是選擇單元格,我只是給它分配文本值。另外,如果您要編寫多個值,您的代碼將繼續寫入同一個單元格。在下面的代碼我已經添加了一對夫婦會寫你的文字落筆列A.

Sub getText() 

    Dim xlApp As Object 
    Dim xlWorkBook As Object 
    Set xlApp = CreateObject("Excel.Application") 
    xlApp.Visible = True 
    Set xlWorkBook = xlApp.Workbooks.Open("~\PROJECTEN\Lopend\office_VA\macroStore.xlsx", True, False) 

    Dim xlWorkSheet As Object ' Create a worksheet object 
    xlWorkSheet = xlWorkBook.sheets(1) ' Set the sheet you activate to that object 

    Dim iRow As Long ' Create a variable to store row number 
    iRow = 1 'Set the first row that you want to start writing data on 

    Dim sld As Slide 
    Dim myInput As String 
    Set sld = Application.ActiveWindow.View.Slide 

    For Each sld In ActivePresentation.Slides 
     With sld.Shapes(1) 
      myInput = .TextFrame.TextRange.Text 
      xlWorkSheet.Cells(iRow, "A") = myInput 'Using .Cells() you can specify the (row, column) location 
      iRow = iRow + 1 'increment by one for next line of text 
     End With 
    Next 
End Sub 
+0

@Philip,非常感謝您的回覆徹底線。但是,當我嘗試它:我得到故障91錯誤這一行:xlWorkSheet = xlWorkBook.sheets(1)。它說,對象變量或變量與配置不好....任何thougts? –

+0

而不是使用xlWorkBook.sheets(1)嘗試xlWorkBook.sheets(「Sheet1」)或任何工作表名稱在工作簿中。 – pheeper

+0

嘗試了它,現在我得到一個438錯誤,指出此對象不支持此方法/配置 –