• Force PrintLayout view (WinXP / Word2003 NL)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Force PrintLayout view (WinXP / Word2003 NL)

    Author
    Topic
    #425894

    In a Sub I open a new document based on a specific template with:

    Dim TargetDoc As Word.Document
    Set TargetDoc = Documents.Add(Template:=”T:WordTemplatesRequest.dot”)

    ‘ some more code

    TargetDoc.ActiveWindow.View.Type = wdPrintView
    TargetDoc.SaveAs FileName:=”C:TestRequest.doc”
    TargetDoc.Close

    The problem is that, to my surprise, after opening Request.doc is shown in Normal view, despite of the first line with View.Type above. Manually applying View | PrintView and then saving & closing again seems to cure the problem: re-opening shows the doc in Printview. Can anyone shed some light at this?

    Viewing 2 reply threads
    Author
    Replies
    • #982593

      I cannot reproduce this, but you could set Print View in the Document_Open event of the Request.dot template:

      Private Sub Document_Open()
      ActiveWindow.View = wdPrintView
      End Sub

      • #982769

        Thanks Hans, but I cannot do that. All our templates are code-less and have to stay that way. All code sits in a global code-only template.

    • #982662

      Word stores the view only if it considers the document to be dirty (unsaved) when closing. For instance if I open a file and do nothing to it other than change the view then this won’t be saved but if I change the view and also add a character to the file then the view will be saved on next open.

      I think you would have more luck if you changed the view, then did something ‘saveworthy’ such as a text change (and unchange if you like) and then saved the document.

      • #982791

        Thanks Andrew. I know that Word only saves documents that are “dirty”. And the TargetDoc in my original post must be “dirty” because it is just created and not saved yet. It’s the result of converting a document that in reality is a disguised remplate – see my post 518,370.
        The whole conversion code (typed over, cannot upload anything because I’m not on my own work-PC right now, so typo’s may occur):

        Sub ConvertDoc
        Dim SourceDoc As Word.Document
        Dim TargetDoc As Word.Document
        Dim rngSourceDoc As Range
        Dim rngTargetDoc As Range
        Dim strConvDoc As String
        Dim strTemplate As String
        Dim strTemplPath as String

        On Error Goto Error_ConvertDoc

        Application.ScreenUpdating = False
        System.Cursor = wdCursorWait

        Set SourceDoc = ActiveDocument
        strConvDoc = SourceDoc.FullName
        strTemplate = LCase(SourceDoc.CustomDocumentProperties(“Template”).Value) & “.doc”
        strTemplPath = “T:WordTemplates”
        Set TargetDoc = Documents.Add(Template:=strTemplPath & strTemplate, Visible:=False)

        SourceDoc.Activate
        ProtOff SourceDoc ‘Generic Sub to lift document protection
        Set rngSourceDoc = SourceDoc.Range
        rngSourceDoc.MoveEnd Count:=-1
        rngSourceDoc.Copy

        TargetDoc.Activate
        ProtOff TargetDoc
        TargetDoc.Content.Delete
        TargetDoc.Range.Paste
        ProtOn TargetDoc ‘Generic Sub to set document protection

        TargetDoc.ActiveWindow.View.Type = wdPrintView
        TargetDoc.ActiveWindow.Visible = True
        SourceDoc.Close SaveChanges:=wdDoNotSaveChanges
        Kill strConvDoc
        TargetDoc.SaveAs FileName:=strConvDoc

        Exit_Error:
        If Not TargetDoc Is Nothing Then TargetDoc.Close SaveChanges:=wdDoNotSaveChanges
        System.Cursor = wdCursorNormal
        Application.ScreenUpdating = True
        Set SourceDoc = Nothing
        Set TargetDoc = Nothing
        Set rngSourceDoc = Nothing
        Set rngTargetDoc = Nothing
        Exit Sub

        Error_ConvertDoc:
        Select Case Err.Number
        Case 4605
        Err.Clear
        Resume Next ‘No Paste
        Case Else
        MsgBox “Error nr.:”& vbTab & Err.Number & vbLf & _
        “Descr.:”& vbTab & Err.Description, _
        vbCritical + vbOKOnly, “iBox: ConvertDoc”
        Resume Exit_Error
        End Select
        End Sub

        So this conversion is a copy of the whole content of a template-doc into a new, real doc based on the same template. That’s why I don’t understand why PrintView doesn’t stick. Apart from this, the conversion works as intended, but any comments and suggestions about this piece of code are welcome.

        • #982943

          Perhaps SaveAs doesn’t save the view but Save does?

          Another possibility is that the ProtOn function actually saves the document and then the SaveAs saves a non-dirty file.

          Does the macro get noticeably slowed down if you move the PrintView line above the Paste line (two lines above)?

          • #983729

            Thanks Andrew and Howard.

            The ProtOn / ProtOff functions do just that: set or lift the protection of a document. I made those functions myself, so I’m positiv that there’s no Save in them.
            Moving the PrintView line doesn’t slow the macro down, but it didn’t do the trick either. But I solved the problem by adding a paragraph at the end of the docs after the initial Save As and then Saving the doc again. So you were right in your first reply, Andrew, about only saving the altered View when something else is changed. Thanks again.

    • #983015

      THe following works in Word 2003, whether or not I use the With construct.

          Dim TargetDoc As Word.Document
          Set TargetDoc = Documents.Add(Template:=NormalTemplate.FullName)
          '
          ' some more code
          '
          With TargetDoc
              .ActiveWindow.View.Type = wdPrintView
              .SaveAs FileName:="C:TestRequest.doc"
              .Close
          End With
      

      Are you certain that there are no AutoClose or document_close subs lurking?

    Viewing 2 reply threads
    Reply To: Force PrintLayout view (WinXP / Word2003 NL)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: