• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1351
  • Last Modified:

How do I paste a picture (BMP) from the clipboard to a Word form?

I want to create a form that accepts a bmp pasted from the clipboard. I know how to create a form but not what I need to put on it to accept bmp from the clipboard.
0
Sparky191
Asked:
Sparky191
  • 27
  • 16
  • 11
  • +3
2 Solutions
 
Joanne M. OrzechManager, Document Services CenterCommented:
Are you talking about a protected form?  Or a user form?  What version of Word?
0
 
Sparky191Author Commented:
Word 2002, protected form fields. I want to create a form that the user can paste a graphic into. I'll then position the graphics as apporpriate using VBA behind the form.  
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
You should be able to insert continuous section breaks into your form where you want the bmp to appear and then unprotect only that section of the form.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
DreamboatCommented:
Sparky191:

Do you have only a certain number of graphics? Or must you allow your users to insert ANY graphic? If ANY graphic, then, you'll want to use JOrzech's method, or make a special menu option to insert a picture... I don't code, but someone might be able to give you the code it would require.
0
 
Sparky191Author Commented:
Adding section breaks might not be possible. I'd like to leave the structure of the docuement as is. I guess what I need is to be able to copy a graphic from the clipboard into VBA.
0
 
Sparky191Author Commented:
Adding section breaks might not be possible. I'd like to leave the structure of the docuement as is. I guess what I need is to be able to copy a graphic from the clipboard into VBA.
0
 
dbase118Commented:
Just an FYI...I tried the section break idea earlier...it didnt work for me. It said "end of protected section" but the entire doc was still locked when I put the protection on.
0
 
Sparky191Author Commented:
I could even save the bmp from the clipboard to a temp location then load it into my word form. How would I do that.
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
Just an fyi sparky - inserting continuous section breaks does not alter the "structure" of the document at all.
0
 
Sparky191Author Commented:
I can't add section breaks. I can't add anything new to the document. Graphics are already there. I'm just changing how they are inserted to the document.
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
Oh - now I really don't understand....  sorry - I'm getting lost in this one...
0
 
Sparky191Author Commented:
I want to be able to paste a picture from the clipboard into a user form. The reason I'm doing this is because the document is protected. I can't set the section with the graphic as unprotected.
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
I still don't understand - it's probably me.... but here's some code....

Sub PasteBMP()
'Unprotect via code
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
'Paste your bitmap
Selection.Paste
'Reprotect form
ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
    wdAllowOnlyFormFields
End Sub
0
 
Sparky191Author Commented:
I have a form protected document. All user access, modifications are via forms I've provided for the users. I want to put a picture box on one form so that the user can paste a graphic into this form from the clipboard. Graphic is a BMP. How can I do this?

I don't want to add section breaks as changing the documents (theres a few hundred of them) means a massive amount of work, not only in editing but getting each document checked and signed off again. Also the graphic is not easily seperated from other content using section breaks. So using section breakes would leave some other content unprotected and I don't want to do that either.

Accessing the clipboard is a problem from VBA. You can do is using an API but the examples I've seen only access text and not a graphic. If I was to load the graphic from a file into the form. How would I do that?
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
You can't paste bitmaps into a protected form without first unprotecting it - pasting - and reprotecting - even using code.

What you're asking for cannot be done.

0
 
DreamboatCommented:
Jo:

Part of my understanding has been confused because it sounds like Sparky is using a protected form (which is neither here nor there for what he's asking), but ALSO has a userform (VBA-type) wherein he wants to allow the user to place a graphic. Now, I don't know why anyone would want a graphic on a userform, but that's what it sounds like to me that Sparky wants.

Given that, the protection of the document is neither here nor there, correct?

Can you confirm, Sparky?
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
But I assume (and, of course, we know that what means lol :) once the user has selected the graphic from the "user form" that sparky then wants to paste it into the document....


0
 
Sparky191Author Commented:
Acutally you're all right. I was hoping that the user would past the graphic into the a custom form, and then I'd be able to insert the graphic using VBA into the form protected document. But it seems I can't do that. I can run code (which I tested) but not write to the document (which I didn't test till now) with the protection turned on. But I could turn off the protection using VBA insert the graphic then turn the protection back on again. Does that sound logical?

