Question

VB Macro Find/Replace with {INSERTPICTURE} loop

Asked by: nheger

Hi,

I need a macro that will search a document for a specified string of text.  If the string is found, it will be replaced with an {INSERTPICTURE} field that will link to a picture (JPG) on my intranet.  If the string is not found, it will loop and search for the next string.  There are approximately 10 strings of text, each linking to a unique web address for a different picture.  

This macro is similar to the one described here: http://www.experts-exchange.com/Applications/MS_Office/Word/Q_20984704.html?query=find+replace+picture&topics=605.

However, this macro will be used by users of Word 97 (with Win 98) up to the most current version of Word (with Win2000 and XP).  Therefore, this macro cannot contain any strings/objects that are not found in older versions of VB.  This is why I cannot use the macro described above.  

Also, there is an issue with the naming - my files contain two spaces in the name.  (eg, "Flammable - Canada.jpg") The macro will need to address this issue as well, since I am unable to change the names of these without great difficulty.  

Thank you very much for any help you can provide!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-08-23 at 07:42:57ID21103705
Tags

word

,

insertpicture

Topic

Microsoft Word

Participating Experts
2
Points
250
Comments
36

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. VB Macro
    I am a 15 year old delphi programmer and I have recently discovered the vb macro builder in microsoft word. I am trying to learn the language with this (since i have no access to a full version) but i have run into some problems. The macro version is extremly limited. I wish ...
  2. excel macros
    hi!, i have some macros from excel that i would like to run from a VB program.what do i need to do this?. Thanks in advance.
  3. Macro
    I need to write a macro. I have this report exported to excel format and saved in a folder. I want to write a macro to get that excel file from that folder and have to sort it by partnumber(a column in the excel file) and subtotal that column by part number and also subtotal ...
  4. Macros
    Hello Experts, I have an excel spreadsheet that contains macros. It there is no record to be put in one of the columns, I want to input zero in each cells of the empty column. Can you please write a simple code in Vb that does solve this problem. You can pick any column you ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: GrahamSkanPosted on 2004-08-23 at 15:13:36ID: 11875814

Hi,
As far as I can see there are no version issues with the macro that you indicate.
 
File naming is an OS issue and has not changed since long names were introduced.

Since you have the environments that require validating, I suggest that you try them and, if you have a problem and can't work out a fix, that you request help at that point.

Regards, Graham

 

by: GrahamSkanPosted on 2004-08-24 at 03:01:40ID: 11879561

Sorry, I was wrong. There are two functions that will not be in Word 97, Replace and InstrRev

This code from www.FreeVbCode.com can be used instead

You can add them alongside the macro.

Public Function InStrRev(ByVal sIn As String, ByVal _
   sFind As String, Optional nStart As Long = 1, _
    Optional bCompare As VbCompareMethod = vbBinaryCompare) _
    As Long

    Dim nPos As Long
   
    sIn = Reverse(sIn)
    sFind = Reverse(sFind)
   
    nPos = InStr(nStart, sIn, sFind, bCompare)
    If nPos = 0 Then
        InStrRev = 0
    Else
        InStrRev = Len(sIn) - nPos - Len(sFind) + 2
    End If
End Function

Public Function Join(Source() As String, _
    Optional sDelim As String = " ") As String

    Dim nC As Long
    Dim sOut As String
   
    For nC = LBound(Source) To UBound(Source) - 1
        sOut = sOut & Source(nC) & sDelim
    Next
   
    Join = sOut & Source(nC)
End Function

Public Function Replace(ByVal sIn As String, ByVal sFind As _
    String, ByVal sReplace As String, Optional nStart As _
     Long = 1, Optional nCount As Long = -1, _
     Optional bCompare As VbCompareMethod = vbBinaryCompare) As _
     String

    Dim nC As Long, nPos As Long
    Dim nFindLen As Long, nReplaceLen As Long

    nFindLen = Len(sFind)
    nReplaceLen = Len(sReplace)
   
    If (sFind <> "") And (sFind <> sReplace) Then
        nPos = InStr(nStart, sIn, sFind, bCompare)
        Do While nPos
            nC = nC + 1
            sIn = Left(sIn, nPos - 1) & sReplace & _
             Mid(sIn, nPos + nFindLen)
            If nCount <> -1 And nC >= nCount Then Exit Do
            nPos = InStr(nPos + nReplaceLen, sIn, sFind, _
              bCompare)
        Loop
    End If

    Replace = sIn
