vba - write text to sfile path does not work error - vba

With the help of Userforms and textboxes, i can add new personnel to my personnel file, but there is something wrong in my code and the code does not work. I do get an error while carrying it out. The error says "Compile error: Method or data member not found". I dont know what is wrong with my code, my expectations are that i fill in some Textboxes and i want them added to the already existing seq file (Notepad). But it doesnt let me do that and i dont know why.
I would be happy if someone can find the problem and give a solution.
Thanks in advance.
If you need code from other Commandbuttons,Userforms,etc.. Please tell me.. I dont know entirely what to publish now.
I have already tried changing the Userform_Nieuw names, but to no avail.
Private Sub seq_bestand_maak_databank()
Dim diploma As String
Dim pad As String
pad = "C:\Users\fhaka\Downloads\Overzicht_Personeelsleden.txt"
If UserForm_Nieuw.OptionButton1.Value = True Then
diploma = "Secundair"
ElseIf UserForm_Nieuw.OptionButton2.Value = True Then
diploma = "Bachelor"
ElseIf UserForm_Nieuw.OptionButton3.Value = True Then
diploma = "Master"
End If
If CInt(nieuw.Label1.Caption) = 1 Then
Open pad For Output As #1
Else
Open pad For Append As #1
End If
Write #1, CInt(UserForm_Nieuw.Label1.Caption), UserForm_Nieuw.Voornaam, UserForm_Nieuw.Naam, CInt(UserForm_Nieuw.Aantal_kinderen), UserForm_Nieuw.Geboortedatum, UserForm_Nieuw.Startdatum, "---N/A---", diploma, "0", "---N/A---", "---N/A---", "EOR"
Close #1
End Sub
The goal is that the commandbutton works again and that i can add more personnel now.

The #1 part in Open pad For Output As #1 etc is a file handle. To obtain a file handle, you need to call FreeFile. You cannot just assume that file handle #1 is available at all times.
Try this:
Dim iOutputFile As Integer
iOutputFile = FreeFile
If CInt(nieuw.Label1.Caption) = 1 Then
Open pad For Output As #iOutputFile
Else
Open pad For Append As #iOutputFile
End If
Write #iOutputFile, CInt(UserForm_Nieuw.Label1.Caption), UserForm_Nieuw.Voornaam, UserForm_Nieuw.Naam, CInt(UserForm_Nieuw.Aantal_kinderen), UserForm_Nieuw.Geboortedatum, UserForm_Nieuw.Startdatum, "---N/A---", diploma, "0", "---N/A---", "---N/A---", "EOR"
Close #iOutputFile

It looks like you have not defined the method of the userform. You failed to compile and the highlight is on the UserForm_Nieuw.Voornaam. So I take that is some sort of input field for the first name? Check whether it is really called that or if it does no have the old default name (something like "Textbox275" or some such like your other elements, e.g. OptionButton1 and 2).

Related

inserting data from a text file to textboxes and setting combobox indecies

