VBA "This command is not available because document is not open"

Being new to the VBA language, I've spent hours researching this simple task and nothing works for me. I get the error "This command is not available because no document is open" and there is a document with text that starts with writing. What's going on here? Thanks for your help.

Sub excel_to_word()
Dim wapp As Word.Application
Dim wdoc As Word.Document

Set wapp = CreateObject("word.application")
wapp.Visible = True


Set wdoc = wapp.Documents.Add

wdoc.Content.InsertAfter Range("B5")

Application.Wait (Now + TimeValue("0:00:01"))


'CreateObject("Word.Application").ChangeFileOpenDirectory "C:\USERS\JOSEPH\DESKTOP\"
 Dim NEWPATH As String
 NEWPATH = "C:\USERS\JOSEPH\DESKTOP"


   CreateObject("Word.Application").ActiveDocument.SaveAs2 Filename:="joseph.docx",       FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", addtorecentfiles:=False, savenativepictureformat:=False, SaveFormsData:=False, saveasaoceletter:=False, CompatibilityMode:=14



End Sub

      

+3


source to share


1 answer


CreateObject("Word.Application").ActiveDocument.SaveAs2 Filename:="joseph.docx" 'etc

      

it should be



wdoc.SaveAs2 Filename:="joseph.docx" 'etc

      

When you call CreateObject

the second time, when you create a completely new instance of Word (and that second instance has no document open in it) ..

+2


source







All Articles