Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA "This Command is not Available because no document is open"

Tags:

save

vba

Being new to the VBA language, I have researched this simple task for hours and I am having nothing work. I am getting the error back "This Command is not Available because no document is open", and there is a word document with writing coming right up. What is 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
like image 273
QProgram Avatar asked Nov 16 '25 04:11

QProgram


1 Answers

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

should be

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

When you call CreateObject the second time you're creating a whole new instance of Word (and that second instance doesn't have any document open in it). .

like image 112
Tim Williams Avatar answered Nov 17 '25 17:11

Tim Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!