Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-23-2015, 03:23 AM
tanzinim tanzinim is offline ord vba replace the variable text with variable images to make offer products with images Windows 7 64bit ord vba replace the variable text with variable images to make offer products with images Office 2007
Novice
ord vba replace the variable text with variable images to make offer products with images
 
Join Date: Dec 2015
Posts: 3
tanzinim is on a distinguished road
Post ord vba replace the variable text with variable images to make offer products with images

Good morning
sorry but my knowledge of VBA is limited.

I have to make offers and bills with a word document RTF (exported from Access 2007) with the codes that need to be replaced with images (photos).
The word sheet looks like this:

FOTO_Eclipse

FOTO_FORMA3

FOTO_Eclipse

FOTO_Eclipse

FOTO_FORMA3

FOTO_ALFA


In practice, the VBA code is to be found throughout the word document the string "FOTO_" read the adjacent code (eg Eclipse) and replace it with a photo that I find in a directory on the server.

The code I've written so far is as follows but I only work for the first code and ends without replacing all the others ... please help me where is the error?
Thanks Marco

Sub Replace_String_ImageNUOVO ()

Dim Message As String
As String Dim Title
As String Dim Default
As String Dim usertext
As String Dim PixPathandName
As Range Dim CurrentRange

PixPathandName = "J: \ APPS \ ACCESS \ MDB2007 \ FOTOxOFFERTE \"

September CurrentRange = Selection.Range

Selection.HomeKey wdStory

Selection.Find.ClearFormatting
With Selection.Find
.text = "FOTO_"
.Replacement.Text = ""
.forward = True
.Wrap = WdFindAsk
.format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Selection.Find.Execute
Selection.MoveRight Unit: = wdCharacter, Count: = 1


Selection.MoveRight Unit: = wdWord, Count: = 1, Extend: = wdExtend
Usertext = "FOTO_" & Selection.Range
With Selection.Find
.forward = True
.Wrap = WdFindContinue
Do While .Execute (FindText: = usertext) = True
Selection.InlineShapes.AddPicture FileName: = PixPathandName & Selection.Range & ".JPG", _
LinkToFile: = False, SaveWithDocument: = True
Loop
End With

CurrentRange.Select
End With
End Sub
Reply With Quote
  #2  
Old 12-23-2015, 04:35 AM
gmaxey gmaxey is offline ord vba replace the variable text with variable images to make offer products with images Windows 7 32bit ord vba replace the variable text with variable images to make offer products with images Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

The code you have posted won't even compile so it won't do anything. What does
September CurrrentRange supposed to mean?

Even after you got you declarations straightened out the fundamental error in your ways is using the selection object. You use the selection object to find something, find it and then redefine the selection. It is therefore impossible to find anything else in your very limited selection.

Learn to use the range object which leaves the selection alone:

Code:
Sub Replace_String_ImageNUOVO()
'A basic Word macro coded by Greg Maxey
Dim PixPathandName As String
Dim oRng As Range
  PixPathandName = "J:\APPS\ACCESS\MDB2007\FOTOxOFFERTE\"
  Set oRng = Selection.Range
  With oRng.Find
    .Text = "FOTO_"
    Do While .Execute()
      oRng.MoveEnd wdWord, 1
      Selection.InlineShapes.AddPicture FileName:=PixPathandName & Trim(oRng.Text) & ".JPG", _
      LinkToFile:=False, SaveWithDocument:=True
      oRng.Collapse wdCollapseEnd
     Loop
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 12-25-2015, 05:09 AM
tanzinim tanzinim is offline ord vba replace the variable text with variable images to make offer products with images Windows 7 64bit ord vba replace the variable text with variable images to make offer products with images Office 2007
Novice
ord vba replace the variable text with variable images to make offer products with images
 
Join Date: Dec 2015
Posts: 3
tanzinim is on a distinguished road
Default

First of all thank you very much but i'm sorry i'm not so expert in vba .
I tried to learn the range object but no results.
The code you post work fine but I find some problem using it.

The macro would replace the variable string text (eg FOTO_Eclipse or FOTO_FORMA3) with the picture with the same name in the same position not at the beginning of the word file

Sometimes the file picture inside the directory "J: \ APPS \ ACCESS \ MDB2007 \ FOTOxOFFERTE \" doesn't exist but I should the code continue until the end to replace all the strings text that have the same names of pictures without stopping.

Is it possible to do it ?
Please help me if you can.
Thank you very much Marco






Quote:
Originally Posted by gmaxey View Post
The code you have posted won't even compile so it won't do anything. What does
September CurrrentRange supposed to mean?

Even after you got you declarations straightened out the fundamental error in your ways is using the selection object. You use the selection object to find something, find it and then redefine the selection. It is therefore impossible to find anything else in your very limited selection.

Learn to use the range object which leaves the selection alone:

Code:
Sub Replace_String_ImageNUOVO()
'A basic Word macro coded by Greg Maxey
Dim PixPathandName As String
Dim oRng As Range
  PixPathandName = "J:\APPS\ACCESS\MDB2007\FOTOxOFFERTE\"
  Set oRng = Selection.Range
  With oRng.Find
    .Text = "FOTO_"
    Do While .Execute()
      oRng.MoveEnd wdWord, 1
      Selection.InlineShapes.AddPicture FileName:=PixPathandName & Trim(oRng.Text) & ".JPG", _
      LinkToFile:=False, SaveWithDocument:=True
      oRng.Collapse wdCollapseEnd
     Loop
  End With
lbl_Exit:
  Exit Sub
End Sub
Reply With Quote
  #4  
Old 12-25-2015, 05:35 AM
gmaxey gmaxey is offline ord vba replace the variable text with variable images to make offer products with images Windows 7 32bit ord vba replace the variable text with variable images to make offer products with images Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Use an error handler:

Code:
oRng.MoveEnd wdWord, 1
On Error Resume Next
Selection.InlineShapes.AddPicture FileName:=PixPathandName & Trim(oRng.Text) & ".JPG", LinkToFile:=False, SaveWithDocument:=True
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 12-30-2015, 01:40 PM
tanzinim tanzinim is offline ord vba replace the variable text with variable images to make offer products with images Windows 7 64bit ord vba replace the variable text with variable images to make offer products with images Office 2007
Novice
ord vba replace the variable text with variable images to make offer products with images
 
Join Date: Dec 2015
Posts: 3
tanzinim is on a distinguished road
Default

Gmaxey thank you very much I worked around and finally I solved thanks to your help ....


Quote:
Originally Posted by gmaxey View Post
Use an error handler:

Code:
oRng.MoveEnd wdWord, 1
On Error Resume Next
Selection.InlineShapes.AddPicture FileName:=PixPathandName & Trim(oRng.Text) & ".JPG", LinkToFile:=False, SaveWithDocument:=True
Reply With Quote
Reply

Tags
images, text



Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Variable or With block variable not set Raza Excel Programming 8 01-15-2015 12:19 AM
Run Time Error '91': Object variable or With block variable not set using Catalogue Mailmerge Berryblue Mail Merge 1 11-13-2014 05:36 PM
ord vba replace the variable text with variable images to make offer products with images Run-time error 91 object variable or with block variable not set JUST ME Word VBA 4 03-25-2014 06:56 AM
ord vba replace the variable text with variable images to make offer products with images object variable or with block variable not set MJP143 Excel 1 02-11-2013 05:07 AM
ord vba replace the variable text with variable images to make offer products with images Run-time error '91': Object variable or With block variable not set tinfanide Excel Programming 2 06-10-2012 10:17 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:56 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft