My Blog

Opening Word Document in Print Layout

Recently my office upgraded from MS Office 2010 to MS Office 2013 and my VB.Net code started breaking on the line

    Dim oWordApp As New word.Application
    Dim oWordDoc As word.Document

    oWordDoc = oWordApp.Documents.Open(Filename:="C:\Sample.Docx", ReadOnly:=True)
    oWordDoc.Tables(1).Cell(1, 1).Range.Text = "Test"

The problem was in MS Office 2013, the view prevented me from writing anything to the document as shown below.

I experimented with MS Word Menu File | Options, but couldn’t get it to work and then I realized what was wrong. I had to change the layout of the word document. The following sorted my problems.

    Dim oWordApp As New word.Application
    Dim oWordDoc As word.Document

    oWordDoc = oWordApp.Documents.Open(Filename:="C:\Sample.Docx", ReadOnly:=True)
    oWordDoc.ActiveWindow.View.Type = word.WdViewType.wdPrintView

    oWordDoc.Tables(1).Cell(1, 1).Range.Text = "Test"

Note: If you are working from VBA or VB6 then you will have to add that line in your code if you are also facing that problem.