copying the picture to different locations in the same document using a macro while the doc is prote

P

pieohpah

Hi there.

Im having a bit of a problem. Dont know much or maybe even less about
macros and VBA so been surfing the net trying to get my problems
solved.

I have made a template where ppl. can fill out my forms.
Problem is that during the protected state I should be able to insert
the same picture in different locations in the document.
I have allready found the code that allowes me to insert a photo in my
document while protected.
That means that I can press the same macrobutton 9 times and find the
same picture.
Isnt there some way so that I can press the button once and then have
a macro placing that photo in all 9 places before beinging my document
back into protected state?

Yours
Søren Vejle
 
J

Jay Freedman

If the code that inserts the picture in the first location also
inserts a bookmark that surrounds the photo, then you can have REF
fields in all the other places that will repeat the photo
automatically. The REF fields should be part of the template.

There are two considerations: (1) For code that can insert a bookmark,
see
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.
(2) The REF fields will have to be updated explicitly by the code,
because they don't update automatically.

If you need more information, please reply and include the code you're
using now.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
P

pieohpah

If the code that inserts the picture in the first location also
inserts a bookmark that surrounds the photo, then you can have REF
fields in all the other places that will repeat the photo
automatically. The REF fields should be part of the template.

There are two considerations: (1) For code that can insert a bookmark,
seehttp://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.
(2) The REF fields will have to be updated explicitly by the code,
because they don't update automatically.

If you need more information, please reply and include the code you're
using now.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.




Hi Jay Freedman.

Its your code that im using. I copied the one you posted on:
http://www.mcse.ms/message668131.html
-------------------------
Public Sub ProtectedInsertPicture()
Dim ilsPicture As InlineShape
Dim strFileName As String
Dim sngRatio As Single
Const max_width = 180 '216 = 3 inches (in points)

' temporarily unprotect
ActiveDocument.Unprotect


' show Insert Picture dialog
With Dialogs(wdDialogInsertPicture)
If .Display = 0 Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True
Exit Sub
End If
strFileName = .Name
End With
' remove macrobutton
Selection.Delete

Set ilsPicture = ActiveDocument.InlineShapes _
..AddPicture( _
FileName:=strFileName, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Range:=Selection.Range)

' limit size of picture to max_width (optional)
With ilsPicture
If .Width > max_width Then
sngRatio = CSng(max_width) / .Width
..Width = max_width
..Height = 122 '.Width * sngRatio
End If
End With

' reprotect, keeping form field contents intact
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End Sub
 
J

Jay Freedman

Its your code that im using. I copied the one you posted on:
http://www.mcse.ms/message668131.html
-------------------------
Public Sub ProtectedInsertPicture()
Dim ilsPicture As InlineShape
Dim strFileName As String
Dim sngRatio As Single
Const max_width = 180 '216 = 3 inches (in points)

' temporarily unprotect
ActiveDocument.Unprotect


' show Insert Picture dialog
With Dialogs(wdDialogInsertPicture)
If .Display = 0 Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True
Exit Sub
End If
strFileName = .Name
End With
' remove macrobutton
Selection.Delete

Set ilsPicture = ActiveDocument.InlineShapes _
.AddPicture( _
FileName:=strFileName, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Range:=Selection.Range)

' limit size of picture to max_width (optional)
With ilsPicture
If .Width > max_width Then
sngRatio = CSng(max_width) / .Width
.Width = max_width
.Height = 122 '.Width * sngRatio
End If
End With

' reprotect, keeping form field contents intact
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End Sub

--------------------------------------
Hope that you can tell me what to do :)

Yours
Søren Vejle

Hi Søren,

How interesting... I can't believe I'm the only person posting code
about this.

The first step is to choose a name for the bookmark that will surround
the macrobutton field and, eventually, will surround the picture
that's inserted in its place. I'll assume "MyPic" but you can choose
anything you like (provided it's a valid bookmark name not used
elsewhere in your document).

In your template, when you create the macrobutton field, select the
field and insert the bookmark named MyPic. At the other locations
where you want the picture to appear, insert copies of the field

{ REF MyPic }

Initially they'll repeat whatever display text you put into the
macrobutton field. You can place as many copies as you want, anywhere
in the document.

In the code, add this line just before the Const max_width line:

Dim Fld As Field

Then add these lines just before the comment that says
' reprotect, keeping form field contents intact

ActiveDocument.Bookmarks.Add Name:="MyPic", _
Range:=ilsPicture.Range

For Each Fld In ActiveDocument.Fields
If Fld.Type = wdFieldRef Then
Fld.Update
End If
Next Fld

(If you chose a different name for the bookmark around the macrobutton
field, change "MyPic" in the code to the name you chose.)

If you put REF fields in the header or footer, or in a text box,
you'll need extra code to force them to update.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
P

pieohpah

Hi Jay.

Im really gratefull that you help me. Been surfing the net and the
only code concerning this issue was yours. So in my book your a
lifesaver :)
Ive pasted the code as mentioned above and it works fine in the
template but when i open a normal copy of the template the pictures
wont show?
Doesnt it work on doc's created from the template?

But all in all - you have really been helpfull and thx a bunch.

Yours
Søren vejle
 
P

pieohpah

Hi again Jay.

Sorry bout the post before - figured it out. Had something to do with
enable/disable macros in word general.
So it works now really cool.

Yours

Søren vejle
 
J

Jay Freedman

Hi again Jay.

Sorry bout the post before - figured it out. Had something to do with
enable/disable macros in word general.
So it works now really cool.

Yours

Søren vejle

Excellent -- glad I was able to help.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top