Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert doc to bmp 4

Status
Not open for further replies.

conjurer111

Programmer
Aug 11, 2002
76
IN
Hi,
I need to create an application where in I can convert a word document into a bmp file. For ex, if a doc file has got 2 pages, I need to convert each page as a bmp file. It would be best if the bmp file size commensurates with the paper size for the doc file. How do I code for this.

Thanks.
 
Hi, I would think that this would be kinda costly in time and money. You will have to convert the doc file into some object (like a PDF file) and then convert the PDF file into a image file like bmp. I would hope that you would have a lot of memory for a whole page bmp file would be in the meg-a-bits area. All this could be done with third party software.

John
 

Search this site for "Print Preview" or planet-source-code.com. Once you are able to preview a printable object you should be able to save that object as a bitmap (could be wrong but in theory?).

Good Luck
 
as I have not worked with graphics much I can only suggest what I would do if I were so tasked

first, I would convert my Word Document to a PDF file

next I would use Image Alchemy (or another conversion software) to convert that PDF file to individual BMPs
 
You may be better off considering using an Enhanced Metafile (EMF). Creating them from a Word document (or part of a word document) is pretty easy. Here's some illustrative code (you'll need a form with an image control and a command button, and add a reference to the Word Object library):
[tt]
Option Explicit

Private Sub Command1_Click()
Dim myWord As Word.Application
Dim myDoc As Word.Document

Set myWord = New Word.Application
Set myDoc = myWord.Documents.Add(, , , True)

myDoc.Select
myWord.Selection.Text = "Now is the winter of our discontent" + vbCrLf + "Hi there"


With myDoc.Content
.WholeStory
.CopyAsPicture
End With


Image1.Picture = Clipboard.GetData(vbCFEMetafile)

myDoc.Close False
myWord.Quit False
Set myDoc = Nothing
Set myWord = Nothing

' At this point Image1.Picture contains an EMF of what ever you selected in Word
SavePicture Image1.Picture, "c:\word.emf"
End Sub




 
I forgot to mention that if you really want to save it as a bitmap then you just need an EMF player to render the EMF into a picturebox's DC, which you can then save as a bitmap.

Now, whilst I do have code to do this, it suddenly occurred to me that there might be a way to get VB to do it for us. And you can, by leveraging the Image property of the picturebox. So here's the bmp version, which is only 3 or 4 lines longer (you need to add a picturebox to your form):
[tt]
Option Explicit

Private Sub Command1_Click()
Dim myWord As Word.Application
Dim myDoc As Word.Document

Set myWord = New Word.Application
Set myDoc = myWord.Documents.Add(, , , True)

myDoc.Select
myWord.Selection.Text = "Now is the wombat of our discontent" + vbCrLf + "Hi there"


With myDoc.Content
.WholeStory
.CopyAsPicture
End With


Image1.Picture = Clipboard.GetData(vbCFEMetafile)

myDoc.Close False
myWord.Quit False
Set myDoc = Nothing
Set myWord = Nothing

' At this point Image1.Picture contains an EMF of what ever you selected in Word

'SavePicture Image1.Picture, "c:\word.emf" ' Ok, we don't care about saving the EMF any more

' Let a picturebox's Image property do the work for us of rendering the EMF
' as a bitmap in the picturebox's picture property
Picture1.Width = Image1.Width
Picture1.Height = Image1.Height
Picture1.Picture = Image1.Picture
SavePicture Picture1.Image, "c:\word.bmp" ' Bingo, one bitmap from a Word document
End Sub
 
Oh - should have said that the picturebox should have it's borderstyle set to 0 (and you may want to change it's backcolor to white)
 
Hi Strongm,

this is absolutely great. one more clarification with the code. suppose i have a richtextbox (rtb1) where in i place all the text from a doc file and then use this line instead of the one you posted ie,

myWord.Selection.Text = rtb1.Text