so I am writing a program and trying to setup the save/open features of the program. I have the Save feature working just fine, but can't get the open feature to work.
The issue I'm running into is pulling the data from the text file to the form to fill in the multiple fields and controls. my example code is below
Imports System.IO
Main 1
Sub openFile_Click(sender, e) handles openFile.Click
Dim lineIndex As Integer = 12 'this is my total lines in my file
ofdRead.ShowDialog()
If ofdRead.FileName <> "" then
sr = New StreamReader(ofdRead.FileName)
For i As Integer = 0 To lineIndex -1
sr.ReadLine()
Next
txtField1.Text = sr.ReadLine
cboBox1.SelectedIndex = sr.ReadLine
'this continues through all fields til complete
sr.Close()
End If
End Sub
End Class
I keep getting an error for anything that is being returned as not being a string, and it seems as though the data is being read in reverse as well according to my error output.
Any help would be much appreciated (been searching and pouring over forums for 2 days looking for help)
Thanks Tim Schmelter for your insight. I was calling the wrong data type for the cboBox1 variable. Here is my corrected code (without the For-Loop, turns out i didn't need it)
cboBox1.SelectedItem = sr.ReadLine
so everytime I run into something like that I just have to tell it to put it in as a string instead of an integer

Requested Member of Collection Does not Exist - VBA error when copying from array to Word table

I am reading a string from a csv file and trying to print this into respective cells in a word document table. So far I have this, but it keeps throwing the Collections does not exist error:
Private Sub CommandButton1_Click()
Dim MyData As String, strData() As String
Open "D:\Users\file2.csv" For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, ",")
For i = 1 To UBound(strData)
ActiveDocument.Tables(1).Cell(i, 2).Range.Text = strData(i)
Next i
End Sub
Basically I am taking the ith element of the array and pasting it in the ith row of the second column.
The csv file has only 1 line (will always have only one line) with 6 fields:
hello,world,blah,haha,yes,huh
To be safe (and for testing purposes), my table is an 8x8 table, and it still throws the error on the ActiveDocument... line.
To be even safer, I have 2 tables, (just in case my Table number is off (not sure if it begins at 1 or 0)).
Changed the line to:
ActiveDocument.Tables(1).Cell(1, i).Range.Text = strData(i - 1)
And it works. However the text is only displayed after I close the user form. How do I get the text show upon button click?
Turns out I had a another function after, that I was not using. When I commented out this function, it worked. I still do not know why an unused function would prevent the userform from displaying the entered data, but it works.
Regarding the Collection does not exist error, I changed the line to:
ActiveDocument.Tables(1).Cell(1, i).Range.Text = strData(i - 1)
and now it works

Getting the current line number using Visual Studio Macros?

