I have an error with this code. It says doc is not defined. Sub SendRequest() Dim…
- User
I have an error with this code. It says doc is not defined. Sub SendRequest()
Dim wordApp As Object Set doc = wordApp.Documents.Open("G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\AdminRequest.docm") wordApp.Visible = True doc.Activate wordApp.WindowState = wdWindowStateNormal
End Sub
- Assistant
You need to define the wordApp object before using it. Here's a corrected version of the code:
Sub SendRequest() Dim wordApp As Object Dim doc As Object
Set wordApp = CreateObject("Word.Application") Set doc = wordApp.Documents.Open("G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\AdminRequest.docm") wordApp.Visible = True doc.Activate wordApp.WindowState = wdWindowStateNormalEnd Sub
This creates an instance of Word and assigns it to the wordApp object. The doc object is also defined within the sub.
- User
error now is that wdWindowStateNormal is not defined
- Assistant
The error you're getting means that the constant
wdWindowStateNormalis not recognized. This constant is defined in the Word Object Library, so you need to make a reference to that library in your Excel VBA project.Here's how to make a reference to the Word Object Library:
- In the VBA Editor, go to the "Tools" menu and select "References".
- In the "References" dialog box, scroll down until you find "Microsoft Word xx.0 Object Library" (where xx is the version of Word installed on your computer).
- Check the checkbox next to "Microsoft Word xx.0 Object Library" and click "OK".
Once you've made the reference to the Word Object Library,
wdWindowStateNormalshould be recognized by Excel VBA and your code should work fine. - User
can you write a simpler code so that it is compatible with other pc's and other versions of word?
- Assistant
Certainly! Here's a simplified version of the code that should work on most PCs and versions of Word:
Sub SendRequest() Dim wordApp As Object Dim doc As Object Set wordApp = CreateObject("Word.Application") Set doc = wordApp.Documents.Open("G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\AdminRequest.docm") wordApp.Visible = True doc.Activate End SubThis code uses late binding, which means it doesn't require a reference to the Word Object Library and is compatible with different versions of Word. The
WindowStateproperty is omitted, but you can still adjust the window state of the Word application manually. - User
No errors, but it does not have primary focus
- Assistant
To bring the Word document to the front and give it focus, you can use the
AppActivatemethod of theWshShellobject. Here's an updated version of the code that should work:Sub SendRequest() Dim wordApp As Object Dim doc As Object Dim WshShell As Object Set wordApp = CreateObject("Word.Application") Set doc = wordApp.Documents.Open("G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\AdminRequest.docm") wordApp.Visible = True doc.Activate Set WshShell = CreateObject("WScript.Shell") WshShell.AppActivate wordApp.Caption End SubThis code creates a
WshShellobject and uses itsAppActivatemethod to bring the Word application to the front. TheCaptionproperty of thewordAppobject returns the title of the Word window, which should be used as the argument forAppActivate.