Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type Mismatch error

Status
Not open for further replies.

QueTech

MIS
Apr 13, 2003
79
US
Hi,

I'm using VB macros in microsoft Word Templates to pull client ID and name and place on the document. I have begun today to modify my macro to open "SaveAs" dialogue window. That part is working correctly. However, in my SaveAs window i am coding the file name to pull the Name , document type and file type. When I added a variable dtdate to the file name I get " Run Time Error "13" Type Mismatch. The variable is dtdate and I set it equal to "Now" or "Date". Please find below my code.

'==================================
'==================================
Private Sub cmdSave_Click()
pgnum = 1
namevar = frmSave.txtname.text
idvar = frmSave.txtID.text
dtdate = Now
Proceedvar = 1
Dim oldname, savFileName As Boolean

'THE NEXT BUTTON WAS JUST PRESSED
'check to see that all fields are filled in with something
'If a field is blank do not proceed

If frmSave.txtID = "" Then
MsgBox ("You need to fill in a Patient ID Number")
txtID.SetFocus
Proceedvar = 0
End If
If frmSave.txtname = "" Then
MsgBox ("You need to fill in a name")
Proceedvar = 0
txtname.SetFocus
End If
'
If Proceedvar = 1 Then ' TESTED FOR BLANK FIELDS
' Now that all fields are filled
' Move collected data to document
While pgnum < 3
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=pgnum
Selection.Find.Replacement.ClearFormatting
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With Selection.Find
.text = "(name)"
.Replacement.text = namevar
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Replace the patien's ID with data from the form
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "(number)"
.Replacement.text = idvar
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
pgnum = pgnum + 1
Wend
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Name:=1
' next we will bring up the "Save As" dialog box
cd1.Filter = "Microsoft Word Document (*.doc)"
UserName = Environ("username")
Pathvar = CurDir
Testvar = "I:\" + UserName
Mylen = Len(Testvar)
MyNew = Mid(Pathvar, 1, Mylen)
Mycheck = Pathvar Like Testvar
If MyNew = Testvar Then
cd1.InitDir = Pathvar
Else
cd1.InitDir = Testvar
End If
' if the user clicks cancel on the Save As dialog box,
' we do not want to save
With Selection.Find
.text = cd1.InitDir
.Replacement.text = thisdir
End With
cd1.FileName = namevar + dtdate + "_Format" + ".doc"
'Name savFileName As InitDir + cd1.FileName + ".doc"
cd1.ShowSave
ActiveDocument.SaveAs FileName:=cd1.FileName
frmSave.Hide
End If ' Ends the Test for blank fields

' Go to the end of the Document and prepare for note entry

End Sub

Thanks
 
Dealing with bookmarks in header can be a bit different. I have offered to help with the bookmark issue, but you are not answering questions. And now you seem to be changing what the problem is. I have no idea at ll if you still have problems with the code replacing only the last page. Is that fixed?

good luck.

Gerry
 
Hi,

I want to respond to the following questions.

1. WHAT "wrong" type of bookmark???????? Since you did not post the code showing how you inserting these, I am very curious as to what you were doing.

I think I hav addressed that in a previous posting. But I am using Word to insert book marks into my Work template.


2. Since you did not post the code for these insertions, are you sure that the bookmarks were actually inserted?

The Bookmarks are displayed on the template.

3. The code you posted only deals with three bookmarks. WHAT other ones?

I was getting ahead of myself. Once this project is working correctly I will need to implement this in other templates as well.

4. I made a comment earlier regarding the multiple uses of Bookmarks("whatever").Range.Text. It often removes the bookmark. Yes, it inserts the text in the proper place, but it overwrites the bookmark itself. There is a way around that.

I understand that it will overwrite the book mark, but the original template should remain the same. Should'nt it?


- When go into my template and reinsert the book marks. And then try to run my macro I get the error Runtime error '5941'the requested member of the collection does not exist. I fill like this points to the book marks, however I have just added them back to the template.

Any help is appreciated.
 
1. WHAT "wrong" type of bookmark???????? Since you did not post the code showing how you inserting these, I am very curious as to what you were doing.

I think I hav addressed that in a previous posting. But I am using Word to insert book marks into my Work template
No, you did not. There has been absolutely NO explanation of what you meant by "wrong" bookmarks. I still do not know what you mean by "wrong " bookmarks. There are no wrong bookmarks. There are bookmarks...but not "wrong" ones. So...no, you never addressed that whatsoever. In fact, here is another quote from you:
I was inseting the wrong type of bookmark. Now am inserting the bookmarks from the menu optio Insert > Bookmarks.
This states you were inserting the "wrong" type of bookmark, NOW you are inserting with Insert > Bookmark. Excuse me, but that is the only way you canb insert bookmarks - so I am still confused about the wrong type of bookmarks that you have NOT addressed. You may think you have...but you have not.
The Bookmarks are displayed on the template.
What does that mean? You have bookmarks visible inthe document? Normally they are NOTvisible. If you have them visible (displayed), of what use is that? And it is not relevant, even if you DO have them visible (displayed). What does "display" mean?
I understand that it will overwrite the book mark, but the original template should remain the same. Should'nt it?
Yes, the original template file remains the same. However, I am not even sure you are properly cloning the template into a new document. If your code is run, then you should not get that error. However, if you run it again (on the same document), you will.
When go into my template and reinsert the book marks. And then try to run my macro I get the error Runtime error '5941'the requested member of the collection does not exist. I fill like this points to the book marks, however I have just added them back to the template.
You are not being clear at all on the steps you are taking.