But haven't been able to find a way of getting a graphic from the clipboard in VBA. I can get text ok, but not a graphic. Is there a control I can use on a form that allows the user to paste a graphic into it? (Its a barcode BTW). I want to lock down the content so user access in only via forms that I provide. Current the documents use a lot of field codes and bookmarks, but the only other thing I need to modifiy in the document is this barcode graphic. Alternatively I might have to look at generating the barcode myself in VBA, but I was hoping to avoid that if possible.  I don't want to use protected sections if at all possible.
0
 
DreamboatCommented:
>Does that sound logical?

Absolutely. It's the only way.
0
 
Joanne M. OrzechManager, Document Services CenterCommented:

"But I could turn off the protection using VBA insert the graphic then turn the protection back on again."

My code posted above will do exactly that....

Another suggestion would be to add the graphic as an autotext entry to the template and insert it via code.  The code above would be modified to read:

'Unprotect via code
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
'Paste your bitmap
ActiveDocument.AttachedTemplate.AutoTextEntries("BarCode").Insert _
        Where:=Selection.Range
'Reprotect form
ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
    wdAllowOnlyFormFields
0
 
Sparky191Author Commented:
The graphic is generated by another application. Its a barcode. I may be able to generate it in VBA using a special font, and I'll look into that option later. But for now I don't want to write code for something that I haven't been asked to.

With regard to my orignal question how can I post a graphic into a word form? I still prefer to do that rather than giving the user access to the main content. Even if its the same thing, to the user it won't be obvious that the document is being unprotected then reprotected.
0
 
Sparky191Author Commented:
I should add thanks to all who have answered so far you've all been very helpful. Its appreciated thanks.
0
 
DreamboatCommented:
Sparky: Can you tell us? How many graphics will the user have to pick from?
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
Then use my first original code posted above.  The user has no idea anything is being protected or unprotected - its a nanosecond to unprotect, paste, reprotect.



0
 
Sparky191Author Commented:
Number of graphics? The barcodes are generated from a number that the user inputs using Barcode 97 (Win 3.1 app). So there could be an umlimited number. A few thousand a the very least. Why is that important?

JOrzech - I know how to protect and unprotect the document. Just I don't know how to create a form that the user can paste a graphic into though. Onec I know how to do that, I'm away! Haven't much experience ith picture controls on forms, in VB or VBA you see.
0
 
DreamboatCommented:
Okay, gotcha, Sparky. Why is it important? Because if you only had 10, again, you could just use autotext, and those graphics would be stored right along with the document. Instead of choosing a file, your users could just pick from a list and depending on the choice, the autotext entry would run. JOrzech and I worked on a similar project. There are about 18 languages to choose from. The user picks a language from a menu, and an autotext is run that inserts a graphic in that language... I didn't mean to keep beating you up about it, but it could have saved you lots of time if the number of graphics were minimal.
0
 
Sparky191Author Commented:
Ahh I see. Actually the template contains a lot of autotext already. I've moved it from the normal.dot to the new custom template that I'm building. Didn't know you could do that before. Handy little trick. Now if some one could tell me how to paste a bmp on a form? I could load the bitmap from a file but that changes the current process that the user have and I'd like to stick as closely to the original process. Mean a lot less paper work and documentation if I can.
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
Once the graphic is on the clipboard - code is:


Selection.Paste
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
Or you could send me the graphic and the template and I'll write it for you.....e addy in profile.
0
 
Sparky191Author Commented:
That pastes it into the document not the form.

What template the Word Basic template I'm converting? Why? I just need to know how to paste a graphic onto a word form picture control.

Can it even be done?
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
I'm sorry - I give up on this one.... I just don't understand what you're trying to do...
0
 
DreamboatCommented:
JO: From what I gather:

He's got a userform that pops up on open.
On that userform, he's giving a dropdown or dialog that let's the user choose a graphic file.
He then wants that graphic file to appear on the userform.
Then, I assume, on close of the userform, that graphic file is then placed in some location of the document.

