VBA WORD: Select Active Document
Solved
ph412
Posted messages
49
Status
Membre
-
ph412 Posted messages 49 Status Membre -
ph412 Posted messages 49 Status Membre -
Hello everyone,
I am decent at VBA Excel but I am a complete beginner in Word.
My problem:
I want to write variable data to a Word file from Excel.
So far, I am managing with the code below.
(my variable is declared as public in Excel and is written perfectly)
Sub Creation_doc_word()
Dim WQ As Object
Set WQ = New Word.Application
WQ.Visible = True
' Creating a new document:
WQ.Documents.Add
WQ.Selection.TypeText Text:=variable
Set WQ = Nothing
End Sub
Now that the Word document is still open, I want to be able to write to this document again (via another macro) but I can't seem to select it again. Can you please help me?
If I understand correctly, Word.Application allows me to indicate to VBA that I am working on Word, but it does not allow me to select my document.
Thank you
I am decent at VBA Excel but I am a complete beginner in Word.
My problem:
I want to write variable data to a Word file from Excel.
So far, I am managing with the code below.
(my variable is declared as public in Excel and is written perfectly)
Sub Creation_doc_word()
Dim WQ As Object
Set WQ = New Word.Application
WQ.Visible = True
' Creating a new document:
WQ.Documents.Add
WQ.Selection.TypeText Text:=variable
Set WQ = Nothing
End Sub
Now that the Word document is still open, I want to be able to write to this document again (via another macro) but I can't seem to select it again. Can you please help me?
If I understand correctly, Word.Application allows me to indicate to VBA that I am working on Word, but it does not allow me to select my document.
Thank you
1 réponse
Hello,
like this:
--
@+ The Woodpecker
like this:
'go to Tools-references and check Microsoft Word 12.0 Object Library Sub Creation_doc_word() Dim MyBeautifulWord As Object Set MyBeautifulWord = New Word.Application MyBeautifulWord.Visible = True ' Creating a new document: MyBeautifulWord.Documents.Add ' Writing a small text in this new document: MyBeautifulWord.Selection.TypeText "Functionality test" ' Saving this document just created: 'MyBeautifulWord.ActiveDocument.SaveAs "C:\My documents\Simple test.doc" ' Closing this document: ' MyBeautifulWord.ActiveDocument.Close ' Set MyBeautifulWord = Nothing End Sub Sub add_text() Dim appword As Object, thedocuments As Object, thedoc As Object Dim i As Byte On Error GoTo errortrap 'Try to get the instance of Word 'active in case Word is open 'If Word is not open, this generates 'error number 429 'In this case, the error handler "errortrap" 'takes over and opens Word Set appword = GetObject(, "Word.Application") appword.Visible = True Set thedocuments = appword.Documents For Each thedoc In thedocuments thedoc.Select appword.Selection.EndKey Unit:=wdStory appword.Selection.InsertParagraph appword.Selection.InsertAfter "It's done" thedoc.Save Next 'Close the documents For Each thedoc In thedocuments thedoc.Close Next 'Close Word appword.Quit 'Routine exit, to avoid the error handler Exit Sub 'Error handler errortrap: Select Case Err.Number Case 429 MsgBox "The file already exists. Program ends and returns to Word" Case Else MsgBox "Unexpected error. Program ends and returns to Word" End End Select End Sub
--
@+ The Woodpecker
ph412
Posted messages
49
Status
Membre
Thank you very much, it works very well! (After that, I don't close the Word document so I can continuously switch from Excel to Word) Great!