it looks like I'm working on a note document based on the text file that does what I tell it to do.
I would like for example to delete in the text file only what is written between [newsubj]and [endsubj] [newtext] and [endtext], from my textbox.
so each block created is a subject and then the message follows.
the topic will not contain anything to link to, for example "good morning" or "I Know What You Did Last Summer" so we can't create a loop.
[newsubj]
This is a first demonstration
[endsubj]
[newtext]
This is a message
[endtext]
[newsubj]
This is a 2th demonstration
[endsubj]
[newtext]
This is a 2th message
hello
[endtext]
[newsubj]
This is a 3th demonstration
hello
good morning
[endsubj]
[newtext]
This is a 3th message
bonjour
morning
after moon
[endtext]
let's start with the beginning, if I have a listbox with the following values:
values are taken from our file.
This is a first demonstration
This is a 2th demonstration
This is a 3th demonstration
I wants when I choise Index 1, for example I want to delete
[newsubj]
This is a first demonstration
[endsubj]
[newtext]
This is a message
[endtext]
that is, to delete the message that contains it, not just the subject. Hey, I'm stuck here.
the subject will contain only one line, but the message will be composed of several lines of course.
this is the code I created that only deletes the subject, it seems to work,
although it can be created differently. it works because it contains the same number of lines, hey, further on the message the situation is a little different.
[newtext]
this is a message
[endtext]
Code:
TextBox1.Text = Array.FindIndex(linestring, Function(s) s.Contains(This is a first demonstration))
Dim delLine As Integer = TextBox1.Text
Dim lines As List(Of String) = System.IO.File.ReadAllLines((My.Application.Info.DirectoryPath + ("\Data.txt"))).ToList
For i As Integer = 0 To 2
lines.RemoveAt(delLine - 1) ' index starts at 0
Next
System.IO.File.WriteAllLines("Data.txt", lines)
Related
I'm having a strange situation:
I have a simple form with 2 text boxes.
The second one has the vba code triggered after being updated.
For example: typing value to the first field, ENTER, typing value into the second field, ENTER and the code starts.
The code, initially, takes the values from the text boxes and assign them to the string variables (pre declared as strings), like:
test1 = Me.frmSSN.Value
The problem is, the test1 variable seems to be empty after the line above.
It seems to happen only when I type, for example this string:
073QB8KJ2D00A4X
It works fine, when entering CNB0K2W5JK
The tool is a simple serial number comparison.
Just for test, I've entered this line into the code:
aaa="073QB8KJ2D00A4X"
When running in the step-by-step mode and hovering mouse over the "aaa" I'm getting: aaa= and then nothing.
Not even single "" like aaa="" or so. Just aaa=
After retrying multiple things - I believe it's about the value I enter:
073QB8KJ2D00A4X
Could be, that for access/vba it's some control string or so?
I'm just dumb now...
Thanks in advance for any help
Marek
p.s. Source fields are plain text boxes. And here's the code:
Dim user As String
Dim 1stSN As String
Dim 2ndSN As String
1stSN = Me.frmSSN.Value
2ndSN = Me.frmHPSN.Value
Then the values are being used as a part of SQL query. The problem is - in this situation - query doesn't work, as the sql string looks like:
"Select * From sbo_SerialSource where SN like " and nothing after "like".
Debug shows the correct value (with serial number), but query fails with "syntax error" message. Seems like there are some strange "control characters" are being created/added.
That's it.
And I have to use the serial numbers as these "strange ones", because that's how they come from the vendor.
I'm using excel vba to extract information from a word document.
In the word document, there are levels of numbered lists. For example:
1. ABC
1.1 DEF
1.1.1 ABCDEF
2. AAA
2.1 BBB
2.1.1. CCC
and I need to get the full context of each heading in each level and put them into an excel range, i.e. {"1.ABC", "1.1 DEF", "1.1.1 ABCDEF", "2. AAA", "2.1 BBB", "2.1.1. CCC"}
The function I use is:
For Each sec In objDoc.getcrossreferenceitems(wdRefTypeNumberedItem)
However, my headings are truncated if the headings are too long. For example, I have (random text is added for confidentiality reasons):
"5.2.11. Current References: As part of the evaluation process, XXX will conduct 2340AERTQ3493YR. When selecting ADT34534FDGSR, please ensure that they are AERA34AEFDS."
But only
5.2.11. Current References: As part of the evaluation process, XXX will conduct 234
is displayed, and the rest of the sentence is gone.
If anybody has an alternate solution, please let me know.
i confirm this behavior. A workeable albeit and elaborate solution is to scan the document for all numbered items which gives you the full text and then cross reference that result against the list returned by the GetCrossReferenceItems. There's quite some work involved but works and gives you the ability to create one list with referable Headings and NumberedItems, which is what I did to build a more user friendly alternative to Word's own implementation.
You'll have to match the formatting Word applies to the list returned by GetCrossReferenceItems, ie. the identation and removal of special characters.
Be careful with track changes. There is a bug in GetCrossReferenceItems which means that items (in my case headers) that have a tracked change at the beginning of the text are not returned by GetCrossReferenceItems but internally are still on the list so the index is offset. If the item in question is item 11, then GetCrossReferenceItems gives the item belonging to item 12 the item 11. A workaround is to accept all revisions before GetCrossReferenceItems and undo it after.
It's not easy but works.
I met a similar problem in MSWord. I found some paragraph's text are shorten in the following code
Sub bug()
items = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem)
For idx = 1 To UBound(items)
MsgBox items(idx)
Next
End Sub
I have to use a some long solution( in Python, sorry. But is is easy to rewrite in VBA):
varHeadings = []
for par in objDoc.Paragraph:
if par.Range.ListFormat.ListType == win32com.client.constants.wdListOutlineNumbering:
idx = par.Range.ListFormat.ListString
txt = par.Range.Text.strip('\n').strip('\r')
varHeadings.append('%s%s' % (idx, par.Range.Text))
which does work. However, as I have said, it is some tedious. So did I miss some VBA function in MSWord, or GetCrossReferenceItems has known bug and can not found any replacement in VBA?
I have been writing a code to build a custom document for weekly reporting. I had it set to create a new doc, build the tables, format everything, insert a logo, etc., and I hit a wall when Word crashed every time I tried to resize the logo through VBA. It then occurred to me that I was doing this the wrong way anyway, and that I should build a template, have the code open that template, and then insert the changes directly into that document.
When I add text to the range I need, everything below is deleted. I have three lines of a header; the third line is variable and will change each week. Below that I have three tables, some cells of which will be updated each week. Right now I am just stuck at the third line of the document.
Here is an example of what I have. It is extremely short because I started anew and hit a dead end immediately.
Dim Template as Document
Set Template = ThisDocument
Dim InsertSpot as Range
Set InsertSpot = Template.Range(46)
InsertSpot.Text = "Hello"
When I run that, everything is deleted below "Hello".
I tried some different things, such as:
Set InsertSpot = Template.Range(46,50)
InsertSpot.InsertAfter "Hello"
That doesn't work; it just adds "Hello" to the first cell of the first table.
I feel silly being stuck at such an elementary part, but I honestly have no idea what to do here. Everything I looked at online talked about how to insert text at a bookmark without deleting the bookmark. I just want to enter text, period.
Any thoughts?
I found the answer. I did the following:
Dim Template as Document
Set Template = ThisDocument
Dim InsertSpot as Range
Set InsertSpot = Template.Range(46, 46)
InsertSpot.Text = "Hello"
I had to list both the starting AND the ending character so that the inserted text would not bleed over into the rest of the page. Hope I helped someone else who is having a similar problem. Unfortunately Word VBA is pretty lacking in support and clear explanations.
other alternatives can be
ThisDocument.Range(46).InsertBefore "Hello"
ThisDocument.Tables(1).Range.InsertBefore "Hello"
ThisDocument.Paragraphs(3).Range.InsertAfter "Hello"
I need to modify a template document (docx) automatically, according to info I've read into some text files and I'm using VBA for this.The template document has a predefined record, which looks like this:
description: first description which can take more rows
...
author: the author
date: the date
...
What I need is copying the previous line below the first one, as many times as many text file I have, and complete each one whit info I've copied into strings. So,for example, if I have 3 text file I'll have the previous line three times, like this:
description: first description
...
author: the first author
date: the date in the first file
description: second description
...
author: the second author
date: the date in the second file
description: third description
...
author: the third author
date: the date in the third file
where the first one is already there and I need to repeat it twice.
How can I tell Word to copy and write in specific points of the document?
I've thought something like the following:(The code probably is not correct, I'll put it there to explaying my idea)
For i = 1 To n(number of text file)
ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(Index).Range.Start
End:=ActiveDocument.Paragraphs(Index+3).Range.End).Select
Selection.Copy
ActiveDocument.Paragraphs(Index+4).Select
Selection.Paste
Next i
the previous was for copying the record.
Now I have to fill that records writing the info after title:, author: and date: of each one, so something like:
For i=1 To n
ActiveDocument.Paragraphs(Index(i)+1).Words(2) = description(i)
ActiveDocument.Paragraphs(Index(i)+2).Words(2) = author(i)
ActiveDocument.Paragraphs(Index(i)+3).Words(2) = date(i)
Next i
where description, author and date are text string I already have.
Does that make any sense or I'm taking a wrong way? Is there any smarter VBA objects I could use?
Unfortunately, I don't have enough points yet to add this as a comment, where it would actually belong...
Which version of Word are you using?
Do you have any influence on the construction of the template, so that you could, for example, add bookmarks such as iDevelop suggests or possibly ContentControls (would be better)?
If you have no influence then it would be better for you to plan entering the data in each "copy" loop.
Note: I hope you're using Documents.Add to create new documents from the template so that you're not actually changing the template?
Greeting, have a little bit of a dilemma. I'm making a VBA that will allow the user to open two separate text files and use them to graph data. So far, I've been successful with getting the two text files open in one spreadsheet. The problem I'm having is using the data from one text file (12 digit numbers) and comparing that to the other text file, which is a log of data. The first text file has a bunch of irrelevant information, but I only need to take the date and 12 digit number recorded. The second text file has a list of 12 digit numbers which I want to check against the first text file and get a count of how many times each specific number came up. Using that, I want to just have a regular count of times it has occurred. I need this to be a VBA because the user that will be opening this macro won't have any programming knowledge so it needs to be very simple.
Also to note, the badge numbers CAN be different based on the text file uploaded, but they are always added in the same located of the spreadsheet.
Thanks.
EDIT: Tried to make a rough sketch of what I'm talking about
| A | B | C | D |
| 1 | DATA WITH NUMBER| ...........|........... |NUMBER TO COMPARE WITH |
| 2 | DATA WITH NUMBER|
I'm not sure I fully grasp what you want, but it sounds like you want to use the Dictionary class in VBA.
You would need to add a reference to the MS Scripting Runtime library, and your code to read the second input file (to count occurrences) could look something like this:
Dim counts As Object
Dim splitValue As Variant
Dim inputLine as String
Set counts = CreateObject("Scripting.Dictionary")
Open "yourfile.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, inputLine
splitValues = Split(inputLine, vbTab)
If counts.Exists(splitValues(0)) Then
counts(splitValues(0)) = counts(splitValues(0)) + 1
Else
counts.Add splitValues(0), 1
End If
Loop
Close #1
Then, when you read the first file, you could essentially call the dictionary to find how many occurrences of the badge number appeared in the second file.
occurrences = counts(badgeNumber)
Again, I may be off. If you can clarify what both inputs look like and what you want the final output to look like, it would help.