Sparky: You'll need a bookmark into which the graphic should be placed. Give us a bookmark name....
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
But if the graphic is already on the clipboard, why do they have to choose one?  This is the confusing part.
0
 
DreamboatCommented:
I kinda wondered about the need to have it on the userform myself. Why not just let them pick the graphic, period. Then, on close of the userform the graphic is dropped into the doc.
0
 
Sparky191Author Commented:
Can we all focus on the original question and not get dragged off topic which is what is happening.

"I want to create a (word 2002) form that accepts a bmp pasted from the clipboard. I know how to create a form but not what I need to put on it to accept bmp from the clipboard"




0
 
Joanne M. OrzechManager, Document Services CenterCommented:
I have told you how to paste it almost three times now.... I'm sorry - I'm outta here....
 
0
 
Sparky191Author Commented:
Sorry our wires have gotten crossed somewhere.

The issue has been confused by discussing user forms and protected forms at the same time. It was established that you can't paste into a protected form earlier. So I want to paste a BMP into a user form instead.

You keep telling me how to paste into a word document? I didn't ask you that. You also told me how to protect and unprotect a word document. I didn't ask you that either. I want to paste into the user form. You can't see why I want to do that if its the same thing as pasting into the word document in code. In fact it isn't.

If the user does something else with the clipboard between the graphic going in there and the user clicking on my form then I'll be pasting in the wrong data/bmp. The user won't know since they don't get any visual feedback. So the code/template/application is at fault. However if the user can see the graphic in a form and clicks ok, then thats fine. They agreed to insert the graphic thats in the user form. If they get it wrong, then it their fault. Theres a world of a difference. One way has a lot less potential for error, which is the whole point of doing this in the first place.

But thanks for your help thus far.

Anyone else help with pasting a BMP into a user form?
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
I'm sorry Sparky but my understanding was that you already had the bitmap in the clipboard...I thought you did that through code.

And your statement:

"It was established that you can't paste into a protected form earlier."

is untrue... that's the code I posted.  It will do that.  Have you even tried it?

You can unprotect momentarily and paste it and then reprotect.

I don't mean to be difficult, but you've got to at least try the suggestions posted.

0
 
Sparky191Author Commented:
I don't need to try what has been suggested thus far. I understand it fully its quite simple stuff. But it doesn't answer the question I originally asked. Which is how to create a userform that I can paste a bmp into.

If you unprotect it via VBA even for a milli sec then its no longer a protected document. You can't have it both ways. Your method is simply pasting into an unprotected document. I am correct. You can't paste a bitmap into a protected document. Using "selection.paste" You have no idea whats in the clipboard and therefore no idea what you are pasting from it.  Neither does the user. I can't be that vague in my code. If I'm pasting in something it has to be the right data. I may be required to validate it. By pasting it into a userform picture box the user can do a visual inspection before inserting it into the document, by clicking an ok button or similar. Thats good practise. What you're suggesting isn't and would be unacceptable for my purposes. I'm not trying to be difficult. I have a specific thing to do, I can't just decide to do an unsecure method, despite how much easier that would be. It simply isn't secure enough for what I'm doing.

The problem is how do I get a BMP from the clipboard into a picture box in a userform in Word 2002. I can get get text from the clipboard as follows

Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText

But I can't find out how to get the BMP into my userform.



0
 
Sparky191Author Commented:
I should add that Dreamboat is exactly right about what I'm doing. However I have no control over having to get the BMP from the clipboard. Thats just the way it is. It doesn't exist anywhere else for me to list them for a user to pick one, or from a file that I can load it from. It exists only in the clipboard. Nothing I can do about that. However in my experience using the clipboard without checking whats in it is bad practise as it not unusual for another application running concurrently to write to the clipboard before you've read from it. You have to validate what you are getting from it. I'm using the userform to allow the user validate whats being pasted into the protected document. JOrzech - I don't  think you understand the business/security reasons for coding this way. If you don't understand then it would appear to a fussy and overcomplication. But I assure you it isn't.
0
 
