Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake substitutions in a WMF file
DescriptionThis example shows how to make substitutions in a WMF file in Visual Basic 6. It reads the WMF file as text and replaces token strings in the file with useful strings.
KeywordsWMF, Windows Metafile
CategoriesGraphics
 
The program reads the WMF file. It then uses the Substitute subroutine to replace token strings in the WMF file with useful data values. This example fills in a name and address.

The program then writes the revised WMF data into a temporary file and displays the results.

 
Private Sub Command1_Click()
Dim txt As String
Dim fnum As Integer

    fnum = FreeFile
    Open Text1.Text For Binary As fnum
    
    txt = Input(LOF(fnum), #fnum)
    
    Close fnum

    ' Insert the name.
    Substitute txt, _
        "$$nom67890123456789012345678901234567890", _
        "Rod Stephens"

    ' Insert the street.
    Substitute txt, _
        "$$rue6789012345678901234567890", _
        "1234 Programmer Way"

    ' Insert the place, country, and postal code.
    Substitute txt, _
        "$$pays7890123456789012345678901234567890", _
        "Bugville CO 80301, USA"

    ' Save the new WMF file.
    Open "C:\Temp\NewFile.WMF" For Binary As fnum Len = _
        Len(txt)
    Put #fnum, , txt
    Close fnum

    ' Load the WMF file.
    Picture1.Picture = LoadPicture("C:\Temp\NewFile.WMF")
End Sub
 
Subroutine Substitute makes the replacement value the same length as the token. It then replaces the token with the replacement value.
 
' Replace the token with the value in the string.
Private Sub Substitute(txt As String, ByVal token As _
    String, ByVal value As String)
Dim pos As Long

    ' Make sure the value is not too long.
    value = Left$(value, Len(token))

    ' Make the value the same length as the token.
    value = value & Space$(Len(token) - Len(value))
    
    ' Make the replacement.
    pos = InStr(txt, token)
    txt = Left$(txt, pos - 1) & _
        value & _
        Right$(txt, Len(txt) - (pos + Len(token)) + 1)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated