Image size macro pasted in Word 2007
Solved
Vesper Lynd
Posted messages
11
Registration date
Status
Member
-
bibilac -
bibilac -
Hello,
First of all, collectively, thank you to all the members of the forum for all the valuable lessons I have benefited from thanks to you for such a long time! I've learned more than 50% of what I know thanks to you :)
Well, otherwise, for the first time, I actually have a problem for which I haven't found any answer anywhere... I'm placing it in the Office forum somewhat randomly, I hope I'm not mistaken...
Starting situation: using a document in Form mode in Word 2007 (with input fields, drop-down lists, protection activated for input only in the fields and all that).
I'm making this doc available to users who must be able to insert screenshots without disabling the protection (while remaining in form mode, of course...).
Important element to know: they are not going to insert the screenshots using the image insertion function, but they will proceed by COPY/PASTE.
So I created a "paste my screenshot" button in the doc for this purpose.
Basically, the associated macro does a few things: it deactivates the protection, goes to the insertion point defined by a bookmark (named "ECRAN"), moves up a line, pastes, and reactivates the protection.
For all that, I have no problem.
What I can't do is to force the resizing of the pasted image so that it does not exceed the text area.
I tried using the recorder to catch the commands for managing the size of the selected image, but, to my great dismay, when I stop the recording, the macro created is just... EMPTY :((
I've tested a lot of things found online but nothing allows me to act on this object that I just pasted, I can't do it.
I'm willing to learn, especially about "InlineShapes", because right now, I don't understand how it works.
Here’s my starting code, for whatever it's worth...
Thanks again a thousand times for your help everyone
Private Sub CommandButton11_Click()
'If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
Selection.GoTo What:=wdGoToBookmark, Name:="ECRAN"
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Paste
' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If
End Sub
First of all, collectively, thank you to all the members of the forum for all the valuable lessons I have benefited from thanks to you for such a long time! I've learned more than 50% of what I know thanks to you :)
Well, otherwise, for the first time, I actually have a problem for which I haven't found any answer anywhere... I'm placing it in the Office forum somewhat randomly, I hope I'm not mistaken...
Starting situation: using a document in Form mode in Word 2007 (with input fields, drop-down lists, protection activated for input only in the fields and all that).
I'm making this doc available to users who must be able to insert screenshots without disabling the protection (while remaining in form mode, of course...).
Important element to know: they are not going to insert the screenshots using the image insertion function, but they will proceed by COPY/PASTE.
So I created a "paste my screenshot" button in the doc for this purpose.
Basically, the associated macro does a few things: it deactivates the protection, goes to the insertion point defined by a bookmark (named "ECRAN"), moves up a line, pastes, and reactivates the protection.
For all that, I have no problem.
What I can't do is to force the resizing of the pasted image so that it does not exceed the text area.
I tried using the recorder to catch the commands for managing the size of the selected image, but, to my great dismay, when I stop the recording, the macro created is just... EMPTY :((
I've tested a lot of things found online but nothing allows me to act on this object that I just pasted, I can't do it.
I'm willing to learn, especially about "InlineShapes", because right now, I don't understand how it works.
Here’s my starting code, for whatever it's worth...
Thanks again a thousand times for your help everyone
Private Sub CommandButton11_Click()
'If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
Selection.GoTo What:=wdGoToBookmark, Name:="ECRAN"
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Paste
' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If
End Sub
Configuration: Windows 7 32 bits Internet Explorer 8.0 Office 2007
18 answers
Hello,
I always say that it's better to reserve macros when absolutely necessary. Because it does generate quite a few problems. There's always someone who hasn't enabled macros, files being fetched to create another one, and suddenly, we lose the macro in the process, etc.
Because if I judge your model through your explanations, and considering you are using Word 2007, there’s no need for macros at all!
- Inserting form fields 2007
- Creating the table cell for the image
- Protection: protect everything (no modifications)
- Selecting form fields and the table
- Check Exception => everyone
And there you go! You have a lightweight, protected template that everyone can use without any issues.
You also need to keep one thing in mind: if the user decides to change the image, they'll end up reusing your macro which will only paste a new image, and the first one will still be there.
In short... in my opinion, either there needs to be more macros than that, or forget about macros.
m@rina
--
- "On the office forum, we ask questions about office automation..."
- "Oh really???"
I always say that it's better to reserve macros when absolutely necessary. Because it does generate quite a few problems. There's always someone who hasn't enabled macros, files being fetched to create another one, and suddenly, we lose the macro in the process, etc.
Because if I judge your model through your explanations, and considering you are using Word 2007, there’s no need for macros at all!
- Inserting form fields 2007
- Creating the table cell for the image
- Protection: protect everything (no modifications)
- Selecting form fields and the table
- Check Exception => everyone
And there you go! You have a lightweight, protected template that everyone can use without any issues.
You also need to keep one thing in mind: if the user decides to change the image, they'll end up reusing your macro which will only paste a new image, and the first one will still be there.
In short... in my opinion, either there needs to be more macros than that, or forget about macros.
m@rina
--
- "On the office forum, we ask questions about office automation..."
- "Oh really???"
Hi,
First of all, I want to clarify that I don't know VBA in Word...
However, I saw in the help that you can assign a height and width value to "InlineShapes". I think we should look into that.
I also saw on CCM: here this:
--
Best regards, and..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
First of all, I want to clarify that I don't know VBA in Word...
However, I saw in the help that you can assign a height and width value to "InlineShapes". I think we should look into that.
I also saw on CCM: here this:
Selection.InlineShapes(1).LockAspectRatio = msoTrue Selection.InlineShapes(1).Height = your height Selection.InlineShapes(1).Width = your width
--
Best regards, and..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
Thank you Pikaju
But it doesn't work :(
I get an error "5941" The requested member of the selection does not exist.
I think the issue comes from the fact that the image is pasted and not inserted using the Word insertion function.
I've been messing around with inlineshapes for 2 weeks without really understanding what I'm doing and with no success...
But it doesn't work :(
I get an error "5941" The requested member of the selection does not exist.
I think the issue comes from the fact that the image is pasted and not inserted using the Word insertion function.
I've been messing around with inlineshapes for 2 weeks without really understanding what I'm doing and with no success...
Here is the new macro, which causes the runtime error 5941:
Private Sub CommandButton11_Click()
'If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
Selection.GoTo What:=wdGoToBookmark, Name:="ECRAN"
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Paste
Selection.InlineShapes(1).LockAspectRatio = msoTrue
Selection.InlineShapes(1).Height = 200
Selection.InlineShapes(1).Width = 200
' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If
End Sub
I wonder if I should define a variable to declare the object I'm pasting.
Under Excel, I wouldn't be asking myself this question, but here under Word, especially 2007, I'm struggling back and I'm tackling the cliff....
Private Sub CommandButton11_Click()
'If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
Selection.GoTo What:=wdGoToBookmark, Name:="ECRAN"
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Paste
Selection.InlineShapes(1).LockAspectRatio = msoTrue
Selection.InlineShapes(1).Height = 200
Selection.InlineShapes(1).Width = 200
' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If
End Sub
I wonder if I should define a variable to declare the object I'm pasting.
Under Excel, I wouldn't be asking myself this question, but here under Word, especially 2007, I'm struggling back and I'm tackling the cliff....
Good gracious, we need to find...
Try this macro that has nothing to do with it, this is a test. If I understood correctly, it is not an InlineShapes object because your image is framed. It is a Shapes object. This macro will tell you:
sources
Shapes and InlineShapes have the same properties so after that it should work with .width and .height
--
Best regards, and ..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
Try this macro that has nothing to do with it, this is a test. If I understood correctly, it is not an InlineShapes object because your image is framed. It is a Shapes object. This macro will tell you:
sources
Sub TestTypeImage() Dim i As Integer If Not IsNull(ActiveDocument.InlineShapes.Count) Then i = ActiveDocument.InlineShapes.Count MsgBox "Your document has " & _ i & vbCrLf & " InlineShape type Images!" End If If Not IsNull(ActiveDocument.Shapes.Count) Then i = ActiveDocument.Shapes.Count MsgBox "Your document has " & _ ActiveDocument.Shapes.Count & vbCrLf & " Shape type Images!" End If End Sub
Shapes and InlineShapes have the same properties so after that it should work with .width and .height
--
Best regards, and ..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
SOMETHING CRAZY !!!!
Total crazy
I ran the macro before clicking my button and I discovered that I had 3 inlineshapes and 4 shapes.
I ran it again after my paste image button, and the Inlinesshapes count goes to 4 while the shapes remain at 4.
Moral of the story, it really creates an Inlineshape!!!
The thing I'm discovering is that I already had 3 :o
Of course, the thing is, it doesn’t know who it should be communicating with...
You're a champion :)
I despair of not having thought of doing this test! It's hard to get older :((((( lol
Total crazy
I ran the macro before clicking my button and I discovered that I had 3 inlineshapes and 4 shapes.
I ran it again after my paste image button, and the Inlinesshapes count goes to 4 while the shapes remain at 4.
Moral of the story, it really creates an Inlineshape!!!
The thing I'm discovering is that I already had 3 :o
Of course, the thing is, it doesn’t know who it should be communicating with...
You're a champion :)
I despair of not having thought of doing this test! It's hard to get older :((((( lol
It's just the beginning, a lead.
As soon as your code works, come back to post it here so it can be useful for future enthusiasts of resizing images in VBA Word...
And then mark this topic as "resolved".
--
Best regards, and ..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
As soon as your code works, come back to post it here so it can be useful for future enthusiasts of resizing images in VBA Word...
And then mark this topic as "resolved".
--
Best regards, and ..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
Hello,
If the goal is to get an image to a specified size, the macro is completely unnecessary.
Just draw a table cell with the correct width, then in the table properties, go to options, and uncheck the "Automatically resize to fit content" option.
This will also have the benefit of preserving the proportions of the image.
m@rina
--
- "On the office forum, we ask questions about office work..."
- "Oh really ???"
If the goal is to get an image to a specified size, the macro is completely unnecessary.
Just draw a table cell with the correct width, then in the table properties, go to options, and uncheck the "Automatically resize to fit content" option.
This will also have the benefit of preserving the proportions of the image.
m@rina
--
- "On the office forum, we ask questions about office work..."
- "Oh really ???"
Pfffffffff...
I kept your code proposal and, very naively, I transformed the little (1) into (4) thinking it would be the solution for him to recover the pasted image.
Nothing works, error 5941
While searching in other forums, I came across some things saying that error 5941 was related to some ActiveDocument story...
That doesn't ring a bell for me, how about you?
I kept your code proposal and, very naively, I transformed the little (1) into (4) thinking it would be the solution for him to recover the pasted image.
Nothing works, error 5941
While searching in other forums, I came across some things saying that error 5941 was related to some ActiveDocument story...
That doesn't ring a bell for me, how about you?
Thank you, Marina!
Indeed, it's a simple and effective approach. I appreciate your advice.
However, I might be stubborn, obstinate, or just plain thick-headed, but now that I've been fiddling with this code for a week, I admit I'm struggling to let go and give up... I would really like to understand what's not working, just for my personal knowledge...
But thanks again!!!
Indeed, it's a simple and effective approach. I appreciate your advice.
However, I might be stubborn, obstinate, or just plain thick-headed, but now that I've been fiddling with this code for a week, I admit I'm struggling to let go and give up... I would really like to understand what's not working, just for my personal knowledge...
But thanks again!!!
Yes, I understand you.
Your problem may come from the fact that every time you insert an image, the "counter" for inline shapes increments and you don't know what the current count is. There, for example, you should have put 5 because 4 was already done...
Try like this:
Otherwise, of course, I agree with Marina (hello!!).
--
Best regards, and ..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, then where is the problem? --
Your problem may come from the fact that every time you insert an image, the "counter" for inline shapes increments and you don't know what the current count is. There, for example, you should have put 5 because 4 was already done...
Try like this:
Dim i As Integer i = ActiveDocument.InlineShapes.Count Selection.InlineShapes(i).LockAspectRatio = msoTrue Selection.InlineShapes(i).Height = 200 Selection.InlineShapes(i).Width = 200
Otherwise, of course, I agree with Marina (hello!!).
--
Best regards, and ..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, then where is the problem? --
Yeah, it's not false...
You've convinced me (even if it annoys me not to have understood!)
Thanks again to both of you
You've convinced me (even if it annoys me not to have understood!)
Thanks again to both of you
Pijaku, it’s not working :( Thank you for your persistence.
I don’t understand why and I feel like I will never understand.
Should I mark the topic as resolved or not?
I don’t understand why and I feel like I will never understand.
Should I mark the topic as resolved or not?
Dim i As Integer i = ActiveDocument.InlineShapes.Count ActiveDocument.InlineShapes(i).Select Selection.InlineShapes(i).LockAspectRatio = msoTrue Selection.InlineShapes(i).Height = 20 Selection.InlineShapes(i).Width = 500
and if this works, mark it as resolved...
--
Best regards, and..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
So let's consider that Marina has the best solution.
As usual rrrrrrh!!!
Have a good day to both of you
--
Best regards, and ..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
As usual rrrrrrh!!!
Have a good day to both of you
--
Best regards, and ..... Happy New Year, my best wishes for 2010!!!
-- Every problem has its solution. If there is no solution, where is the problem? --
Re...
I see we're becoming reasonable!!! ;))
I haven't followed whether they are shapes or inline shapes, but here's a code example (to avoid getting you too annoyed!;)) that will set a width of 200 pt to all the inline shapes in the active document.
m@rina
--
- "On the office forum, we ask questions about office automation..."
- "Oh really???""
I see we're becoming reasonable!!! ;))
I haven't followed whether they are shapes or inline shapes, but here's a code example (to avoid getting you too annoyed!;)) that will set a width of 200 pt to all the inline shapes in the active document.
Sub drawings() Dim image For Each image In ActiveDocument.InlineShapes image.Width = 300 image.LockAspectRatio = True Next image End Sub
m@rina
--
- "On the office forum, we ask questions about office automation..."
- "Oh really???""
Hello,
I have a direct response to the initial question: I think the issue comes from the fact that the graphic we've just inserted is not selected, so any command referencing the InlineShapes list of the selection causes an error.
2 solutions: either we select the graphic we've just inserted:
Selection.InlineShapes.AddPicture FileName:="my graphic file"
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.InlineShapes(1).LockAspectRatio = msoTrue
Selection.InlineShapes(1).Height = CentimetersToPoints(10)
or we call the collection of InlineShapes from the active document (which seems more elegant to me):
Selection.InlineShapes.AddPicture FileName:="my graphic file"
ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count).LockAspectRatio = msoTrue
ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count).Height = CentimetersToPoints(10)
I hope this will please Pijaku (who may have moved on since ...)
I have a direct response to the initial question: I think the issue comes from the fact that the graphic we've just inserted is not selected, so any command referencing the InlineShapes list of the selection causes an error.
2 solutions: either we select the graphic we've just inserted:
Selection.InlineShapes.AddPicture FileName:="my graphic file"
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.InlineShapes(1).LockAspectRatio = msoTrue
Selection.InlineShapes(1).Height = CentimetersToPoints(10)
or we call the collection of InlineShapes from the active document (which seems more elegant to me):
Selection.InlineShapes.AddPicture FileName:="my graphic file"
ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count).LockAspectRatio = msoTrue
ActiveDocument.InlineShapes(ActiveDocument.InlineShapes.Count).Height = CentimetersToPoints(10)
I hope this will please Pijaku (who may have moved on since ...)