Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-01-2016, 11:20 PM
Sinsearach Sinsearach is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Mar 2016
Posts: 2
Sinsearach is on a distinguished road
Question TrueColor BMP “fonts” in word w/ replace text>bmp script (trouble)

Note: Until few days ago I had never touched VBA, let alone any real programming.



I have read through the top several dozen google returns on 'vba replace text with images' and they either cannot be extended to several dozen searches (94 char bmps) or require a dialog box for each or are far too complex for me to think about fiddling with.

Here I will be using my 94 bmps each 32^2 px as my 'font' to substitute for what I type with this script. This is what I've cobbled together (Im only testing with a few chars 1st)

Problem: It only replaces 1 instance of each character.

Code:
Sub InsertImages()
 
With ActiveDocument
  Selection.Find.ClearFormatting
 
  With Selection.Find
    .Forward = True
    .Text = "0"
    .Replacement.Text = ""
    .Format = False
    .MatchCase = False
    .MatchWholeWord = Flase
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  
  If Selection.Find.Execute Then
    Selection.InlineShapes.AddPicture FileName:= _
      "C:\BMP Fonts\Q0.bmp", LinkToFile:=False, _
      SaveWithDocument:=True
 
  ElseIf Selection.Find.Wrap = wdFindContinue Then
  End If
 
  Selection.Find.ClearFormatting
 
  With Selection.Find
    .Forward = True
    .Text = "1"
    .Replacement.Text = ""
    .Format = False
    .MatchCase = False
    .MatchWholeWord = Flase
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
 
  If Selection.Find.Execute Then
    Selection.InlineShapes.AddPicture FileName:= _
      "C:\BMP Fonts\Q1.bmp", LinkToFile:=False, _
      SaveWithDocument:=True
 
  ElseIf Selection.Find.Wrap = wdFindContinue Then
  End If
 
End With
 
End Sub
It works fine except for 2 that issues remain:

1: only replacing 1 instance

2: Sometimes it doesn't even start at the beginning of my document, even with cursor placed there or entire document highlighted (as if it runs "too fast")

1st issue is critical, 2nd is tolerable.

I found this for replacing all text:

Selection.Find.Execute Replace:=wdReplaceAll

but it ONLY works for with substituting text, not images.

Help!

-Laters

Last edited by Sinsearach; 03-02-2016 at 12:57 AM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 03-02-2016, 12:47 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub InsertImages()
Application.ScreenUpdating = False
Dim i As Long
For i = 0 To 9
  With ActiveDocument.Range
    With .Find
      .ClearFormatting
      .Forward = True
      .Text = i
      .Replacement.Text = ""
      .Format = False
      .Wrap = wdFindStop
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute
    End With
    Do While .Find.Found
      .InlineShapes.AddPicture FileName:="C:\BMP Fonts\Q" & i & ".bmp", _
        LinkToFile:=False, SaveWithDocument:=True
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-02-2016, 03:09 AM
Sinsearach Sinsearach is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Mar 2016
Posts: 2
Sinsearach is on a distinguished road
Thumbs up Maddening final defiance of the code.

AWESOME.
And so close now!! spent so many hours on this....
but while your revision checks each instance, it does not remove the original character, it just ADDS my bmps; take a look:
Code:
http://www.imagebam.com/image/105392468865976
Quote:
Originally Posted by macropod View Post
Try:
Code:
Sub InsertImages()
Application.ScreenUpdating = False
Dim i As Long
For i = 0 To 9
  With ActiveDocument.Range
    With .Find
      .ClearFormatting
      .Forward = True
      .Text = i
      .Replacement.Text = ""
      .Format = False
      .Wrap = wdFindStop
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute
    End With
    Do While .Find.Found
      .InlineShapes.AddPicture FileName:="C:\BMP Fonts\Q" & i & ".bmp", _
        LinkToFile:=False, SaveWithDocument:=True
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #4  
Old 03-02-2016, 04:16 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

OK, simply insert:
.Delete
before:
.InlineShapes.AddPicture ...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
font, replace, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Replace fonts? daroga Publisher 1 01-22-2016 09:13 PM
Slow "comparison/replace" script mavCZ Excel Programming 53 07-27-2014 10:57 AM
replace data from variable with "sub and super script" from excel to word by vba krishnaoptif Word VBA 9 06-22-2012 05:08 AM
WORD: Rtf and search-replace (regexp/fonts) seteshpl Word 1 09-06-2011 01:35 AM
Macro to Replace Fonts ballj_35 Word 3 05-10-2011 01:10 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:19 AM.


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