'Open a Word Document and Set it to the newly created object above 'Create a New Object for Microsoft Word Application following conditions are true: Word previously quit improperly and, therefore, did not delete the owner file.-or-A second instance of Word is running in the background with the document already open. Sub Copy_From_Word()Īpplication.DisplayAlerts = False 'Disable all the Alerts from excelĪpplication.ScreenUpdating = False 'After opening Word Doc, Document will not be visible This behaviour may occur if Word determines that the owner file for. Just change the path to the Word file that you are referencing, and change the ranges, if need be.
#Force quit microsoft word microsoft error reporting code#
Is that right? The code below will do this for you. It sounds like you want to copy data from a Word doc into Excel. ObjWord.Documents("Template1").Bookmarks("").Range.FormattedText In order to transfer formatted content from one place ( Range) in any document to another place ( Range) in the same or another document, using bookmarks can be done in one step, without any selecting or activating: objDoc.Bookmarks("").Range.FormattedText = _ In any case, it's possible to address the document's windows, specifically, rather than having VBA search for it: objDoc.Windows(1).Activate I might wait until the end of the code, but that would be personal preference. If it's in order to show it to the user, for editing, then by all means. It's not necessary for manipulating a document's content via VBA. It's unsure whether you'd really need to activate this window. So declare and instantiate a variable for the Word document and use that instead of ActiveDocument: Dim objDoc as Object 'Word.Document Better to work with the application's objects, directly. In this case, as the file is in Word and not Excel, using objWord.Windows(Filename).Activate should avoid this particular error.Īs with Excel, using Active, Activate or Selection in Word VBA is unreliable and shouldn't be used unless absolutely necessary. The reason for the error is that this code is running in Excel, so if objWord is not used for Word syntax, and both Word and Excel use the same name for an object, VBA will try to use the Excel object model. There are a number of factors in play with this code that will or might cause problems. The requested member of the collection does not exist. Selection.MoveLeft Unit:=wdCharacter, Count:=2 Selection.PasteAndFormat (wdFormatOriginalFormatting) Selection.GoTo What:=wdGoToBookmark, Name:="" IntChoice = Application.FileDialog(msoFileDialogOpen).ShowĪnd here it breaks :( Windows(FileName).Activate
Set objWord = CreateObject("Word.Application")Īpplication.FileDialog(msoFileDialogOpen).AllowMultiSelect = False "FileWithData" could be any file and that is the reason I am using OpenDialog.Įverything is working but I am stuck when it comes to Activating "FileWithData" window to start "Copying and Pasting" process. I am trying to write the VBA code to open a Word Document file ("FileWithData") and copy the information from "FileWithData" into "Template1". I have one Word Document ("Template1") opened already.