'myWord.Selection.Text = "Now is the wombat of our
discontent" + vbCrLf + "Hi there"




then, the problem which i am facing is that only a part of whole of the text is being copied onto the image1 and there after picture1. how do i fix this.

in short, how do i load a whole word file onto the richtextbox and if possible create a number of bmp files according to the number of pages contained in the doc file. for ex, if the file contained 10 pages, then i would like to create 10 bmp files corresponding to the number of pages.

thanks for your help.
 
Oh, I have a completely different solution for Rich Text boxes, albeit a little more complicated...
 
Hi there,

would you mind posting it or may be suggest how to incorporate the above mentioned functionalities in the already existing code.
 
May be tricky to extract a generalised version of the RTF stuff from the specific application it was written for. I'll see what I can do.
 
strongm, have two stars! One for each post (the EMF and BMP versions of your code). They're both great.

conjurer111, while he's trying to set you up with his code, let me ask you some things, too, please.

You state that you get the text into Word from a RichTextBox, then continue with the rest of the code as strongm provided it, but that now "only a part [...] of the text is being copied [into] image1 and [thereafter into] picture1".

Where you get the text that you put into Word shouldn't have anything to do with how the program functions after that. The code after that point simply puts (a picture of) the whole Word doc's contents ("myDoc.Content.WholeStory") into image1 and so forth. (If I ask Farmer Brown for a photo of his chicken, and he gives me one, then the chicken's country of origin doesn't affect the fact that I now have a photo of Farmer Brown's chicken.)

But you're still having your problem, conjurer111. So, is the picture actually incomplete in the Image and Picture controls? Is it complete, but just too big for all of it to be seen in the Image or Picture control? (strongm's code resizes the Picture control to the same size as the Image control, but seems to assume that the Image control is the size you want it to be -- doesn't alter it. Did you consciously note that?)

If there are troubles with pasting a complete picture, I would check to confirm or deny that everything you want is being copied to the clipboard in the first place. Does strongm's code
Code:
    With myDoc.Content
       .WholeStory
       .CopyAsPicture
    End With
work correctly for you? I don't have full-blown VB here, so I can't test the whole code segment for you, reliably.

However -- Using Word 2000 (v.9) I copied the above statements into a separate Sub in a document's module. I typed in a bunch of stuff, with different fonts and sizes and colors, and a WordArt object to boot. I ran the code. What was copied to the clipboard -- and the help file confirms that this is correct (designed) behavior -- was exactly what would be copied to the clipboard had I simply used the .Copy method. To quote the help file, "The CopyAsPicture method works the same way as the Copy method for Range and Selection objects." (So why is it there anyway?) But I'll note in particular, the important point: Everything was copied, including the wordArt.

For instance:
I pasted into MS-Paint, and the text and WordArt appear as a bitmap. I pasted into Notepad, and the text appeared as plaintext (of course) in the default font, and the WordArt did not appear. I pasted into MS-WordPad, and the text appears as formatted in Word, and the WordArt appears as a metafile. I couldn't paste into MS Photo Editor (comes with MS Office 2000) or Wang Imaging (comes with Win NT 4.0), because Paste was not an available option.

So, what's on the clipboard is text (& other objects), with all the properties from Word -- *not* a (singled unified) picture. Undoubtedly, this code:
Code:
    Image1.Picture = Clipboard.GetData(vbCFEMetafile)
is what does the conversion to a metafile. So the question is, "Is *that* working for you properly?" I can't test that in the VBA I do have access to; it's strictly VB stuff (I don't know which versions, without references).

Hopefully, these are useful questions and meditations for you. Have a great weekend!

-- C Vigil =)
(Before becoming a member, I also signed on several posts as
"JustPassingThru" and "QuickieBoy" -- as in "Giving Quick Answers")
 
>seems to assume that the Image control is the size you want it to be

No need. An enhanced metafile contains sizing information; the Image control resizes itself appropriately when it plays the metafile
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top