0
votes

I'd like to change the view in Word from my Excel macro (which creates the Word document).

I'd like to execute: ActiveWindow.View.Type = wdWebView

In my Excel macro, I have:

Dim objWord
Dim objDoc    
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
2
You're late-binding Word here; wdWebView has no meaning unless you've referenced the Word type library... in which case late-binding serves little purpose other than making your life much harder than it needs to be.Mathieu Guindon
ActiveWindow is in the scope of the current host application, i.e. Excel.Application.ActiveWindow - if you want the Word window, you need to work off the objWord object.Mathieu Guindon

2 Answers

1
votes

As mentioned in the comments, you're mixing late-binding and early-binding, and also need to reference the Word instance.

An early-binding approach might be (add a reference to the Microsoft Word xx.0 Object Library under Tools > References).

Sub MyWord()
    Dim wordApp As New Word.Application
    Dim myDoc As Word.Document
    
    wordApp.Visible = True
    Set myDoc = wordApp.Documents.Add
    wordApp.ActiveWindow.View.Type = wdWebView
End Sub

If you want to late-bind, note from the WdViewType enum docs that wdWebView corresponds to the value 6.

Sub MyWord()
    Const wdWebView As Long = 6
    Dim objWord As Object
    Dim objDoc As Object
    
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    
    Set objDoc = objWord.Documents.Add
    objWord.ActiveWindow.View.Type = wdWebView
End Sub
0
votes

You don't have to use early binding to use wdWebView. Instead, you could use:

Dim objWord As Object, objDoc As Object
Const wdWebView As Long = 6
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.ActiveWindow.View.Type = wdWebView