Issue with pdf docs not showing up - vb.net

We recently wrote some code for a client using the Aspose.pdf library, on my system the pdf in question opened fine and most of the merge fields were filled in (we don't have the exact list of merge fields that they do).
They're telling me that on their system, some documents take 2-4 mins to open while others don't open at all.
What could be a possible cause of the document not opening at all?
My code is below:
' Load form
Dim doc As Aspose.Pdf.Document = New Aspose.Pdf.Document(sTemplateDir & sDocName)
'Get names of form fields
Dim fields As Aspose.Pdf.InteractiveFeatures.Forms.Field() = doc.Form.Fields
Dim sField As String
Dim field As Aspose.Pdf.InteractiveFeatures.Forms.Field
If fields.Length > 0 Then
For Each field In fields
'Get name of field
sField = field.FullName
'If the merge field isn't valid then we'll just leave it and assume its a fill-in
If nMergeCol.Contains(sField) And Not IsNothing(sField) Then
field.Value = nMergeCol.Item(sField)
End If
Next
End If

This has been resolved! As we suspected, it was a problem with the client's Javascript within the pdf file. The problem was within the calculations the absolute value was being used (name.value). Once this was switched to the relative value (this.event.value) the pdf file began behaving correctly with the AsPose code.

Related

Word automation failing on server, but dev station works great

I am automating a oft-used paper form by querying the user on a web page, then modifying a base Word document and feeding that modified doc file to the user's browser for hand-off to Word.
The code is Visual Basic, and I am using the Microsoft.Office.Interop module to manipulate the document by manipulating Word. Works fine on the development system (Visual Studio 2015) but not on the production server (IIS 8.5).
Both the Documents.Open() call and the doc.SaveAs() call fail with Message="Command failed" Source="Microsoft Word" HResult=0x800A1066
Things I've tried:
Added debugging out the whazoo: Single-stepping is not an option on the production machine, so I pinpointed the problem lines with debug output.
Googled and found that this problem has been reported as early as 2007, but no viable solutions were reported.
A couple sites mentioned timing issues, so I added several pauses and retries -- none helped.
Some mentioned privileging, so I tried changing file permissions & application pool users -- neither helped.
Enhanced my exception handling reports to show more details and include all inner exceptions. That yielded the magic number 800A1066 which led to many more google hits, but no answers.
Added fall-back code: if you can't open the main document, create a simple one. That's when I found the SaveAs() call also failing.
Dropped back to the development system several times to confirm that yes, the code does still work properly in the right environment.
Greatly condensed sample code does not include fallback logic. My Word document has a number of fields whose names match the XML tokens passed as parameters into this function. saveFields() is an array of those names.
Dim oWord As Word.Application
Dim oDoc As Word.Document
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open(docName)
Dim ev As String
For i = 0 To saveFields.Length - 1
Try
ev = dataXD.Elements(saveFields(i))(0).Value
Catch
ev = Nothing
End Try
If ev IsNot Nothing Then
Try
Dim field = oDoc.FormFields(saveFields(i))
If field IsNot Nothing Then
If field.Type = Word.WdFieldType.wdFieldFormTextInput Then
field.Result = ev
End If
End If
Catch e As Exception
ErrorOut("Caught exception! " & e.Message)
End Try
End If
Next
...
oDoc.SaveAs2(localDir & filename)
oDoc.Close()
oWord.Quit(0, 0, 0)
The code should generate a modified form (fields filled in with data from the parameters); instead it fails to open, and the fallback code fails to save the new document.
On my dev system the document gets modified as it should, and if I break at the right place and change the right variable, the fallback code runs and generates the alternate document successfully -- but on the production server both paths fail with the same error.
Barring any better answers here, my next steps are to examine and use OpenXML and/or DocX, but making a few changes to the existing code is far preferable to picking a new tool and starting over from scratch.
Unfortunately, Lex Li was absolutely correct, and of course, the link to the reason why is posted on a site my company considers off limits, thus never showed up in my google searches prior to coding this out.
None of the tools I tried were able to handle the form I was trying to automate either -- I needed to fill in named fields and check/uncheck checkboxes, abilities which seemed beyond (or terribly convoluted in) the tools I evaluated ...
Eventually I dug into the document.xml format myself; I developed a function to modify the XML to check a named checkbox, and manipulated the raw document.xml to replace text fields with *-delimited token names. This reduced all of the necessary changes to simple string manipulation -- the rest was trivial.
The tool is 100% home-grown, not dependent upon any non-System libraries and works 100% for this particular form. It is not a generic solution by any stretch, and I suspect the document.xml file will need manual changes if and when the document is ever revised.
But for this particular problem -- it is a solution.
This was the closest I got to a complicated part. This function will check (but not uncheck) a named checkbox from a document.xml if the given condition is true.
Private Shared Function markCheckbox(xmlString As String, cbName As String, checkValue As Boolean) As String
markCheckbox = xmlString
If checkValue Then ' Checkbox needs to be checked, proceed
Dim pos As Integer = markCheckbox.IndexOf("<w:ffData><w:name w:val=""" & cbName & """/>")
If pos > -1 Then ' We have a checkbox
Dim endPos As Integer = markCheckbox.IndexOf("</w:ffData>", pos+1)
Dim cbEnd As Integer = markCheckbox.IndexOf("</w:checkBox>", pos+1)
If endPos > cbEnd AndAlso cbEnd > -1 Then ' Have found the appropriate w:ffData element (pos -> endPos) and the included insert point (cbEnd)
markCheckbox = markCheckbox.Substring(0, cbEnd) & "<w:checked/>" & markCheckbox.Substring(cbEnd)
End If
' Any other logic conditions, return the original XML string unmangled.
End If
End If
End Function

store word content in variable

How do I copy the entire content (approx 2 pages) of a Word document in VBA and store in a variable?
I keep trying several things, none of which works:
Dim mainData As String
ThisDocument.Activate
ActiveDocument.WholeStory 'error on this line
mainData = Selection.Text
With 'record macro' I can simulate selecting a piece or the entire text, but I can't simulate storing that into a variable.
The above code throws
'This command is not available because no document is open',
but hadn't I first activated this (the current) document, and then selected it (ActiveDocument.WholeStory)?
Why doesn't this work?
Later edit: I managed to do the selection like this:
Dim sText As String
Application.Selection.ClearFormatting
Application.Selection.WholeStory
sText = Application.Selection.Text
MsgBox sText
but the problem is I can't store the entire text (2 pages) in a variable. Part of it is truncated. Would you know how to store word by word (I only need a word at a time anyway)?
Later edit. I applied strReverse on the text to find out the text is actually stored entirely in the variable, just not fully displayed in the message box.
Don't use ThisDocument in code, unless you specifically want to address the file in which the code is stored and running. ThisDocument is the "code name" of that file.
Instead, use ActiveDocument to mean the document currently active in the Word window.
An addition, if you want the Selection in the currently active document, there's no reason to activate it - it's already active.
So to get the entire document content in a string
Dim mainData As String
mainData = ActiveDocument.Content.Text
where Content returns the entire main body's text as a Range object.
Note: The MsgBox has an upper character limit. If you're working with long text strings and want to see what they hold the following has more (but not "infinite") capacity:
Debug.Print mainData
All you need is:
Dim mainData As String
mainData = ActiveDocument.Range.Text

Relative path to images in access reports

It is my first time building a database and I wanted to share a solution to a problem I encountered.
My problem was that I wanted to show different images for each record in a report, but I also wanted to be able to move the database. This was a problem. I search in all the forums and all the different solutions didn’t work. I also found an article written by Microsoft saying that the only way is to either store the full path to the images or to store the image in the database. But this causes a problem if the database is moved, or storing the images in the database will take up a lot of storage space.
The problem is that the codes doesn’t work for each record in the report, the codes are for the entire report. So writing codes to find the backend and the image folder would result in displaying the first image in the report for all the records in that report.
However I discovered, when only storing the name of the image in a table, it would sometimes work (but it shouldn’t have, because I didn’t have the path) but when I restarted the database it would stop working. Investigating further I discovered that whenever you open the file browser it will store the path in some kind of memory. As long as the path to the images is stored in the memory it will be able to link the images to the path.
So my solution…
When the form, from where you access the reports is opened, the file browser is opened and the path to the images is pasted in (using codes to find backend and the image folder) and then the browser is closed. And this creates a link to the image names (stored in a table) with the path. And each different images will be shows for each different records in the report.
Not a pretty solution. Whenever the form is opened, you will see a flash of the file browser. But it gets the job done.
In the load form event:
`' this will find the backend and the image folder:
Dim filepath As String
Dim strBackEndPath As String
Dim lenPath As Integer
Dim i As Integer
Dim j As Integer
strBackEndPath = CurrentDb.TableDefs("yourTabeInBackend").Connect
j = InStrRev(strBackEndPath, "=") + 1
strBackEndPath = Mid(strBackEndPath, j)
BackPath = Left(strBackEndPath, InStrRev(strBackEndPath, "\"))
filepath = BackPath & "YourImageFolder\"
'this will open the folder browser and paste in the path and close it:
Dim f As Object
Set f = Application.FileDialog(msoFileDialogFolderPicker)
Dim varFile As Variant
Dim strPath As String
Dim fileName As String
With f
.InitialFileName = (filepath)
.AllowMultiSelect = False
SendKeys "{ESC}", True
f.Show
For Each varFile In .SelectedItems
Next varFile
End With
`
You can move the pictures to a subfolder of the folder of your database.
Then save the pictures' names like this:
Picture1.jpg
Picture2.jpg
etc.
When you run the application, obtain the path to the pictures:
PictureFolder = CurrentProject.Path & "\FolderName\"
Then the path to a picture will be:
PictureFolder & Me!PictureFileName.Value
When you "move" your database, move both the database file and the folder with the picture files with it.
yup, i just encountered same problem and i agree with what Richard_Ha said, but in my case i solve it with storing image path on textbox and its work..
first textbox name "fileimage_txt" and bound to list of image filename on table
second textbox name "image_path" with property
Source Control : =GetImagePath()
visible : No (if u dont want it get printed)
and image control with property
Source Control : =[path_txt] & [fileimage_txt]

Navigating to specific place in XPS document viewer VB.net

I've spent 2 days trying to find the solution with no luck.
I have a XPS file which I am displaying in a document viewer, and what I want to do, is use a variable that I have, to navigate to a certain place in the document within the viewer.
Firstly, how would I set some sort of identifier within the xps document? At the moment, I have got bookmarks set in it from word and then converted to xps, which I can see in the FixedDoc file, therefore I know it can see them, but I don't know how to the utilise this.
At the moment I have:
Dim _XpsPackage As XpsDocument
_XpsPackage = New XpsDocument(xpsFilename, IO.FileAccess.Read)
docViewer.Document = _XpsPackage.GetFixedDocumentSequence
Dim _CurrentDocSection() As String = Split(_CurrentWindow.Title, ".", 2)
Dim docSeq As IXpsFixedDocumentSequenceReader = _XpsPackage.FixedDocumentSequenceReader
Dim doc As IXpsFixedDocumentReader = docSeq.FixedDocuments(0)
Dim a = doc.Uri
From here, I want to use _CurrentDocSection(1), as my identifier and then navigate to where that bookmark is within the document, but cannot seem to find it.
Thanks

VB.NET - Working with tab delimited text file

I need help with how to work on text file (like database).
I create excel GUI (with macro's), that search imputed string in sheets with lots of data and display entire row with matching string (for people with installed MS office)
Now I must create alternative VB.Net application working only on tab delimited text files (without ADO.Net) for people who haven't installed MS office, and I don't know how start to work with it.
import them? if yes, then how.
working directly on them? if yes, then how.
My text files is exported excels files/sheets to tab delimited .txt, with loots of columns (100+) with headers, and lots of rows 500+
need help :)
thx
If you want to get the headers from the first line of the file then do this ...
Sub Main()
Dim dt = New DataTable
Dim lines = File.ReadAllLines("TextFile1.txt")
Dim headers = lines(0).Split(vbTab)
For Each header In headers
dt.Columns.Add(header)
Next
For Each line In lines.Skip(1)
Dim parts = line.Split(vbTab)
dt.Rows.Add(parts)
Next
End Sub