Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-16-2013, 06:13 AM
rcVBA rcVBA is offline check for duplicate before appending to new file Windows 7 64bit check for duplicate before appending to new file Office 2003
Novice
check for duplicate before appending to new file
 
Join Date: May 2013
Posts: 6
rcVBA is on a distinguished road
Default check for duplicate before appending to new file


I have the code below which searches through an html file and extracts the names of all the images in the html code. The image names are written to an xml file and each image name has a set of xml tags added with it.
The problem is that I don't want to add an image name to the xml file if it has been added previously. I don't know if I can search the xml file or if I need a separate file that is an array or a collection, or . . ??

Code:
Function GetRegExResult(strInputData As String) As String
Dim Match, Matches
Dim RegExp As Object
Dim strMatchesFile As String 
Dim sRegPath As String
Dim sRegImageFile As String

sRegPath = ActiveDocument.Path
sRegImageFile = sRegPath + "\ImageNames.txt"

    strMatchesFile = sRegImageFile
    Set RegExp = CreateObject("VBScript.RegExp")
    With RegExp
        .Global = True
        .IgnoreCase = True
        .Pattern = "<img\s*src=""([^""]*)"""
    End With

    Set Matches = RegExp.Execute(strInputData) ' Execute search.
    For Each Match In Matches
Dim strMatchOrig As String
Dim intMatchLength As Integer
intMatchLength = Len(Match)
intMatchLength = intMatchLength - 11
strMatchOrig = Match
strMatchOrig = Mid(strMatchOrig, 11, intMatchLength)
        hFile = FreeFile
        Open strMatchesFile For Append As #hFile
Print #hFile, "<Uses>" + vbNewLine _
+ "<FileName>" + strMatchOrig + "<" + Chr(47) + "FileName>" + vbNewLine _
+ "<MD5>456789<" + Chr(47) + "MD5>" + vbNewLine _
+ "<" + Chr(47) + "Uses>"
        Close #hFile
    Next
End Function
Reply With Quote
  #2  
Old 05-16-2013, 08:29 AM
macropod's Avatar
macropod macropod is offline check for duplicate before appending to new file Windows 7 32bit check for duplicate before appending to new file Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,374
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

You can easily-enough use the Instr function to test whether the xml file contains your image name. If it returns anything greater than 0, the image name is already there.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-16-2013, 09:34 AM
rcVBA rcVBA is offline check for duplicate before appending to new file Windows 7 64bit check for duplicate before appending to new file Office 2003
Novice
check for duplicate before appending to new file
 
Join Date: May 2013
Posts: 6
rcVBA is on a distinguished road
Default

strMatchesFile is the file that the image names are being stored in. I am trying to use InStr like this:
Code:
myNoDupes = InStr(strMatchOrig, strMatchesFile)
but strMatchesFile is searched as the file path and name, how can I search the contents of strMatchesFile? I can do the Instr at the point where strMatchesFile is open for Append As #hFile
I'm having some trouble getting the content of the file instead of the file path/name
Reply With Quote
  #4  
Old 05-16-2013, 02:56 PM
macropod's Avatar
macropod macropod is offline check for duplicate before appending to new file Windows 7 32bit check for duplicate before appending to new file Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,374
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

No, that's not how you would use Instr. You have to open both file before you can test the xml file's contents with Instr. For example, to test each image from your "ImageNames.txt" file:
Code:
Dim I As Long
With #hFile
  For I = 0 To UBound(Split(sRegImageFile, vbCr))
    If InStr(.Text, Split(sRegImageFile, vbCr)(I)) = 0 Then
      'The image name isn't present, so add it.
      'Your code to add the image goes here
    End If
  Next
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-21-2013, 11:40 AM
rcVBA rcVBA is offline check for duplicate before appending to new file Windows 7 64bit check for duplicate before appending to new file Office 2003
Novice
check for duplicate before appending to new file
 
Join Date: May 2013
Posts: 6
rcVBA is on a distinguished road
Default

Thank you very much, that worked perfect. It took me awhile to work it into the code but now it is all working great.
Thanks again.
Reply With Quote
Reply

Tags
vba word



Similar Threads
Thread Thread Starter Forum Replies Last Post
check for duplicate before appending to new file cannot check/uncheck check box but added check box learn2office Word 1 11-27-2012 02:02 AM
check for duplicate before appending to new file Appending the filename with the current date sheeand Word VBA 2 05-14-2012 05:18 AM
check for duplicate before appending to new file Word Macro That Checks a Check Box Form Field When File Print is Executed DKerne Word VBA 4 06-09-2011 11:54 AM
Appending Word Docs Frank Word 0 02-26-2010 04:38 PM
Saving Word document creates a duplicate transparent file bbailey Word 0 12-05-2009 10:54 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:18 PM.


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