"PrintTicket provider failed to convert DEVMODE to PrintTicket" - wpf

few years ago I wrote a WPF (Visual Basic 2012 based) app which included "printing" some report using standard WPF printdialogs/paginators/features....
everyting worked fine since then (2011/2015), and I did not change a line of that working code...
Now I had to change something (not printing related) to that project and of course I checked the printing features as well...
now the "printing" code fails when using it... actually is not the "paginator" code, but accessing the PrintTicket/Que objects fails reporting
"PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect. "
but it's weird as it does not always fail, and it fails differently with different printers...
using for instance "PDF Creator" virtual printer, it fails writing/settings some properties of my Que object and not others, while using a different printer fails on others...
for instance, PDF Creator virtual printer:
1) myQue.DefaultPrintTicket = myTicket = OK
2) myQue.CurrentJobSettings.Description = "some title" = OK with some printer, FAILS with a different printer
but 2) only fails the very first time it is set, and I can "resolve" "looping" a few times with a minor time sleep.... again, the second or third time it succeded...
so I wrote an ugly code like
Dim dlg As New PrintDialog
.....
myTicket = dlg.PrintTicket
myQue = dlg.PrintQueue
'-----
' to check in a loop if the interesting
' properties has been set
Dim ticketIsSet(1) As Boolean
For iLoop As Integer = 1 To 5
'it can fail with DEVMODE = NULL
Try
If Not ticketIsSet(0) Then
myQue.DefaultPrintTicket = myTicket
ticketIsSet(0) = True
End If
If Not ticketIsSet(1) Then
myQue.CurrentJobSettings.Description = Me.Title
ticketIsSet(1) = True
End If
Catch ex As Exception
Errors.Add(ex.Message)
System.Threading.Thread.CurrentThread.Sleep(1000)
End Try
If ticketIsSet(0) AndAlso ticketIsSet(1) Then Exit For
Next
If Errors.Count Then
' it's still ok if title can't be set... :(
If ticketIsSet(0) Then Errors.Clear()
End If
so far, "so good" as I get the job done... but only partially... as my Paginator is used to (succesfully) populate a System.Windows.Controls.DocumentViewer... this alternate documentviewer window can later actually print if requested...
and again, with different printers it succed or fail with "PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect. " exception...
with PDF Creator virtual printer it succed, with a different physical printer it fails...
I tried the very same looping trick as above, like
Dim Errors As New Specialized.StringCollection
Dim bDone As Boolean = False
For iLoop As Integer = 1 To 5
Try
Dim writer As System.Windows.Xps.XpsDocumentWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(m_printQueue)
writer.Write(Me.docViewer.Document.DocumentPaginator, Me.m_printTicket)
writer = Nothing
bDone = True
Catch ex As Exception
Errors.Add(ex.Message)
System.Threading.Thread.CurrentThread.Sleep(1000)
End Try
If bDone Then Exit For
Next
If bDone Then Errors.Clear()
If Errors.Count <> 0 Then BasShowErrors(Errors, Me, False)
and here it fails (again, on some printers) on
writer.Write(Me.docViewer.Document.DocumentPaginator, Me.m_printTicket)
with "PrintTicket provider failed to convert PrintTicket to DEVMODE. Win32 error: The parameter is incorrect. " exception...
obviously that printer is ok as regards other programs like World, Excel and the like, and it's ok as well with all other (NOT WPF based) projects... and again this only occur "NOW", as it worked like a charme at the time of the initial development...
my current settings are:
Win7 Ultimate sp 1
SQL 2014 Dev Edition (not relevant)
Visual Studio 2012 Ultimate Version 11.0.61219.00 Update 5
Microsoft .NET Framework version 4.6.01055
I even tried "repairing" the NET Framework with NetFramwork Repair Tool (https://www.microsoft.com/en-us/download/details.aspx?id=30135) with no success...
any hint is very appreciated :)
TIA
asql

Related

Why do I get a Run-Time error '1004' on a MacBook when I copy the code from a PC version of VBA

I am new to VBA and learning; however, I am also running VBA on my MAC instead of PC. That being said, I copy the code down just like I have seen in VBA used by PCs and I get a run-time Error '1004'Method 'Range' of object '_Global' failed. Can someone please explain to me why that is?
My code is simple: MonthArray(1) = Range("myMonths").Cells(i,1).value
Thoughts anyone?
Sub Array_OneDimension()
Dim MonthArray(1 To 12) As String
Dim i As Byte
For i = 1 To 12
MonthArray(i) = Range("myMonths").Cells(i, 1).Value 'This is where the error is
Next i
End Sub
I was expecting the code to loop through the range and pull out the value in the locals window.