So I looked through other user's question but couldn't find on specifically upon what I am looking for. What I am trying to do is very simple. I am writing a Visual Studio Macro and am trying to obtain the number of the current line that the TextSelection is on. So that's it really, my question is quite simple. How do you get the number of the line that the selection is currently at? Any help is greatly appreciated!
Just so it's clear to anybody reading this, I am using VB and am writing Visual Studio Macro.
Keep in mind that a TextSelection can span multiple lines, so there's potentially a range of lines.
Looking at the docs for TextSelection (i.e. I haven't tested this), you should be able to do something like this:
Dim mySelection As EnvDTE.TextSelection = ' however you get the active selection
mySelection.TopPoint.Line ' gets the line of the top of the selection
If you want to get it based on where the cursor is (top or bottom of the selection) you can try this:
mySelection.ActivePoint.Line
It looks like the TextRanges might also be useful, but it sounds like it's for box selection only, so it might not apply.
mySelection.TextRanges.Item(0).Line
There is probably a better way than this, but the first way that comes to my mind is something like this.
First, make sure that the last line in your file is "xyz"
Dim linenumber As Integer = 1
dim mystring as string = ""
Using myfile As New IO.StreamReader("C:/myfile")
mystring = myfile.readline()
while mystring <> "xyz"
linenumber += 1
messagebox.Show(mystring & " is on line " & linenumber)
end while
End Using
So if the contents of C:/myfile looked like this....
I
Love
Pie
Then you would get as output....
"I is on line 1"
"Love is on line 2"
"Pie is on line 3"

Object reference not set to an instance of an object (Completely broken?) in vb.net

I know, I know, I could have used a for loop, dont tell me anything about that. Please, help!
Private Function LoadSaved() ''//Loads saved clippings if the user wants us to
Dim ZomgSavedClips As StringCollection
If IsDBNull(My.Settings.SavedClips) = False Then ''//If it is null this would return a rather ugly error. Dont want that do we?
ZomgSavedClips = My.Settings.SavedClips ''//ZomgSavedClips name was a joke, I just felt like it.
ZomgSavedClips.Add(" ") ''//This line ought to fix the error, but doesnt
i = 0
While i < ZomgSavedClips.Count ''//This is where the error occurs
ClipListings.Rows.Add(ZomgSavedClips(i))
i = i + 1 ''//First time I wrote this function I forgot this line. Crashed mah comp. Fail.
End While
End If
End Function
The line While i < ZomgSavedClips.Count is bugging, I know that the .count should return null but I even added a blank piece of text just to stop that. Whats up with this? Should I add actual text?
SavedClips is null no? If it is null it could pass the test IsDBNull beacuse the both are not the same
Obviously, My.Settings.SavedClips is still set to Nothing.
SavedClips is regular 'ole null (nothing in VB). Include a check for "My.Settings.SavedClips is nothing". If that evaluates to true then just leave the function.
I even added a blank piece of text just to stop that.
All you did was move where the error happens. You can't call .Add() on a null/Nothing object.
'''<summary>Loads saved clippings if the user wants us to</summary>'
Private Sub LoadSaved() ''//Loads saved clippings if the user wants us to
''//Load saved clips into memory
Dim ZomgSavedClips As StringCollection = My.Settings.SavedClips
If ZomgSavedClips Is Nothing Then ZomgSavedClips = New StringCollection()
''//Apply loaded clips to visible listings
Dim i As Integer
While i < ZomgSavedClips.Count ''
ClipListings.Rows.Add(ZomgSavedClips(i))
i += 1
End While
End Sub
Some notes on this code:
Don't use Function when you mean Sub
Since you'll be selling this code to others, you want to use xml comments at the top so that Visual Studio can give better intellisense helps.
IsDBNull() doesn't do what you think it does.
Yes, you should use a for loop, but since you already commented on that I left the while loop alone with the assumption that there's more code you didn't show us.

GetCrossReferenceItems in msword and VBA showing only limited content

I want to make a special list of figures with use of VBA and here I am using the function
myFigures = ActiveDocument.GetCrossReferenceItems(Referencetype:="Figure")
In my word document there are 20 figures, but myFigures only contains the first 10 figures (see my code below.).
I search the internet and found that others had the same problem, but I have not found any solutions.
My word is 2003 version
Please help me ....
Sub List()
Dim i As Long
Dim LowerValFig, UpperValFig As Integer
Dim myTables, myFigures as Variant
If ActiveDocument.Bookmarks.Count >= 1 Then
myFigures = ActiveDocument.GetCrossReferenceItems(Referencetype:="Figure")
' Test size...
LowerValFig = LBound(myFigures) 'Get the lower boundry number.
UpperValFig = UBound(myFigures) 'Get the upper boundry number
' Do something ....
For i = LBound(myFigures) To UBound(myFigures) ‘ should be 1…20, but is onlu 1…10
'Do something ....
Next i
End If
MsgBox ("Done ....")
End Sub*
Definitely something flaky with that. If I run the following code on a document that contains 32 Figure captions, the message boxes both display 32. However, if I uncomment the For Next loop, they only display 12 and the iteration ceases after the 12th item.
Dim i As Long
Dim myFigures As Variant
myFigures = ActiveDocument.GetCrossReferenceItems("Figure")
MsgBox myFigures(UBound(myFigures))
MsgBox UBound(myFigures)
'For i = 1 To UBound(myFigures)
' MsgBox myFigures(i)
'Next i
I had the same problem with my custom cross-refference dialog and solved it by invoking the dialog after each command ActiveDocument.GetCrossReferenceItems(YourCaptionName).
So you type:
varRefItemsFigure1 = ActiveDocument.GetCrossReferenceItems(g_strCaptionLabelFigure1)
For k = 1 To UBound(varRefItemsFigure1)
frmBwtRefDialog.ListBoxFigures.AddItem varRefItemsFigure1(k)
Next
and then:
frmBwtRefDialog.Show vbModeless
Thus the dialog invoked several times instead of one, but it works fast and don't do any trouble. I used this for one year and didn't see any errors.
Enjoy!
Frankly I feel bad about calling this an "answer", but here's what I did in the same situation. It would appear that entering the debugger and stepping through the GetCrossReferenceItems always returns the correct value. Inspired by this I tried various ways of giving control back to Word (DoEvents; running next segment using Application.OnTime) but to no avail. Eventually the only thing I found that worked was to invoke the debugger between assignments, so I have:
availRefs =
ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem):Stop
availTables =
ActiveDocument.GetCrossReferenceItems(wdCaptionTable):Stop
availFigures = ActiveDocument.GetCrossReferenceItems(wdCaptionFigure)
It's not pretty but, as I'm the only person who'll be running this, it kind of works for my purposes.