在Excel中选择CSV的导出列

时间:2018-04-12 10:42:38

标签: excel vba excel-vba csv

我找到了以下代码来解析PPT文件的文本并将其转储到CSV文件中。

 Sub ExportTextToCSV()

  Dim oPres As Presentation
  Dim oSlides As Slides
  Dim oSld As Slide         'Slide Object
  Dim oShp As Shape         'Shape Object
  Dim iFile As Integer      'File handle for output
  Dim sTempString As String

  Dim PathSep As String
  Dim Quote As String
  Dim Comma As String
  iFile = FreeFile          'Get a free file number

  #If Mac Then
    PathSep = ":"
  #Else
    PathSep = "\"
  #End If

  Quote = Chr$(34)
  Comma = ","

  Set oPres = ActivePresentation
  Set oSlides = oPres.Slides

  'Open output file
  ' NOTE:  errors here if original PPT file hasn't been saved
  Open oPres.Path & PathSep & "ParsedText1.csv" For Output As iFile

  For Each oSld In oSlides    'Loop thru each slide
    For Each oShp In oSld.Shapes                'Loop thru each shape on slide

      'Check to see if shape has a text frame and text
      If oShp.HasTextFrame And oShp.TextFrame.HasText Then
          sTempString = sTempString & Quote & oShp.TextFrame.TextRange.Text & Quote & Comma
      End If

    Next oShp

    ' print the result to file:
    Print #iFile, sTempString
    sTempString = ""

  Next oSld

  'Close output file
  Close #iFile

End Sub

当我运行它时,它会将文本转储到CSV文件的A列。是否有任何修改可以让我选择将其转储到哪个列?例如,如果我想将它转储到D列,我该做什么修改? 任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

对于列D,在字符串前写下3个逗号。

因此来自:

sTempString = sTempString & Quote & oShp.TextFrame.TextRange.Text & Quote & Comma

写:

sTempString = sTempString & ",,," & Quote & oShp.TextFrame.TextRange.Text & Quote & Comma