Only a portion of my front-end users are getting the full functionality of the application

I developed a front-end for a separate department that would allow them to key in data to our back-end table structure. Part of that front-end form includes a message box which opens each time they make a change. To allow them to provide a reason, add the date, etc.
Only certain users are seeing this message box when they use the form. It's not a permissions issue or a compatibility problem with versions of Access. We've looked into that. We're lost as to what it could be.
I know the code we use for the message box uses a particular library in MS Access. Could that be an issue? Anything else that you folks think it might be? We're just looking for possibilities here.
Source Code:
Option Compare Database
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim cmmnt As String
If Me.NewRecord Then
'Calls modAudit function to record new records to Audit Trail
cmmnt = InputBox("What is the reason for adding this record")
If StrPtr(cmmnt) = 0 Then
Me.Undo
Exit Sub
Else
Call AuditChanges("providerID", "NEW", cmmnt)
End If
Else
'Calls modAudit function to record edits to Audit Trail
cmmnt = InputBox("What is the reason for changing the value of this field?")
If StrPtr(cmmnt) = 0 Then
Me.Undo
Exit Sub
Else
Call AuditChanges("providerID", "EDIT", cmmnt)
End If
End If
End Sub

VB.Net Inserting information from an Access Database into an array

I'm doing my first project attempting to bind data from an .MDB file to a Windows Form project in Visual Basic. Most of the functions work off an arrays "Hubs", so I wanted to fill this array with information drawn from my database. After using the wizard to create DataSet, I created the following code.
Public Sub LoadData()
For i As Integer = 0 To 15
With Hubs(i)
.ID = HubsDataSet.Tables("Hubs").Rows(i).Item(1).ToString 'This line is flagged as bad by the unit tests.
.City = HubsDataSet.Tables("Hubs").Rows(i).Item(2).ToString
.Lat = HubsDataSet.Tables("Hubs").Rows(i).Item(3).ToString
.Lon = HubsDataSet.Tables("Hubs").Rows(i).Item(4).ToString
End With
Next
'Load ID1ComboBox List
For i As Integer = 0 To 15
ID1ComboBox.Items.Add(Hubs(i).ID)
Next
'Load ID2ComboBox List
For i As Integer = 0 To 15
ID2ComboBox.Items.Add(Hubs(i).ID)
Next
'Set default values for Combo boxes
ID1ComboBox.SelectedIndex = 0
ID2ComboBox.SelectedIndex = 1
End Sub
While the project appears to run and function properly, I now find that all my unit tests fail, throwing an error that no Row 0 exists. In addition, if I attempt to use a for each statement instead of a for loop, the program no longer functions (it skips the lines entirely, since it does not hit a break point I inserted). I'm new to this and very confused what I'm doing wrong.

How can I automatically log into a website using vbscript?

Im trying to automatically log into a website using a script so our employees don't have to remember the admin password.
Dim IE
Dim Helem
Set IE =CreateObject("InternetExplorer.Application")
IE.Visible = 1
IE.navigate "http://rentalapp.zillow.com/"
Do While (IE.Busy)
WScript.Sleep 10
LoopSet
Helem = IE.document.getElementByID("formUsername")
Helem.Value ="username"
Set Helem =IE.document.getElementByID("formPassword")
Helem.Value = "password"
Set Helem = IE.document.Forms(0)
Helem.Submit
I've gotten this far from other posts I've seen but I keep getting an error saying:
Line: 10
char: 2 Error:
Object required
code: 800A01A8
source: Microsoft VBScript runtime error
I've researched the code and and it says there is a typo somewhere but for the life of me I can't figure out where.
I'm not too familiar with vbscript but with functions like this I want to become more familiar with it. Please help.
Helem = IE.document.getElementByID("formUsername")
Helem.Value ="username" <= this is line 10
Failed at line 10: looks like you failed to find an element with the ID "formUsername".
Take a look at the source of the page and check your ID is correct.
Helem = IE.document.getElementByID("formUsername")
==>
Set Helem = IE.document.getElementByID("formUsername")
as later in your code.

Version control Access 2007 database and application