If you get that error on a cloned document, putting the bookmarks back into the template is not going to do ANYTHING with that document.

I am absolutely sure that the 5941 error is because of the bookmarks.

I am absolutely sure you are wiping out those bookmarks with your code.

I am absolutely sure I mentioned that there was a way around wiping out bookmarks when you put text into them.

I am absolutely sure that fixing bookmarks in a template does nothing to an existing document previously cloned from that template.

Gerry
 
What I should have said was that my process for creating a bookmark was different(not the wrong type). Previously I would place the text "(name)", "(number)", etc. in the header by typing them in the header. I would then use my code to search the header for the text and replace the text(as seen in my original code). I felt the my process was wrong.
 
I believe I have inserted the bookmarks correctly on all of the pages of my template. However, when I run the macro the text is place on the last page only, withou the 5941 error.

Here is my VB code.

Private Sub CmdFill_Click()
namevar = frmFormat.txtname.Text
idvar = frmFormat.txtID.Text
Adatevar = frmFormat.txtAdate.Text
dtdate = Now
' If Proceedvar = 0 Then ' TESTED FOR BLANK FIELDS
' Now that all fields are filled
' Move collected data to document
With ActiveDocument
.Bookmarks("name").Range.Text = namevar
.Bookmarks("number").Range.Text = idvar
.Bookmarks("Adate").Range.Text = Adatevar
End With
Unload frmFormat
' End If ' Ends the Test for blank fields
' Go to the end of the Document and prepare for note entry
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Name:=1
End Sub

Any help is appreciated

 
Your code will put the strings into the bookmark - although actually it probably put it after the bookmark.

That's it. It will put it in the bookmark. So what is your problem again? You persist in not describing it. If the text is only on the last page:

1. That is where the bookmark is.
2. There may be other bookmarks - but I don't know as you do not clearly state anything - but so what? Your code does not put anything in any other bookmarks. If you want o put text in other bookmarks...then do so. I am not sure what is stopping you.
I believe I have inserted the bookmarks correctly on all of the pages of my template.
Perhaps not.

A key question is - what are your headers like? Do you have DifferentFirstPage, DifferentOddEven? If you describe what your header structure is like - but please, describe it clearly - then that may help.

If you DO have the bookmarks in the header, and you are putting text into them, if they are only on the last page, then the header for the other pages is not the same.

Gerry
 
The first eight headers are the same. The nineth header is different and headers 10 and 11 are the same as the first eight.

I have "Link to the Preveious" for headers 2 - 8 back to Header 1 .
Headers 2-8 state (at the top) "Same as Previous"
Header 9 is dfferent
Header 10 does not state "Same as Previous"
Header 11 states (at the top) "Same as Previous".

When I run the macro with these settings for the header I get the 5941 error.

If I link only the headers 2-8 to header 1 and do nothing with the rest of the headers, the text is place on headers 1-8. I understand that the problem is in having a different header for header 9.
 
I have not corrected my issue with the placing text in each header, however,I think I understand why I recieved the 5491 error.

In an attempt to clarify what I am trying to produce:

My macro opens a form that gather information and the macro places the text on the document. Once the user is satisfied I want to (code) where the file will be saved. I want a second form to open when the user closes the document to ensure that the user is satisfied and ready to save,if the user presses "Save" then the file should be saved if the user presses "cancel" then the document should be dumped. I am able to open a save dialogue box, however I understand that there is an easier way to code a save command prompt the user to keep or dump the file. That coding I am not clear on.
 
In the Header there are two tables (created by going to Table > Insert > Table). The first table consist of two columns and Three rows, each column is 4 inches in width and the rows are 1/4 inch in hieght.

The second table consist of two columns and 7 rows with same diminsions as the first.

I placed my bookmarks name, number & Adate in cells 3, 5 & 7 respectively. THe following text is permenantly displayed in the header.

ALBEMARLE MENTAL HEALTH CENTER NORTH CAROLINA DIVISION OF
AND MENTAL HEALTH, DEVELOPMENTAL DISABILITIES
DEVELOPMENTAL DISABILITIES & SUBSTANCE ABUSE SERVICES AND SUBSTANCE ABUSE SERVICES


File material in reverse chronological order.
CLIENT:


When more than one identical form, file latest on top.
RECORD NUMBER:
 
OK, can we keep things a little more separate and clear? There are TWO things that you seem to be dealing with.

1. The file saving issue.
2. The header text pacement issue.

Which do you want to deal with?

Gerry
 
I am in need of help for both but the saving issue will do me now good, with out the header text placement issue resolved. Previously, you wrote that there is away around the text placement issue. If you could provide that information I will implement the fix.
 
FYI

I am using the code below to save the document. However the dtdate variable is giving me an (variable not defined) error.

Private Sub CommandButton1_Click()
dtdate = Format(Now, "yyyy-mm-dd_hh_nn")
ActiveDocument.SaveAs filename:="C:\temp\" & dtdate & "Format." & "TEST3"
Unload SaveYourFile
ActiveDocument.Close
ActiveDocument.Close SaveChanges:=False 'The commond with will ensure that the SaveAs dialogue box will not open.
End Sub

 
Private Sub CommandButton1_Click()
[!]Dim dtdate As String[/!]
dtdate = Format(Now, "yyyy-mm-dd_hh_nn")
...


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top