Joanne M. OrzechManager, Document Services CenterCommented:
As I said, I thought you already had the code to grab the bitmap - you asked how to paste it into a form.  

Your original question said:

"I want to create a form that accepts a bmp pasted from the clipboard. I know how to create a form but not what I need to put on it to accept bmp from the clipboard."

I'm not going to go on and on with this - I do understand security - I work for lawyers....so believe me, that's their top priority.

But I am bowing out of this discussion - sorry I couldn't help you Sparky.

Hope you get an answer you can live with.

0
 
Sparky191Author Commented:
JOrzech - Thanks for your help thus far. Sorry I could explain myself more clearly for you.
0
 
Suat OzgurWeb / Application DeveloperCommented:
Hi all,

I really cannot read the whole conversation but;

Sparky,

You have a picture data in clipboard (already copied picture) and now you want to put this data from clipboard to Image control on a Word userform by using VBA ?

Just let me know this shortly - yes or no, because if you need this then I will give you the code to do that.

Suat
0
 
Suat OzgurWeb / Application DeveloperCommented:
Sorry, forgot to mention.

I am asking that question because I will need to re-write to code for you. Make it shorter etc.. So if it is not what you need, then I don't want to spend time on it.

Suat
0
 
Sparky191Author Commented:
smozgur - Yes exactly that

... in addition I want to then copy from the Image control into the word document when the user clicks a button "ok" etc. Once  I see how to reference the control and the bmp I should be ok.
0
 
Suat OzgurWeb / Application DeveloperCommented:
For the first part as I promised, I'll be back in 10 minutes.

Suat
0
 
Suat OzgurWeb / Application DeveloperCommented:
BTW, no need to copy from Image, since it is already Clipboard Data (Selection.Paste after making sure about the picture by viewing with my code in the Image control as already suggested. It is already loaded into Clipboard, see ? ).

Suat
0
 
Sparky191Author Commented:
But then you are not pasting the image in the control (what the user is looking at) but what happens to be in the clipboard. A small but significant difference no? Anyway thats a minor detail once I have whats in the clipboard on the form.
0
 
Suat OzgurWeb / Application DeveloperCommented:
>But then you are not pasting the image in the control

Clipboard is not being cleared when you paste data from Clipboard.

Suat
0
 
Sparky191Author Commented:
Not by us (VBA developer) no. Its a small point and I conceed that. But users have a habit of alt tabbing and doing something else just when you don't want them to. Lets not worry about it for now though.
0
 
Suat OzgurWeb / Application DeveloperCommented:
(I really cannot remember the source since I got the method a long time ago and already changed it to work with Bitmap clipboard data but you can use the following code)

Following code basically opens the clipboard and copy the picture data as object (on fly) by using Clipboard APIs and set the image picture by using this object. Easy, right ?

To test it, please create a new userform, put an image control on it named as Image1 and a command button named as CommandButton1 then copy & paste the following code into the USERFORM module declaration section then run the Userform and goto copy a bitmap picture and return form and finally click on the command button.


'----Code Start----
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
Private Declare Function CopyImage Lib "user32" (ByVal handle As Long, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long

Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

Private Type uPicDesc
    Size As Long
    Type As Long
    hPic As Long
    hPal As Long
End Type

Private Sub CommandButton1_Click()
    Call GetPicture
End Sub

Sub GetPicture()
Dim h As Long
Dim hPtr As Long
Dim hPal As Long
Dim hCopy As Long
    On Error GoTo ErrHandler
    If IsClipboardFormatAvailable(2) = 0 Then Exit Sub
        h = OpenClipboard(0&)
        If h > 0 Then
            hPtr = GetClipboardData(2)
            hCopy = CopyImage(hPtr, 0, 0, 0, 4)
            h = CloseClipboard
            If hPtr = 0 Then Exit Sub
           
            Dim r As Long
            Dim uPicInfo As uPicDesc
            Dim IID_IDispatch As GUID
            Dim IPic As IPicture
                With IID_IDispatch
                    .Data1 = &H7BF80980
                    .Data2 = &HBF32
                    .Data3 = &H101A
                    .Data4(0) = &H8B
                    .Data4(1) = &HBB
                    .Data4(2) = &H0
                    .Data4(3) = &HAA
                    .Data4(4) = &H0
                    .Data4(5) = &H30
                    .Data4(6) = &HC
                    .Data4(7) = &HAB
                End With
                With uPicInfo
                    .Size = Len(uPicInfo)
                    .Type = 1
                    .hPic = hCopy
                    .hPal = 0
                End With
                r = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic)
                If r <> 0 Then MsgBox "Error": Exit Sub
                Set Image1.Picture = IPic
        End If
        Exit Sub