I need to version control a Microsoft Access 2007 database and application. Currently everything is contained in a single mdb file.
The application includes:
Forms
VBA code
Actual database
I would assume I need to separate the database from the forms/code. I would like to be able to version control the forms/code as text to support version diffs.
At the moment I don't have access to SourceSafe (I heard there may be some access support) so I would prefer a solution that would work with subversion or git.
Access 2007 has a feature where you can split a DB into its Tables/Queries (backend) and Forms/Reports (front-end). Since your question mentions only version controlling the forms and modules, this might be a more elegant solution. I don't know where modules go after the split, so that might be a stumbling block.
Microsoft offers VSTO (Visual Studio Tools for Office), which will let you develop in VS and run version control via any VS plugin (CVS/SVN/VSS/etc.).
Finally, you can just directly connect to Visual Source Safe. This MSKB article has some good information and background to go through, while this Office Online article is designed for getting you up and running.
Ultimately, I would suggest against taking the code out of Access if at all possible. Assuming the VBA editor is your primary development environment, you'll be adding extra steps to your development process that cannot easily be automated. Every change you make will need to be manually exported, diff'd, and stored, and there is no Application.OnCompile event that you could use to export the changes. Even tougher, you'll have to manually import all changed source files from other developers when they do checkins.
I use the code below to extract the vba code from Excel files, you may be able to modify this to extract from Access.
Sub ExtractVBACode(strSource, objFSO, strExportPath, objLogFile)
Dim objExcel
Dim objWorkbook
Dim objVBComponent
Dim strFileSuffix
Dim strExportFolder
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = true
Set objWorkbook = objExcel.Workbooks.Open(Trim(strSource))
strExportFolder = strExportPath & objFSO.GetBaseName(objWorkbook.Name)
If Not objFSO.FolderExists(strExportFolder) Then
objFSO.CreateFolder(strExportFolder)
End If
For Each objVBComponent In objWorkbook.VBProject.VBComponents
Select Case objVBComponent.Type
Case vbext_ct_ClassModule, vbext_ct_Document
strFileSuffix = ".cls"
Case vbext_ct_MSForm
strFileSuffix = ".frm"
Case vbext_ct_StdModule
strFileSuffix = ".bas"
Case Else
strFileSuffix = ""
End Select
If strFileSuffix <> "" Then
On Error Resume Next
Err.Clear
objVBComponent.Export strExportFolder & "\" & objVBComponent.Name & strFileSuffix
If Err.Number <> 0 Then
objLogFile.WriteLine ("Failed to export " & strExportFolder & "\" & objVBComponent.Name & strFileSuffix)
Else
objLogFile.WriteLine ("Export Successful: " & strExportFolder & "\" & objVBComponent.Name & strFileSuffix)
End If
On Error Goto 0
End If
Next
objExcel.DisplayAlerts = False
objExcel.Quit
End Sub
Can you extract the forms as XML perhaps?
I've struggled with this same problem. I originally wrote code very much like the existing answer. The trick is to get all of your modules onto the file system, but that method has some drawbacks. Going that route, you can get your forms and reports out of the VBA Projects, but you can't get them back in. So, I created a library as part of our Rubberduck VBE Add-in. The library I wrote takes care of importing and exporting all of your code to/from the VBA project to/from the repository as you seemlessly push, pull, and commit. It's a free and open source project, so feel free to download and install the latest version.
Here is an example of how the library is used. I'll be adding actual integration with the VBA editor in a future release.
Dim factory As New Rubberduck.SourceControlClassFactory
Dim repo As Rubberduck.IRepository
Dim git As ISourceControlProvider
Dim xl As New Excel.Application
xl.Visible = true
Dim wb As Excel.Workbook
Set wb = xl.Workbooks.Open("C:\Path\to\workbook.xlsm")
' create class instances to work with
Set repo = factory.CreateRepository(wb.VBProject.Name, "C:\Path\to\local\repository\SourceControlTest", "https://github.com/ckuhn203/SourceControlTest.git")
Set git = factory.CreateGitProvider(wb.VBProject, repo, "userName", "passWord")
' Create new branch to modify.
git.CreateBranch "NewBranchName"
' It is automatically checked out.
Debug.Print "Current Branch: " & git.CurrentBranch
' add a new standard (.bas) code module and a comment to that file
wb.VBProject.VBComponents.Add(vbext_ct_StdModule).CodeModule.AddFromString "' Hello There"
' add any new files to tracking
Dim fileStat As Rubberduck.FileStatusEntry
For Each fileStat In git.Status
' fileStat.FileStatus is a bitwise enumeration, so we use bitwise AND to test for equality here
If fileStat.FileStatus And Rubberduck.FileStatus.Added Then
git.AddFile fileStat.FilePath
End If
Next
git.Commit "commit all modified files"
' Revert the last commit, throwing away the changes we just made.
git.Revert

Resources