Open Word doc in Excel and save as text

Viper147

New Member
Joined
Apr 19, 2018
Messages
34
Hi all,

As a noob with VBA I am trying to expand on my newly found skills, and recently came across an exercise where I had to save a Word document as text. The text file then has to be opened, the contents copied and pasted in an Excel sheet. Whilst I got the last part of the code right (in a separate module), I have been struggling with the first part of opening the Word document and saving it as text. I copied code from a previous post related to the same subject matter and amended it to my needs, but am still stuck. I get a "Type mismatch" error on the red line of code below. Any assistance with this would be greatly appreciated.

Code:
Sub CreateText()
Dim psWorkbookCurrentWorkingDirectory As String
Dim psNextFullChecklistFileName As String
Dim psNextChecklistFileName As String
Dim wdApplication As Object
[COLOR=#ff0000]Set wdDocument = New Workbook.Document[/COLOR]
 
    On Error Resume Next
    Set wdApplication = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
                     Set wdApplication = CreateObject("Word.Application")
    End If
    On Error GoTo 0
 
    Set wdDocument = wdApplication.Documents.Open(Range("Web_folder") & Range("Query_file") & ".docx")
 
    wdApplication.Visible = True
 
    wdDocument.SaveAs Filename:=Range("Quote_folder") & Range("Quote_file") & ".txt", _
                      FileFormat:=wdFormatText, LockComments:=False, Password:="", _
                      AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
                      EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
                      SaveAsAOCELetter:=False, Encoding:=1252, InsertLineBreaks:=False, _
                      AllowSubstitutions:=False, LineEnding:=wdCRLF
 
    wdDocument.Close
 
    wdApplication.Quit
 
    Set wdApplication = Nothing
    Set wdDocument = Nothing
 
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi Paul,

Checked again and only noted now that the MS Office and MS Excel ones were ticked and not MS Word as well. My apologies for the oversight on my part and wasting your time. Works perfectly now.
Thanks for all your help and patience with this.

Cheers
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,048
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top