0

I saw a lot of documentation on Excel VBA but not nearly enough on word VBA.

I want to take a word file, open it, copy the whole story and paste it in an excel and then remove all the pictures.

Here is my VBA code:

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

I've Already Tried Several methods with pywin32 but so far i have been able to open the 2 files (word and excel), save them but i have not been able to copy and paste these files from word to excel

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43

0 Answers0