将VBA代码转换为python代码以更容易集成

时间:2019-06-04 15:03:36

标签: python vba

我在Excel VBA上看到了很多文档,但在单词VBA上却看得不够。

我要提取一个Word文件,将其打开,复制整个故事并将其粘贴到Excel中,然后删除所有图片。

这是我的VBA代码:

Sub Exportwordtoexcel(ByVal strPath As String)
    Dim objDocument As Document
    Dim wordDoc As Object
    Dim oXL As Excel.Application
    Dim DocTarget As Word.Document
    Dim Target As Excel.Workbook
    Dim tSheet As Excel.Worksheet

    Set objDocument = Documents.Open(strPath)
    objDocument.Activate
    objDocument.AutoSaveOn = False
    Selection.WholeStory
    Selection.Copy

    Set DocTarget = ActiveDocument
    small_word = Split(DocTarget.Name, ".")
    path_to_word = "C:SomePath" & small_word(0)
    Debug.Print small_word(0)


    'If Excel is running, get a handle on it; otherwise start a new instance of Excel
    On Error Resume Next
    Set oXL = GetObject(, "Excel.Application")

    If Err Then
       ExcelWasNotRunning = True
       Set oXL = New Excel.Application
    End If

    oXL.Visible = True

    Set Target = oXL.Workbooks.Add
    Set tSheet = Target.Sheets(1)
    tSheet.Paste

    For Each Shape In tSheet.Shapes
            Shape.Delete
    Next

    'Target.SaveAs FileName:=small_word(0), FileFormat:=xlCSV, CreateBackup:=False
    Target.SaveAs FileName:=path_to_word, CreateBackup:=False
    Target.Close SaveChanges:=False

    objDocument.Close SaveChanges:=True

End Sub

我已经尝试了pywin32的几种方法,但是到目前为止,我已经能够打开2个文件(word和excel),保存它们,但是我无法将这些文件从word复制并粘贴到excel中

0 个答案:

没有答案