I am trying to overwrite specific lines in a sequential file
For example if file has:
"1"
"Kii"
"Kii"
"Kii"
"Kii"
"2"
"Troy Martinez"
"Edoy"
"Edoy"
"69"
"3"
"Snoop Dogg"
"Weed"
"President Troy"
"420"
And I have this code to overwrite
Private Sub OverWrite()
Dim Count As Integer
On Error GoTo ErrSub
LineCount = 1
Open App.Path & "\Data.txt" For Input As #1
Do While Not EOF(1)
If LineCount < ((IDCount - 1) * 5) + 1 Or LineCount >= (IDCount * 5) + 1 Then
For Count = 0 To 4
Input #1, TextTemp
FileText = FileText & """ & Text1(Count) & """ & vbCrLf
LineCount = LineCount + 1
Next Count
Else
For Count = 0 To 4
Input #1, TextTemp
FileText = FileText & """ & TextTemp & """ & vbCrLf
LineCount = LineCount + 1
Next Count
End If
Loop
Close
Open App.Path & "\Data.txt" For Output As #1
Print #1, FileText
Close
ErrSub:
Resume Next
End Sub
Assuming my IDCount is 2, the text file becomes
" & Text1(Count) & "
" & Text1(Count) & "
" & Text1(Count) & "
" & Text1(Count) & "
" & Text1(Count) & "
" & TextTemp & "
" & TextTemp & "
" & TextTemp & "
" & TextTemp & "
" & TextTemp & "
" & Text1(Count) & "
" & Text1(Count) & "
" & Text1(Count) & "
" & Text1(Count) & "
" & Text1(Count) & "
How do I fix this? Thanks
I am very noob
Edit: My Option Explicit Is
Option Explicit
Dim IDCount As Integer
Dim LineCount As Integer
Dim FileText As String
Dim TextTemp
4 quotes ("""") escape a single quote (") so:
FileText = FileText & """" & Text1(Count) & """" & vbCrLf
(chr$(34) also outputs a ")
Related
I am running the below vbscript on outlook 2013/2016 and having issues trying to read emails in sub folders off the Inbox. I can read the Inbox emails. . Can anyone point me in the right directions?
thanks in advance.
Function CheckMail(strMailBox,strFolder,strFolderAbbr,strDetails)
'
olFolderInbox = 6
set Session = CreateObject("Redemption.RDOSession")
'
Set objOutlook = CreateObject("Outlook.Application")
Session.MAPIOBJECT = objOutlook.Session.MAPIOBJECT
set Store = Session.Stores.GetSharedMailbox(strMailBox)
set Inbox = Store.GetDefaultFolder(olFolderInbox)
'
Wscript.Echo "MailBox: " & Store.Name & " - " & Inbox.Name
If strFolder = "" then
set SubFolder = Inbox
strFolderAbbr = strMailBox & " Inbox"
Else
'set SubFolder = Inbox.Folders(strFolder)
'
set SubFolder = Inbox.Folders.Item(strFolder)
'
strFolderAbbr = strMailBox & " Inbox\" & strFolder
Wscript.Echo " Sub Folder: " & SubFolder
End If
'
nItems = SubFolder.Items.Count
If nHowlong > 1 Then
nHowlong = Round((nItems/110)/60,0)
strTime = " Hour(s)!!"
Else
nHowlong = Round(nItems/110,0)
strTime = " Minute(s)!!"
End If
Wscript.Echo nItems & " - Emails in folder " & strFolderAbbr & " About " & nHowlong & strTime
'" - " & nItems
'
for each Msg in SubFolder.Items
nCounter = nCounter + 1
'Wscript.Echo "Item " & nCounter & "/" & nItems & vbCRLF & "EID: " & Msg.EntryID & vbCRLF & "ABOUT: " & Msg.Subject & vbCRLF & "FROM: " & Msg.SenderName & vbCRLF & "LEVEL: " & IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & vbCRLF & "Status: " & IIf(Msg.UnRead, "Not Read", "Read") & vbCRLF & "Received: " & Msg.ReceivedTime & vbCRLF & "Body: " & Msg.Body
'& nCounter & "/" & nItems & vbCRLF & "EID: " & Msg.EntryID & vbCRLF & "ABOUT: " & Msg.Subject & vbCRLF & "FROM: " & Msg.SenderName & vbCRLF & "LEVEL: " & IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & vbCRLF & "Status: " & IIf(Msg.UnRead, "Not Read", "Read") & vbCRLF & "Received: " & Msg.ReceivedTime & vbCRLF & "Body: " & Msg.Body
'
' process all emails in the box
strRecords = strRecords & "REG-" &strFolderAbbr & "~" & Msg.Subject & "~" & Msg.ReceivedTime & "~" & IIf(Msg.UnRead, "Not Read", "Read") & "~" & Msg.SenderName & "~" &IIf(Msg.Importance=2,"High",IIf(Msg.Importance=1,"Normal","Low")) & "*"
On Error Resume Next
err.clear
if Err Then
'WScript.Echo "ReceivedTime was null"
End If
On Error GoTo 0
next
CheckMail = strRecords
End Function
Function IIf(bClause, sTrue, sFalse)
If CBool(bClause) Then
IIf = sTrue
Else
IIf = sFalse
End If
End Function
I finally figured out the code that works. I would first like to thank Dmitry for all the help he gave me. But this was a stuburn issue. the following code solved the problem. Please dont ask me to explain it just plain ole luck and trail and error.
set Session = CreateObject("Redemption.RDOSession")
Set objOutlook = CreateObject("Outlook.Application")
Session.MAPIOBJECT = objOutlook.Session.MAPIOBJECT
'Set Root foldedr of the mail box of the stores
set IPMRoot = Session.Stores.Item(strMailBox).IPMRootFolder
'Set the subfolder to the inbox
If strFolder = "" then
set subFolder = IPMRoot.Folders("InBox")
strFolderAbbr = strMailBox & " Inbox"
Else
'Set subfolder to subfolder chosen
set subFolder = IPMRoot.Folders("InBox").Folders(strFolder)
strFolderAbbr = strMailBox & " Inbox\" & strFolder
End If
'
I have currently been tasked to put together a script that will change the DNS settings of 15,000 ish servers. However, there is no common unique identifer of these NIC's other than their current DNS IP. My Question, Is it possible to somehow have my script do an ipfonfig /all and then if one of the NIC's reports back with the current DNS settings target that NIC for the new updated settings?
I was currently using the below script until i was made aware that some of the NIC will not be called "Production". Any suggestions are welcome! (powershell was not an option as we may be targeting some very old servers)
Dim strDns1
Dim strDns2
strDns1 = "10.10.10.10"
strDns2 = "10.10.10.10"
Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set dns name=""Production"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Production"" addr="& strDns2, 0, True
Set objShell = Nothing
WScript.Quit
You can do the ipconfig query with a script like this:
Set wso = CreateObject("WScript.Shell")
Set execo = wso.Exec("ipconfig /all")
Set stdout = execo.StdOut
While Not stdout.AtEndOfStream
cmdOutput = cmdOutput & VbCrLf & stdout.ReadLine
Wend
wscript.echo cmdOutput
The main problem is this is just a string, so you have to parse it yourself, which is annoying and probably error prone.
I would rather suggest you take a look at the wmi class
Win32_NetworkAdapterConfiguration
which has a lot of information on the network connections. Only caveat with this WMI class is that it stores many values in arrays instead of strings so you can not just query for everything easily. Either pick something to query against that is a string like DNSDomain or just handle the logic within the script.
I'm agree with Syberdoor , You should use the Wmi Class
Win32_NetworkAdapterConfiguration
This code give you some informations :
Call ListDNSInfo()
'********************************************************************
Sub ListDNSInfo()
Dim ComputerName,IPConfigSet,IPConfig,BailObtenu,BailExpirant
ComputerName="."
On error resume next
set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & ComputerName).ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE")
If Err.Number <> 0 Then
wscript.echo " - non accessible -"
Else
for each IPConfig in IPConfigSet
BailObtenu = IPConfig.DHCPLeaseObtained
BailExpirant = IPConfig.DHCPLeaseExpires
'---- Convertion des date et heure d'obtention et d'expiration des baux DHCP en un format lisible par l'utilisateur. ----
BailObtenu = mid(BailObtenu, 7, 2) & "/" & mid(BailObtenu, 5, 2) & "/" & mid(BailObtenu, 1, 4) & " - " & mid(BailObtenu, 9, 2)& ":" & mid(BailObtenu, 11, 2)& ":" & mid(BailObtenu, 13, 2)
BailExpirant = mid(BailExpirant, 7, 2) & "/" & mid(BailExpirant, 5, 2) & "/" & mid(BailExpirant, 1, 4) & " - " & mid(BailExpirant, 9, 2)& ":" & mid(BailExpirant, 11, 2)& ":" & mid(BailExpirant, 13, 2)
MsgBox " Configuration réseau de l'ordinateur " & ComputerName & vbcrlf & vbcrlf & _
"Nom Machine " & vbtab & " : " & IPConfig.DNSHostName & vbcrlf & _
"Carte active" & vbtab & " : " & IPConfig.Description & vbcrlf & _
"Adresse MAC " & vbtab & " : " & IPConfig.MACAddress & vbcrlf & _
"DHCP Activé" & vbtab & " : " & IPConfig.DHCPEnabled & vbcrlf & _
"Adresse IP " & vbtab & " : " & IPConfig.IPAddress(0) & vbcrlf & _
"Masque " & vbtab & vbtab & " : " & IPConfig.IPSubnet(0) & vbcrlf & _
"Passerelle " & vbtab & " : " & IPConfig.DefaultIPGateway(0) & vbcrlf & _
"Serveur DHCP " & vbtab & " : " & IPConfig.DHCPServer & vbcrlf & vbcrlf & _
"Serveur DNS " & vbtab & " : " & IPConfig.DNSServerSearchOrder(0) & vbcrlf & _
" " & vbtab & vbtab & " : " & IPConfig.DNSServerSearchOrder(1) & vbcrlf & _
"Serveur WINS " & vbtab & " : " & IPConfig.WINSPrimaryServer(0) & vbcrlf & _
" " & vbtab & vbtab & " : " & IPConfig.WINSSecondaryServer(0) & vbcrlf & vbcrlf & _
" Bail obtenu " & vbtab & " : " & BailObtenu & vbcrlf & _
" Bail expirant " & vbtab & " : " & BailExpirant _
,VbInformation,"Configuration réseau de l'ordinateur "
Next
End If
End Sub
I downloaded a spreadsheet which seems to be originally written for windows. The file is here http://www.automateexcel.com/2004/12/15/create_an_rss_feed_with_excel/.
For line RssLocation = ThisWorkbook.Path & "\" & FeedSheet.Cells(5, 2).Value
I have replaced "\" with with "/user/desktop/rsscreate.xlsm" but I still get the error.
I have also, tried "& Application.PathSeparator &" but still the error.
Any suggestions?
The code for the macro is below.
Sub WriteRss()
Dim X As Long
Dim FeedSheet As Worksheet
Dim RssLocation As String
Dim DomainName As String
Dim DomainDescription As String
Dim DomainTitle As String
'Change the word "Sheet1" to the tabname of
'the sheet where you keep your feeds list
Set FeedSheet = Sheets("Sheet1")
'Your domain name(include the http://)
DomainName = FeedSheet.Cells(2, 2).Value
'Your Site's Title
DomainTitle = FeedSheet.Cells(3, 2).Value
'Your Site's Description
DomainDescription = FeedSheet.Cells(4, 2).Value
'Location to write file, defaults to workbook directory
RssLocation = ThisWorkbook.Path & "\" & FeedSheet.Cells(5, 2).Value
'Kill the file if it already exists
If Len(Dir(RssLocation)) > 0 Then
Kill RssLocation
End If
Open RssLocation For Append As #1
Print #1, "<?xml version=""1.0""" & " encoding=""iso-8859-1""?>"
Print #1, "<rss version=" & """" & "2.0" & """" & _
" xmlns:content = ""http:" & _
"//purl.org/rss/1.0/modules/content/"">"
Print #1, " <channel>"
Print #1, " <title>" & DomainTitle & "</title>"
Print #1, " <link>" & DomainName & "</link>"
Print #1, " <description>" & DomainDescription & "</description>"
Print #1, " <language>en-us</language>"
'Loop through sheet specified
'Start # row 2
'ColumnA=Title, ColumnB=Link, ColumnC=Description
For X = 9 To FeedSheet.Range("A" & FeedSheet.Rows.Count) _
.End(xlUp).Row
Print #1, " <item>"
Print #1, " <title>" & FeedSheet.Cells(X, 1).Value & _
"</title>"
Print #1, " <link>" & FeedSheet.Cells(X, 2).Value & _
"</link>"
Print #1, "<description>"
Print #1, "<![CDATA["
Print #1, FeedSheet.Cells(X, 3).Value
Print #1, " ]]> "
Print #1, "</description>"
Print #1, " </item>"
Next
Print #1, " </channel>"
Print #1, "</rss>"
Close #1
MsgBox "Your new RSS feed can be found here: " & RssLocation
End Sub
Sub InsertNew()
Range("A9:C17").Select
Selection.Cut
Range("A10:C18").Select
ActiveSheet.Paste
Range("A18:C18").Select
Selection.Copy
Range("A9:C9").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
EDIT: adding the following from the Answers section.
I tried the code in previous answer. I'm still getting error at line If Len(Dir(RssLocation)) > 0
Any suggestions on how can use MacId and ThisWorkbook.Path instead of Applcaton.PathSeparator?. Siddharth your thoughts?
Try this (Untested)
RssLocation = ThisWorkbook.Path & "\" & FeedSheet.Cells(5, 2).Value
RssLocation = Replace(RssLocation ,"\", Application.PathSeparator)
'Debug.Print RssLocation
I inherited a VB program that prints shipping labels. We need to update the barcode that gets printed and I'm trying to understand what the existing program is doing. There is a bunch of "WrittenData" (stored as "s") that seems to contain the Postscript code to produce the labels, but I haven't found anywhere that specifies the meaning of half this stuff.
Public Function PostScriptItem(address As Variant, Optional intPageNumberOut As Integer = 1, Optional intPageNumberIn As Integer = 2) As String
' 0 - MatchUpId
' 1 - Our Barcode
' 2 - Outgoing PostNet Code
' 3 - Outgoing Line 1
' 4 - Outgoing Line 2 (optionally EMPTY)
' 5 - Outgoing Line 3
' 6 - Outgoing Line 4
' 7 - Outgoing PlaNET Code
' 8 - Incoming PostNet Code
' 9 - Incoming Line 1
'10 - Incoming Line 2 (optionally EMPTY)
'11 - Incoming Line 3
'12 - Incoming Line 4
'13 - Incoming PlaNET Code
'14 - Title Id
'Escape Parenthesis and Backslashes
Dim tPos As Integer
For tPos = 0 To 14 Step 1
address(tPos) = Replace(address(tPos), "\", "\\")
address(tPos) = Replace(address(tPos), "(", "\(")
address(tPos) = Replace(address(tPos), ")", "\)")
Next tPos
Dim s As String
Dim pos As Integer
Dim fsize As Integer
Dim strBarcodeCaption As String
strBarcodeCaption = address(1) & " " & address(14) & " " & address(0)
pos = 50
fsize = 12
s = s & "%%Page: " & CStr(intPageNumberOut) & " " & CStr(intPageNumberOut) & vbCrLf & _
"<< /Duplex true >> setpagedevice" & vbCrLf & _
"<< /Tumble true >> setpagedevice" & vbCrLf & _
"%%BeginPageSetup" & vbCrLf & _
"180 rotate" & vbCrLf & _
"/pagelevel save def" & vbCrLf & _
"%%EndPageSetup" & vbCrLf & _
"newpath" & vbCrLf & _
"-338 -205 translate" & vbCrLf & _
"/Courier-Bold findfont 6 scalefont setfont" & vbCrLf & _
"newpath" & vbCrLf
s = s & "32 104 moveto (" & strBarcodeCaption & ") show" & vbCrLf & _
"newpath" & vbCrLf
s = s & "12 -450 translate" & vbCrLf & _
"62 104 moveto (" & strBarcodeCaption & ") show" & vbCrLf & _
"newpath" & vbCrLf & _
"30 80 moveto (^104" & address(1) & ") (height=0.3) code128 barcode" & vbCrLf & _
"newpath" & vbCrLf
s = s & "/Helvetica findfont 11 scalefont setfont" & vbCrLf & _
"0 " & CStr(pos) & " moveto (" & address(3) & ") show" & vbCrLf & _
"newpath" & vbCrLf
pos = pos - fsize
If address(4) <> Empty Then
s = s & "0 " & CStr(pos) & " moveto (" & address(4) & ") show" & vbCrLf & _
"newpath" & vbCrLf
pos = pos - fsize
End If
s = s & "0 " & CStr(pos) & " moveto (" & address(5) & ") show" & vbCrLf & _
"newpath" & vbCrLf
pos = pos - fsize
s = s & "0 " & CStr(pos) & " moveto (" & address(6) & ") show" & vbCrLf & _
"newpath" & vbCrLf
pos = pos - fsize - 5
s = s & "1 " & CStr(pos) & " moveto (" & address(2) & ") () postnet barcode" & vbCrLf & _
"%%PageTrailer" & vbCrLf & _
"pagelevel restore" & vbCrLf & _
"showpage" & vbCrLf
s = s & "%%Page: " & CStr(intPageNumberIn) & " " & CStr(intPageNumberIn) & vbCrLf & _
"<< /Duplex true >> setpagedevice" & vbCrLf & _
"<< /Tumble true >> setpagedevice" & vbCrLf & _
"%%BeginPageSetup" & vbCrLf & _
"/pagelevel save def" & vbCrLf & _
"210 711 translate" & vbCrLf & _
"%%EndPageSetup" & vbCrLf & _
"newpath" & vbCrLf & _
"/Courier-Bold findfont 6 scalefont setfont" & vbCrLf & _
"0 21 moveto (" & address(1) & " " & address(0) & ") show" & vbCrLf & _
"gsave" & vbCrLf & _
"0.5 0.5 scale" & vbCrLf & _
"0 12 moveto (^104" & address(1) & ") (height=0.3) code128 barcode" & vbCrLf & _
"grestore" & vbCrLf & _
"newpath" & vbCrLf & _
"32 0 moveto (" & address(14) & ") show" & vbCrLf & _
"newpath" & vbCrLf & _
"/Helvetica findfont 11 scalefont setfont" & vbCrLf & _
"-70 -180 translate" & vbCrLf
pos = 56
s = s & "0 " & CStr(pos) & " moveto (" & address(9) & ") show" & vbCrLf & _
"newpath" & vbCrLf
pos = pos - fsize
If address(10) <> Empty Then
s = s & "0 " & CStr(pos) & " moveto (" & address(10) & ") show" & vbCrLf & _
"newpath" & vbCrLf
pos = pos - fsize
End If
s = s & "0 " & CStr(pos) & " moveto (" & address(11) & ") show" & vbCrLf & _
"newpath" & vbCrLf
pos = pos - fsize
s = s & "0 " & CStr(pos) & " moveto (" & address(12) & ") show" & vbCrLf & _
"newpath" & vbCrLf
pos = pos - fsize - 5
s = s & "1 " & CStr(pos) & " moveto (" & address(8) & ") () postnet barcode" & vbCrLf & _
"%%PageTrailer" & vbCrLf & _
"pagelevel restore" & vbCrLf & _
"showpage" & vbCrLf
PostScriptItem = s
End Function
I need to change the contents of "address(2)" and "address(8)" and use a different font to print those. I tried putting the new font in the projects fonts folder and referencing it the way "postnet" and "code128" are referenced but that left me with an entirely blank label.
The "code128" font seems to get defined in a separate file called "postscript_main.ps" and I have no idea how to incorporate the new font into that, I am really lost and hoping for a clue or a link to some documentation that might get me going in the right direction.
I'm getting an "object required" error on line 54, the last line, when I run the following script. What is wrong?
Option Explicit
Dim cmdString, g_strHostFile, filepath, flexnetpath, importcmd, dtmToday, dtmYesterday, dtmFileDate, param1, param2, param3, i4path, objFSO, objTextStream, g_strComputer, WshShell
'Initialize global constants and variables.
Const FOR_READING = 1
g_strHostFile = "D:\dataimports\LUM_servers.txt"
i4path = "C:\IFOR\WIN\BIN\i4blt.exe"
filepath = "D:\DataImports\"
flexnetpath = "C:\Program Files (x86)\Flexnet\Manager\Admin"
importcmd = flexnetpath & "flexnet bulkimport -uadmin -padmin -f" & filepath
dtmToday = Date()
dtmYesterday = Date() - 1
dtmFileDate = Year(Date) & padDate(Month(Date)) & padDate(Day(Date))
param1 = "-r1 -e2,4 -n "
param2 = " -v 'Dassault Systemes' -b "
param3 = " -g "
WScript.Echo "i4Path: " & i4path
WScript.Echo "FilePath: " & filepath
WScript.Echo "flexnetpath: " & flexnetpath
WScript.Echo "importcmd: " & importcmd
WScript.Echo "dtmToday: " & dtmToday
WScript.Echo "dtmYesterday: " & dtmYesterday
WScript.Echo "dtmFileDate: " & dtmFileDate
'Read LUM Server Names from text file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(g_strHostFile) Then
Set objTextStream = objFSO.OpenTextFile(g_strHostFile, FOR_READING)
Else
WScript.Echo "Input file " & g_strHostFile & " not found."
WScript.Quit
End If
'Loop through list of computers and perform tasks on each.
Do Until objTextStream.AtEndOfStream
g_strComputer = objTextStream.ReadLine
WScript.Echo "Processing Server: " & g_strComputer
Set cmdString = i4path & param1 & g_strComputer & param2 & dtmYesterday & param3 & dtmToday & filepath & g_strComputer & "_" & dtmFileDate & "_lum.lrl"
WScript.Echo "Processing Command: " & cmdString
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmdString"
Loop
objTextStream.Close
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo "Processing Bulk Import: " & importcmd
WshShell.Run "importcmd"
Function padDate(intNumber)
if intNumber <= 9 Then
padDate = "0" & CStr(intNumber)
Else
padDate = CStr(intNumber)
End If
End Function
Object required is raised when you have a statement like Set x = y where x is not an object type, but is instead an simple type (Integer, Double, Date, etc. ). I think the line
Set cmdString = i4path & param1 & g_strComputer & param2 & ...
is causing the error, and I think all you have to do is remove the Set statement. I think strings do not derive from Object and thus do not need the Set statement.
There are a few problems, I think.
importcmd = flexnetpath & "flexnet bulkimport -uadmin -padmin -f" & filepath
You probably need some spaces:
importcmd = flexnetpath & " flexnet bulkimport -uadmin -padmin -f " & filepath
Set is only used with objects, not strings, so it should be removed from this line:
Set cmdString = i4path & param1 & g_strComputer & param2 & dtmYesterday & param3 & dtmToday & filepath & g_strComputer & "_" & dtmFileDate & "_lum.lrl"
I am fairly sure you either mean
WshShell.Run importcmd
Or
WshShell.Run """" & importcmd & """"