NdNoviceHlp
Well-known Member
- Joined
- Nov 9, 2002
- Messages
- 3,752
My objective is to present a quick view of stored Word documents within an XL userform frame (a userfom listbox would contain the file names and a selection would display the doc within the userform frame). Not quite there yet. The following XL code copies the contents of the Word doc, pastes it to a chart, creates a jpeg file then loads the pic to the XL userform frame. To maintain the doc format it seems the chart has to be sized similar to the Word doc. Now for the issues, the document header/footer is not copied and only 1 doc page is copied. Not sure that these can be fixed and maybe really not important to the quick view objective. It all seems to work somewhat adequately with XL 2016 and Windows 10 however not so much in earlier office versions and/or earlier Windows os
the formatting deteriorates. Any ideas where to start looking... it seems like the document width/height is different? Also, an API solution seems like it should be possible. The only problem seems to be that the doc is copied as a jpeg not as a bmp file. Here is a link to a thread involving making pic files of XL ranges.
https://www.mrexcel.com/forum/excel...en-shot-given-range-every-worksheet-file.html
If someone could convert the API code to taking pic files of Word documents, it seems like that approach would be much better and may resolve my version issues. Anyways, thanks for any help. Dave
XL module code...
A large XL userform with a document wide Frame code...
The code requires the wb to have sheet2. The Word doc file name/location needs to be adjusted in this line...
To operate...
https://www.mrexcel.com/forum/excel...en-shot-given-range-every-worksheet-file.html
If someone could convert the API code to taking pic files of Word documents, it seems like that approach would be much better and may resolve my version issues. Anyways, thanks for any help. Dave
XL module code...
Code:
Public PFWdApp As Object
Public PagFlag As Boolean
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Sub NofileEr1()
'open Word application
On Error Resume Next
Set PFWdApp = GetObject(, "word.application")
If Err.Number <> 0 Then
On Error GoTo 0
Set PFWdApp = CreateObject("Word.Application")
'turn on pagination
End If
If PFWdApp.Options.Pagination = False Then
PFWdApp.Options.Pagination = True
PagFlag = True
End If
End Sub
Code:
Option Explicit
Sub ShowDocFrame()
Dim Fname As String, ht As Double, wt As Integer, FS As Object
Dim Flag As Boolean, ObjPic As Object, TmpFl As String
'Excel Userform code. Word doc image to XL userform frame
'requires Userform1 with Frame1 and sheet1 and sheet2 in wb
'places document image to Frame1 in Userform1
'check for open Word
Call NofileEr1
'copy test doc contents
Fname = "C:\testfolder\test.doc"
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.fileexists(Fname) Then
Flag = False
PFWdApp.Documents.Open Filename:=Fname, ReadOnly:=False
PFWdApp.activedocument.Windows(1).View.Type = 3 'wdPrintView
ht = 0
'get ht for chart from inlineshapes
For Each ObjPic In PFWdApp.activedocument.InlineShapes
ht = ht + ObjPic.Height
Flag = True
Next ObjPic
'get ht for chart from shapes
For Each ObjPic In PFWdApp.activedocument.Shapes
ht = ht + ObjPic.Height
Flag = True
Next ObjPic
'get ht for chart from text
If Not Flag Then
PFWdApp.activedocument.Characters.Last.Select
If PFWdApp.Selection.Information(3) < 2 Then 'page info
ht = PFWdApp.Selection.Information(6) 'wdVerticalPositionRelativeToPage
Else
ht = 792 ' 11 inches in points(72/in)
End If
End If
'copy content & close doc
PFWdApp.activedocument.Content.Copy
PFWdApp.activedocument.Close savechanges:=False
'paste to chart, make pic file, delete chart
Charts.Add.Location Where:=xlLocationAsObject, Name:="Sheet2"
TmpFl = ThisWorkbook.Path & "\" & "test.jpg"
With ActiveChart
.Parent.Width = UserForm1.Frame1.InsideWidth
.Parent.Height = ht
.Paste
.Export Filename:=TmpFl, Filtername:="jpg"
End With
wt = ActiveChart.ChartArea.Width
ht = ActiveChart.Parent.Height
ActiveChart.ChartArea.Select
ActiveWindow.Visible = False
ActiveChart.Parent.Delete
Sheets("Sheet1").Select
Sheets("Sheet1").Range("A1").Select
'load pic to userform frame
With UserForm1.Frame1
.ScrollBars = fmScrollBarsBoth
.KeepScrollBarsVisible = fmScrollBarsBoth
.ScrollWidth = wt
.ScrollHeight = ht
.Picture = LoadPicture(TmpFl)
.BorderStyle = fmBorderStyleNone
.Caption = vbNullString
End With
'clean up
Kill TmpFl
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
Else
MsgBox "NO file"
End If
Set FS = Nothing
If PagFlag Then
PFWdApp.Options.Pagination = False
End If
PFWdApp.Quit
Set PFWdApp = Nothing
Application.ScreenUpdating = True
Exit Sub
Erfix:
On Error GoTo 0
MsgBox "Error"
If PagFlag Then
PFWdApp.Options.Pagination = False
End If
Set FS = Nothing
PFWdApp.Quit
Set PFWdApp = Nothing
Application.ScreenUpdating = True
End Sub
Code:
Fname = "C:\testfolder\test.doc"
Code:
Call ShowDocFrame