End Function

 

by: nhegerPosted on 2004-08-24 at 05:39:28ID: 11880650

Thanks, GrahamSkan.  
Although, I don't understand where to put in my choices for the variables to search for and the web addresses to which they link.  I'm not a huge novice, but I need some more clarification, much like was done in  http://www.experts-exchange.com/Applications/MS_Office/Word/Q_20984704.html?query=find+replace+picture&topics=605.  

Or, does the page on freevbcode.com explain it?  If so, can you include the complete link?  
Thanks.

 

by: GrahamSkanPosted on 2004-08-24 at 06:54:14ID: 11881424

All I've actually done so far is to ensure that the macro would run in Word97.

I'll have another look in a few hours if no-one else has given you the answer by then.

 

by: GrahamSkanPosted on 2004-08-24 at 11:39:35ID: 11885071

And I didn't even do that. I only gave you two functions of those in the code. Wouldn't be a problem, except that one of the missing ones (Reverse) is called in the code I gave you.

The idea of the macro is that you make your pictures folder = to the sPath. You need to have double backslashes because the backslash is a special character in Word fields.

This is the full code (from http://www.freevbcode.com/ShowCode.Asp?ID=17)

'Split         Split a string into a variant array.
'
'InStrRev      Similar to InStr but searches from end of string.
'
'Replace       To find a particular string and replace it.
'
'Reverse       To reverse a string.

Public Function InStrRev(ByVal sIn As String, ByVal _
   sFind As String, Optional nStart As Long = 1, _
    Optional bCompare As VbCompareMethod = vbBinaryCompare) _
    As Long

    Dim nPos As Long
   
    sIn = Reverse(sIn)
    sFind = Reverse(sFind)
   
    nPos = InStr(nStart, sIn, sFind, bCompare)
    If nPos = 0 Then
        InStrRev = 0
    Else
        InStrRev = Len(sIn) - nPos - Len(sFind) + 2
    End If
End Function

Public Function Join(Source() As String, _
    Optional sDelim As String = " ") As String

    Dim nC As Long
    Dim sOut As String
   
    For nC = LBound(Source) To UBound(Source) - 1
        sOut = sOut & Source(nC) & sDelim
    Next
   
    Join = sOut & Source(nC)
End Function

Public Function Replace(ByVal sIn As String, ByVal sFind As _
    String, ByVal sReplace As String, Optional nStart As _
     Long = 1, Optional nCount As Long = -1, _
     Optional bCompare As VbCompareMethod = vbBinaryCompare) As _
     String

    Dim nC As Long, nPos As Long
    Dim nFindLen As Long, nReplaceLen As Long

    nFindLen = Len(sFind)
    nReplaceLen = Len(sReplace)
   
    If (sFind <> "") And (sFind <> sReplace) Then
        nPos = InStr(nStart, sIn, sFind, bCompare)
        Do While nPos
            nC = nC + 1
            sIn = Left(sIn, nPos - 1) & sReplace & _
             Mid(sIn, nPos + nFindLen)
            If nCount <> -1 And nC >= nCount Then Exit Do
            nPos = InStr(nPos + nReplaceLen, sIn, sFind, _
              bCompare)
        Loop
    End If

    Replace = sIn
End Function

Public Function Reverse(ByVal sIn As String) As String
    Dim nC As Long
    Dim sOut As String

    For nC = Len(sIn) To 1 Step -1
        sOut = sOut & Mid(sIn, nC, 1)
    Next nC
   
    Reverse = sOut
End Function

Public Function Split(ByVal sIn As String, _
    Optional sDelim As String = " ", _
    Optional nLimit As Long = -1, _
    Optional bCompare As VbCompareMethod = vbBinaryCompare) _
    As Variant

    Dim nC As Long, nPos As Long, nDelimLen As Long
    Dim sOut() As String
   
    If sDelim <> "" Then
        nDelimLen = Len(sDelim)
        nPos = InStr(1, sIn, sDelim, bCompare)
        Do While nPos
            ReDim Preserve sOut(nC)
            sOut(nC) = Left(sIn, nPos - 1)
            sIn = Mid(sIn, nPos + nDelimLen)
            nC = nC + 1
            If nLimit <> -1 And nC >= nLimit Then Exit Do
            nPos = InStr(1, sIn, sDelim, bCompare)
        Loop
    End If

    ReDim Preserve sOut(nC)
    sOut(nC) = sIn

    Split = sOut
End Function

 

by: nhegerPosted on 2004-08-24 at 13:02:52ID: 11886095

I'm sorry but this still makes no sense to me:
Where in this code do I put the variable text strings I'm searching for?
Where in this code do I put the variable web addresses which link to the text strings.

e.g.,
If text "A" is found, it will link INSERTPICTURE field to website "Y" and display the picture.

Some may link to the same picture, ie, text "A" "B" and "C" all link to website "X"
No one text string will link to more than one picture though.  

Just tell me where exactly to put the search text string variables and the website variables, please.  

Thanks for your help!

 

by: GrahamSkanPosted on 2004-08-24 at 14:28:55ID: 11887180

Hi nheger,
I think that we we may be talking at cross-purposes here.

The macro that you described as nearly suiting your purpose will (under Word2000 &+) search a document for a text of a particular format that contains the name of a picture file to insert at that point. The picture file will be found in a given folder.

The text strings are inserted in the document, not the code.

There is no provision in the macro to get text via a URL, only a local folder. It may work. I don't know. You say that it is your intranet, so I suppose(d) that you can access the folder locally.

If you must use URLs to get to the pictures, perhaps you could instance a URL folder that we can test it with.

If not, perhaps we should start again from scratch. You can request, in the Community Support area, that this question be closed and your points refunded.

If you can further clarify your requirements, I am still willing to carry on working on this open question, but you should be aware that it is slipping down the list of open questions and I will probably be the only respondent.

All the best, Graham

 

by: Kelly_in_Los_AngelesPosted on 2004-08-24 at 14:51:50ID: 11887358

Like Graham, I am not entirely sure of the precise requirements and parameters here.  I think that I may have re-written the macro that nheger originally referred to in such a way that it will accomplish what nheger is asking.

Since nheger wants to "tell" the code what to look for and what to like to, I wrote the following macro.

Nheger, you will need to change the values that are assigned to "Const nhegers_number_of_searches"
You will also need to "personalize" the values that are assigned to the array named nhegers_list()

You will notice that the array values come in pairs.  Hopefully it will make sense to you when you look below.  Currently, I have only included four pairs.  If you have 10 things to search for, then you need to add more pairs.  Just follow the pattern below.

Everything from the "For x" line on down should not require alteration.

There are TWO CAVEATS.  However, look at the code first, and then read my further comments below

Sub insPic()

Dim x

Const nhegers_number_of_searches = 3 'this allows for FOUR, because we start at ZERO

Dim nhegers_list(1, nhegers_number_of_searches)

nhegers_list(0, 0) = "Flammable " & Chr(150) & " Canada.bmp"
nhegers_list(1, 0) = "C:\\Folder of Pics\\Flammable - Canada.bmp"

nhegers_list(0, 1) = "Search For This 2"
nhegers_list(1, 1) = "Replace With File Path"

nhegers_list(0, 2) = "Search For This 3"
nhegers_list(1, 2) = "Replace With File Path"

nhegers_list(0, 3) = "Search For This 4"
nhegers_list(1, 3) = "Replace With File Path"


For x = 0 To nhegers_number_of_searches

Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting

With Selection.Find
.Text = nhegers_list(0, x)
.Forward = True
.Wrap = wdFindContinue
End With

Do While Selection.Find.Execute

Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="INCLUDEPICTURE  " & """" & nhegers_list(1, x) & """"

Loop

Next

End Sub

THE TWO CAVEATS:

1. Note that paths (such as "C:\\Folder of Pics\\Flammable - Canada.bmp") must use "double slashes"

2. The spaces in your file names are not a problem, but the HYPHENS are potentially a very tricky problem.  If the hyphens show up IN THE WORD DOCUMENT TEXT as an "en dash" (Chr(150)) or an "em dash" (Chr(151)) rather than a hyphen, this can severly foul up your FIND operation.

Anyway, see what you think and then post back any questions for me.

 

by: Kelly_in_Los_AngelesPosted on 2004-08-24 at 14:53:28ID: 11887370

I just noticed my own typo.  Where I wrote "what to like to," I meant to say "what to link to"

 

by: GrahamSkanPosted on 2004-08-24 at 15:04:16ID: 11887460

I've just now decided to start a campaign to ask all members to put more info in their profiles.

It is a big help to know your time-zone, Kelly_in_Los_Angeles.

Graham in London

 

by: Kelly_in_Los_AngelesPosted on 2004-08-24 at 15:06:09ID: 11887479

Sorry, I just subscribed.

I'll go do my profile right away.

Thanks!!

 

by: Kelly_in_Los_AngelesPosted on 2004-08-24 at 15:07:20ID: 11887484

Oh, wait.

Are you just teasing me about my handle?  (hee hee)

It's just that "Kelly" is so common I have to add something to it

 

by: GrahamSkanPosted on 2004-08-24 at 23:47:13ID: 11889650

Kelly
You don't have to apologise. I was actually thinking that your handle was as good as putting the information in your profile. I only have one language and I'm not too good with that.
Cheers, Graham

 

by: nhegerPosted on 2004-08-25 at 08:14:49ID: 11893377

Kelly,

Thank you...this is what I was looking for!  It is quite easy to edit and for novice users (such as myself) to edit the macro to my needs.

One of my major concerns (which I thought I could fix myself) is not working correctly through this macro though.

I added in a line of code underneath the INCLUDEPICTURE part to add in a carriage return and to format the picture to the correct dimensions:

Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, Text:="INCLUDEPICTURE " & """" & nhegers_list(1, x) & """"
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        Selection.InlineShapes(1).Height = 48.25
        Selection.InlineShapes(1).Width = 48.95
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeParagraph
        Selection.TypeParagraph

Since there are about 12 images which are placed in the document, they all need to be the same size.  For some reason though, it only formats some of them to this size, and leaves others their original size.  And then if I paste my text in again, and re-run the code, different ones are affected by the size change.  I don't understand why it does this?  Perhaps there's a better place to put the re-size picture command in the code?  Any thoughts?

Thanks for all your help!

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 10:00:12ID: 11894723

Hi nheger!

I just woke up this morning, so as soon as I have some coffee I'll play around with image formatting and see what I can come up with.

I'm pretty sure the problem lies in referring to the shape by "index 1" (meaning the part about InlineShapes(1))

The best way would be if we could assign a user-defined range to each shape AS WE INSERT IT. (This is what I'll work on after coffee)

But the "easy" way might be to keep adding "+1" to the index (InlineShapes(1), InlineShapes(2))
I wonder if it would work to keep your code the way it is and then just put "InlineShapes(x+1).Height" and "InlineShapes(x+1).Width"  (I'm suggesting x+1 because x starts at 0)

oh!  one thing I should ask for sure:
          IMPORTANT QUESTION:  Are the pictures that are getting inserted **by the macro** the ONLY pictures in the document???   And a corollary-type question: whether or not the "macro pictures" are the only ones in the document... do you want ALL PICTURES in the ENTIRE document to recieve the same formatting?

If so, you could go all the way down to the end of the macro (just directly before the "End Sub") and add this:

Dim aPic As InlineShape

For Each aPic In ActiveDocument.InlineShapes

aPic.Height = 48.25
aPic.Width = 48.95
aPic.Select
Selection.InsertAfter Chr(13)   'note: Chr(13) is a carriage return or 'new paragraph'
Selection.InsertAfter Chr(13)

Next aPic

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 10:04:25ID: 11894769

Gee!  I said I'd get my coffee before answering, and now that I think about it, that little "aPic" solution just might do the trick and I actually wrote it BEFORE coffee. <laughing>

Oh! I just wanted to clarify...

Obviously, If you add the "For Each aPic" loop to the very end of the macro, then you need to DELETE the following:

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
        Selection.InlineShapes(1).Height = 48.25
        Selection.InlineShapes(1).Width = 48.95
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.TypeParagraph
        Selection.TypeParagraph

The above stuff is duplicated in the aPic loop, so it wouldn't make sense to leave it in.

 

by: nhegerPosted on 2004-08-25 at 10:10:03ID: 11894841

Hi Kelly,

Unfortunately, there are quite a few other pictures (of varying sizes) in the document .  I had already considered a macro to format all the pictures to the same size, but it turned out that some of the images became unclear after resizing.  

Maybe we will have to make a user-defined size for each picture?  What do you suggest?

Thanks,
Nick

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 10:15:32ID: 11894913

Darn!!

It's okay, I think we can still swing it.

In that case, you were totally correct to place the formatting code where you put it.

Okay...  let me play around some more....

 

by: nhegerPosted on 2004-08-25 at 10:20:31ID: 11894966

Hi Kelly,

I just talked to IT - seems that some of my icon pictures just have different resolution values, which are causing them to appear in different sizes on the documents, but appear fine on the intranet preview page.  Let me fix these so they're all the same values, see if this fixes the problem and let you know if I need further help.  Should only be another hour from now or so.  Thanks again! :)

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 10:29:13ID: 11895059

Hmm...  okay, I have toyed with this and I think I may have fixed it.

By the way (as you will surely notice below)...  I spoke too soon when I said I thought the error was "InlineShapes(1)"

Obviously, I realize now that you were referencing the first shape in the selected range, not the first shape in the document each time.  Forgive me for not seeing the wisdom of your ways.

I'm nervous about this, because I'm so afraid you may get the same bad result as with your previous code, but what the heck.

Please give this a shot, and let me know....   (I'm just re-pasting the entire macro)

Sub insPic()

Dim x

Const nhegers_number_of_searches = 3 'this allows for FOUR, because we start at ZERO

Dim nhegers_list(1, nhegers_number_of_searches)

nhegers_list(0, 0) = "Flammable " & Chr(150) & " Canada.bmp"
nhegers_list(1, 0) = "C:\\Folder of Pics\\Flammable - Canada.bmp"

nhegers_list(0, 1) = "Search For This 2"
nhegers_list(1, 1) = "Replace With File Path"

nhegers_list(0, 2) = "Search For This 3"
nhegers_list(1, 2) = "Replace With File Path"

nhegers_list(0, 3) = "Search For This 4"
nhegers_list(1, 3) = "Replace With File Path"


For x = 0 To nhegers_number_of_searches

Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting

With Selection.Find
.Text = nhegers_list(0, x)
.Forward = True
.Wrap = wdFindContinue
End With

Do While Selection.Find.Execute

Dim myField As Field

Set myField = Selection.Fields.Add(Range:=Selection.Range, Type:=wdFieldEmpty, Text:="INCLUDEPICTURE  " & """" & nhegers_list(1, x) & """")

myField.Select
 
Selection.InlineShapes(1).Height = 48.25
Selection.InlineShapes(1).Width = 48.95
Selection.InsertAfter Chr(13)
Selection.InsertAfter Chr(13)

Loop

Next

End Sub

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 10:31:31ID: 11895086

Hi Nick!

I just read your message about the different resolution values.

Okay, I'll wait and see.  I'm crossing my fingers!

 

by: nhegerPosted on 2004-08-25 at 10:51:38ID: 11895320

Hi Kelly,

IT just updated my pictures.  Now all the pictures are the same size, but too big.  Is there a way to alter the "myfield" code you used above and resize them to 50% of their original size?

Thanks again!

 

by: nhegerPosted on 2004-08-25 at 10:56:04ID: 11895376

Oh wait...nevermind..it works!  (user error, of course!)

Thank you so VERY much!  Now, all I have to do is adapt this for some other cases...hmm - we'll see how that goes ;)

Thanks again!
-Nick

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 11:08:37ID: 11895517

You're very welcome.

Glad to be of service.

 

by: nhegerPosted on 2004-08-25 at 11:13:38ID: 11895565

Ugh...ok..now it's not working again?  I'm not sure why?  I even reduced the height and width numbers to 24?
When the run the macro the first time, it does not work.  Second time, only some work.  Third, maybe fourth, it works correctly.  Why is this?

Any ideas?

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 11:21:41ID: 11895635


When you say you run it 2 or 3 or more times...  how can you do that?  after one time, aren't all the text strings replaced with pictures so that there is nothing to find and therefore nothing to replace nothing to insert and nothing to then format (cuz it was never inserted and selected)?

I'm confused.

 

by: nhegerPosted on 2004-08-25 at 11:41:51ID: 11895820

OH, I repaste the text - i dont run the macro over and over.

 

by: nhegerPosted on 2004-08-25 at 11:43:13ID: 11895833

I think I can actually live with it - it's not the end of the world.

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 11:48:46ID: 11895884


Do you know how to "step through" a macro?

you should step through it to see what's going on.

the weird thing is that it is apparently running without generating any error messages, right?  and yet it doesn't work.

I'm willing to keep brainstorming if you need me.

Otherwise, if you're totally sick of this thing, then I'll see you again sometime on another question!

 

by: nhegerPosted on 2004-08-25 at 12:08:10ID: 11896119

Ok.  I'll try the step through thing and let you know what I find ;)

 

by: nhegerPosted on 2004-08-25 at 12:16:11ID: 11896236

When I step through it, everything works as it is supposed to.  When running the full macro, it doesn't always work right.  It's as if it's loading the pictures too fast.  Could it have anything to do with my work's intranet?  

Or, is there a way to assign an bookmark or unique Dim to each picture and then resize them all as the last step?

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 12:58:08ID: 11896697

That's a good idea.

You know what...  you could even run them as TWO SEPARATE MACROS.  See what I have below, and I think you'll know what I mean.  If you divided the following code into two, then in the first macro, the find and replace could occur.  Then when that macro ended you could verify that all the pictures had finished loading or whatever.  Then run the second macro that cycles through the bookmarks.


Sub insPic()

Dim x

Const nhegers_number_of_searches = 3 'this allows for FOUR, because we start at ZERO

Dim nhegers_list(1, nhegers_number_of_searches)

nhegers_list(0, 0) = "Flammable " & Chr(150) & " Canada.bmp"
nhegers_list(1, 0) = "C:\\Folder of Pics\\Flammable - Canada.bmp"

nhegers_list(0, 1) = "Search For This 2"
nhegers_list(1, 1) = "Replace With File Path"

nhegers_list(0, 2) = "Search For This 3"
nhegers_list(1, 2) = "Replace With File Path"

nhegers_list(0, 3) = "Search For This 4"
nhegers_list(1, 3) = "Replace With File Path"


For x = 0 To nhegers_number_of_searches

Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting

With Selection.Find
.Text = nhegers_list(0, x)
.Forward = True
.Wrap = wdFindContinue
End With

Do While Selection.Find.Execute

Dim myField As Field

Set myField = Selection.Fields.Add(Range:=Selection.Range, Type:=wdFieldEmpty, Text:="INCLUDEPICTURE  " & """" & nhegers_list(1, x) & """")

ActiveDocument.Bookmarks.Add "nhegerpictures" & x, myField.Code

Loop

Next

'**** possible division point would be right here.  you could start a new macro *********

'**** if you divided this into two macros, you would have to make sure to match up nhegers_number_of_searches

For y = 0 to nhegers_number_of_searches

ActiveDocument.Bookmarks("nhegerpictures" & y).Select

Selection.MoveRight wdCharacter, 1, True

Selection.InlineShapes(1).Height = 48.25
Selection.InlineShapes(1).Width = 48.95
Selection.InsertAfter Chr(13)
Selection.InsertAfter Chr(13)
ActiveDocument.Bookmarks("nhegerpictures" & y).Delete


Next y

End Sub

 

by: nhegerPosted on 2004-08-25 at 13:21:33ID: 11896945

Hi Kelly,

I talked with a few people and they are fine with leaving the macro as it is (as there are no more than 3 of the pictures from the strings inserted into the document at any one point).  Also, I actually removed the manual carriage returns as it will save paper having the pictures appear next to each other.  If they need them to be substantially smaller, I'll give this a try, but I'll just leave it for now.  

Thanks!

 

by: GrahamSkanPosted on 2004-08-25 at 13:25:01ID: 11896985

Hi Nick,
I think you can increase the points at any time. I suggest that you do this just before accepting Kelly's final answer, whenever it comes. She's not let go of this one.
Cheers, Graham
 

 

by: GrahamSkanPosted on 2004-08-25 at 13:27:30ID: 11897013

Oops. It's already accepted. Not to worry.

 

by: Kelly_in_Los_AngelesPosted on 2004-08-25 at 13:31:05ID: 11897049

You're very sweet, Graham.

Nick did accept my answer (above).

I tend to get hooked on these things.  I'm learning, too, so it's for my own benefit as well.

As a further demonstration of my obsession, I was already thinking about how this could be done with OLE or some other method than the INCLUDEPICTURE fields.

Just for my own future needs.  You never know!

I'm not working today, and actually I'm about to go to the beach!  (it's a beautiful day in these parts)

So I'm rewarding myself <grin>

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...