Saving excel workbook not working

Using the following VBA code in microsoft 2010 to create an excel workbook from a word doc:

Sub ParseDoc()

    Dim doc As Document
    Set doc = ActiveDocument
    Dim paras As Paragraphs
    Set paras = doc.Paragraphs
    Dim para As Paragraph
    Dim sents As Sentences
    Dim sent As Range
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Dim counter As Integer
    counter = 1
   
    Set xlWB = xlApp.Workbooks.Add ' create a new workbook
   
    With xlWB
        .Title = "TestParse"
        .Subject = "TestParse"
        .Comments = "TestParse"
        .Author = "TestParse"
        .SaveAs FileName:="testParse.xlsx"
    End With

   
    ' or
    'Set xlWB = xlApp.Workbooks.Open("C:\Foldername\Filename.xls")
    ' open an existing workbook
    ' example excel operations

    For Each para In paras
        Set sents = para.Range.Sentences
        For Each sent In sents
           
            xlWB.Sheets("Sheet1").Cells(counter, 1).Value = sent.Text
            counter = counter + 1
            Debug.Print sent.Text
        Next
    Next

    'xlWB.SaveAs ("C:\testParse.xlsx")
   
   
    With xlWB
       
        If Dir("C:\testParse.xlsx") <> "" Then
            Kill "C:\testParse.xlsx"
        End If
        .SaveAs ("C:\testParse.xlsx")
        .Close ' close the document
    End With

   
   
    xlApp.Quit ' close the Excel application
    Set xlWB = Nothing
    Set xlApp = Nothing
   
End Sub


WHen I run, the workbook is created fine.  However, i get the following error and can't figure out why

error
LVL 6
HyperBPPAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FaustulusCommented:
The following code works:-
Option Explicit

Sub ParseDoc()

    Dim xlApp As Object
    Dim xlWb As Excel.Workbook
    Dim doc As Document
    Dim paras As Paragraphs
    Dim para As Paragraph
    Dim sents As Sentences
    Dim sent As Range
    Dim counter As Integer
    
    Set doc = ActiveDocument
    Set paras = doc.Paragraphs
    Set xlApp = CreateObject("Excel.Application")
    Set xlWb = xlApp.Workbooks.Add ' create a new workbook
    xlApp.Visible = True
    counter = 1
    
    With xlWb
'        .Title = "TestParse"
'        .Subject = "TestParse"
'        .Comments = "TestParse"
'        .Author = "TestParse"
        .SaveAs FileName:="C:\My Documents\TestParse.xlsx"
    End With

    
    ' or
    'Set xlWB = xlApp.Workbooks.Open("C:\Foldername\Filename.xls")
    ' open an existing workbook
    ' example excel operations

    For Each para In paras
        Set sents = para.Range.Sentences
        For Each sent In sents
            
            xlWb.Sheets("Sheet1").Cells(counter, 1).Value = sent.Text
            counter = counter + 1
            Debug.Print sent.Text
        Next
    Next

    'xlWB.SaveAs ("C:\testParse.xlsx")
    
    
'    With xlWB
'
'        If Dir("C:\testParse.xlsx") <> "" Then
'            Kill "C:\testParse.xlsx"
'        End If
'        .SaveAs ("C:\testParse.xlsx")
'        .Close ' close the document
'    End With

    
   
    xlApp.Quit ' close the Excel application
    Set xlWb = Nothing
    Set xlApp = Nothing
    
End Sub

Open in new window

Note that Title, Subject etc. aren't properties of the Workbook object. Rather, they are members of the workbook's BuiltInDocumentProperties collection. I think you should also be careful with your Kill command. That part of the code you apostrophed out yourself, but if your intention is to delete any existing workbook before creating a new one that action must be taken before you create a new one. not after. Let me know if you require help with that part of the code. Right now, the above code will create a new workbook but will prompt you for instruction if a workbook by that name already exists.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.