ErrHandler:
        h = CloseClipboard
End Sub
'----Code End----


Enjoy API programming.
Suat
0
 
DreamboatCommented:
Wow. That's a lotta code for 70 points. :)
0
 
Sparky191Author Commented:
Ok thanks thats perfect! Exactly what I was looking for. Had to unload the userform or it wouldn't quit for me, seemed to be in a loop. I had to reference the image1.picture as userform1.image1.picture but thats ok. Of course I want to know how to write to word from the image control now! : ) Because if I alt tab and do a copy before the I click my button and "selection.paste" runs I'll get the new item in the clipboard and not the image from the form. I know that would come back and haunt me eventually.




0
 
DreamboatCommented:
But but but....Sparky! That's another question!!
0
 
Sparky191Author Commented:
Ah fair enough. Tell you what. I'll leave this open a little while, sign up properly and get some decent points. As this has been hugely helpful and exactly what I was looking for.  I'll boost up the points and divide them around. Can't say fairer than that. definately a nice bit o code. Just need that last 10% for perfection. Last bit is another question alright. Shall I start another question? Have to sign up for that though. No points left you see.
0
 
DreamboatCommented:
Don't sign up again, Sparky...it's not really legal.
Can't buy a few points?
0
 
Sparky191Author Commented:
Registered properly now.

What do you mean not legal?
0
 
Sparky191Author Commented:
I've boosted the points and will split them appropriately. But although not part of the original question, it would make logical sense to finish this question with how to read the graphic from the form and write it to the active doc. I think this is important as I couldn't find this information anywhere and it would make a good resource for others reading the question. Otherwise its only half finished. I'll check back tomorrow.

Thanks for all your help thus far everyone. Really helped me out.
0
 
DreamboatCommented:
Oh.....
I thought you were going to register under some other name to get more points. LOL!!
0
 
Suat OzgurWeb / Application DeveloperCommented:
Sparky,

You also couldn't find the first one anywhere, right ? :) j/k

Anyway, for your information: I am currently working on a project (at 03:10AM) so I certainly cannot write the necessary code for the second part. But I can hopefully take care of it when I get chance tomorrow. And if someone comes before me to end it up quicker for you then I would be of course happy.

BTW: yes, it will be a good resource.

Suat
0
 
Sparky191Author Commented:
Nah only starting out here on EE. Have to give a good impression. Besides the help and info thus far has been excellent. This is the second time smozgur has come to the rescue. Have to pay my dues and all that. BTW nice looking book Dreamboat take long to write?

0
 
Sparky191Author Commented:
smozgur - its 1.24am for me and a 5.45 rise with a 2hr commute. So I know exactly how you feel. But I look forward to seeing the second part. Now that I know its possible I can move forward with my project. I'm only doing documentation and prototyping at the moment. Talk to you later.
0
 
piekematCommented:
The code is from Stephen Bullen's Excel Page (http://www.bmsltd.co.uk/Excel/Default.htm).

I would certainly be interested in finding out how to place the bitmap from the image control back onto a blank Word document.
0
 
Suat OzgurWeb / Application DeveloperCommented:
Thanks for the information piekemat.  I think it is.

You can look at the other Sparky's other question for the 2nd part. I just sent a possible solution (other than copying image picture as file and reloding as shape) today.

Suat
0
 
Suat OzgurWeb / Application DeveloperCommented:
0
 
piekematCommented:
Thanks Suat,

That's exactly what I was looking for!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 27
  • 16
  • 11
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now