Adding and Setting Text to Content Controls within Repeating Section Content Control - vba

I have a word template that is filled in with information from an Access database and part of that template includes a table with two cells, type and location, for all attachments for that specific document. Within those fields are content controls, name accordingly, and then the whole table is wrapped in a repeating section content control titled "Attachments".
For each specific document there is an attachments folder that I iterate through to get the attachment type and attachment filepath. I would like to add this data to the Attachments content controls but am having issues with displaying the information correctly.
Private Sub AttachmentControl(wrdApp As Word.Application, wrdDoc As Word.Document, attachmentsFolder As String)
Dim fso As New Scripting.FileSystemObject, folderAttachments As Files, attachment As file, i As Long
Set folderAttachments = fso.GetFolder(attachmentsFolder).Files
For Each attachment In folderAttachments
If attachment.Type <> "Data Base File" Then
wrdDoc.SelectContentControlsByTag("Type").Item(1).Range.Text = attachment.Type
wrdDoc.SelectContentControlsByTag("Location").Item(1).Range.Text = attachment.Path
wrdDoc.SelectContentControlsByTag("Attachments").Item(1).RepeatingSectionItems.Item(1).InsertItemBefore
End If
Next attachment
End Sub
The attachmentFolder is where the attachments I want to iterate through are located. For my testing purposes the files within this folder are 6 jpg images named Image 1 through Image 6 but in real life applications will contain more attachments than just images.
Expected Result
Actual Result
Is this a problem with my code or am I just not correct in how this should work?

Related

VBA for Word - UNC Paths

I have a Word document that uses VBA forms to generate the document content based on selection boxes. The VBA copies and pastes from other word documents into the main document. I use explicit paths to specify where it should go find the word documents.
It would be nice to use UNC paths instead, to remove some of the "explicitness" of the file locations (c:\Files\Example\Content\PROFILE.docx) and maybe replace it with something like "..\Content\Profile.docx "
Example of the code:
Sub GenerateProfile()
Dim currentPathProfile As String
currentPathProfileText = ActiveDocument.Path
currentPathProfileText = currentPathProfileText & "c:\Files\Example\Content\PROFILE.docx"
Documents.Open FileName:=currentPathProfileText
Dim currentPathProfileDoc As Document
Set currentPathProfileDoc = Documents(1)
currentPathProfileDoc.Activate
Call CopyWholeContent
currentPathProfileDoc.Close
Call PasteWholeContent
End Sub
If the other word documents are in a relative path position to the template holding the code you can do it. It is easier if they are in a subfolder. Generate the path of ThisDocument (your code holder) and use that as a base.
ThisDocument.Path
However, rather than pull from documents, why not have the text stored in your template in the form of AutoText and use that instead? If the information is needed in more than one template, it can be stored in a global template as AutoText. This is much more flexible and less prone to problems, IMO.

Why do Embedded Image Sizes Evaluate to 0 in Outlook VSTO Add-In?

We are building a VSTO Outlook Add-In that scans an outgoing mail message for attachments to alert users and noticed some unexpected behavior.
Considering the following ways of adding a file to an Outlook mail message:
A file attachment
Screen Shot
Screen Clipping
Mail Signature file
All four are recognized as attachments when the Item Send event fires:
Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
In the following code example:
For Each attachment As Outlook.Attachment In Item.Attachments
'do some stuff like check attachment size
Next
We are checking for small embedded images in a signature file that don't want to notify the user of.
In the following cases:
Screen Shot
Screen Clipping
Mail Signature file
We've noticed that when the added files are embedded images (not attachments), we don't see a correct size property for the image using:
attachment.Size
IE: Say we are sending an Outlook email that has:
One Attachment.
One Screen Shot.
One Signature File with one image in it.
Our code seems to recognize the correct number of attachments, however if we check the attachment size for a screen shot or a signature file image, the attachment size property always evaluates to 0, which we're think is due to the fact that the file doesn't exist on disk and the attached file does.
For Each attachment As Outlook.Attachment In Item.Attachments
if attachment.size > 755 then
'ignore the image
end if
Next
Is there a way to check the image size in VB.Net or do we need to save the file off to a temp directory in order to do this?
EDIT
Outlook Spy Troubleshooting steps:
New Mail Message
Inserted screen shot and signature file:
OutlookSpy->IMessage
IMessage window blank (below)
Close IMessage window.
Re-Open IMessage Window
Inserted (attached) files appear (below)
8. Double Clicked on attachment
Selected Inspector Button
Current Item:
Browse:
Attachments:
Browse:
IEnumVariant:
I suspect that the differences between steps 4 and 7 may be due to the fact that Outlook may have saved a draft of the email message?
ADDITIONAL EDIT
Code added to save mail message before checking signature/embedded image size:
'convert generic object to Outlook.MailItem object.
Dim objMailItem As Outlook.MailItem = CType(Item, Outlook.MailItem)
'Save message
objMailItem.Save()
'quick check to see if message is saved (it is)
Dim saved As Boolean = objMailItem.Saved()
For Each attachment As Outlook.Attachment In objMailItem.Attachments
'all items still evaluate to 0.
If attachment.Size >= 20 Then
'do some stuff
End If
Next
Thanks.
Attachment size (which includes raw binary data as well as the per-attachment properties) is updated by the store provider when the mesage is saved. Make sure you call MailItem.Save first.
We ended up using Outlook Spy (awesome tool..) to find the PR_ATTACH_SIZE property:
Then set up using MS schema as follows:
'property access to get attachment sizes
Const PR_ATTACH_SIZE As String = "http://schemas.microsoft.com/mapi/proptag/0x0E200003"
Also great info from this SO Post.
Then iterated through our attachment collection as follows to find our attachment sizes:
For Each attachment As Outlook.Attachment In Item.Attachments
attSize = CType(attachment.PropertyAccessor.GetProperty(PR_ATTACH_SIZE), Integer)
if attSize.size > 755 then
'ignore the image
end if
Next
Thanks..

Executing mailmerge removes all bookmarks. What's the alternative?

So my challange is this.
I've created a Word Macro Enabled Template (MS Word 2016) which creates a document using the MailMerge function using data from an Excel file which colleagues complete.
Once completed, they add in their signature - a scanned jpeg file of their signature from a central network.
My code worked on the principle of attaching to a bookmark, unbeknown to me that when executing a MailMerge, it removes all bookmarks.
I am thinking that if I can change my code after ActiveDocument so that instead of looking for Bookmark it looks for specific text, I can then insert this text in the specific place in the Word template so when the MailMerge is executed it places their signature in that specific [text] place.
Is this possible?
Thanks in advance.
My code is
Sub CurrentUserSignature()
Dim folder As String
folder = "C:\\MacroTemp\\"
Dim path As String
path = folder & Application.UserName & ".jpg"
Dim shape As InlineShape
Set shape = ActiveDocument.Bookmarks("Signature").Range.InlineShapes.AddPicture(path, False, True)
With shape
.LockAspectRatio = msoTrue
.Height = CentimetersToPoints(4.3)
End With
End Sub
`
You could simply insert a borderless single-cell table, a Rich Text Content Control, or a Picture Content Control at the desired location in the mailmerge main document, then access each of those in the output document. If you use a borderless single-cell table, you can give it a fixed height and width so the inserted image's size is automatically constrained to fit.
Where multiple output documents are being produced, each to be signed by the same person, you might do better to insert the signature into the mailmerge main document before execution, then close that without saving changes post-merge.
Alternatively, especially if different records require different signatures and the relevant details can be gleaned from the data source, you can add field coding to the mailmerge main document to automatically add the signature images. See Managing Mailmerge Graphics in the Mailmerge Tips and Tricks thread at:
http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
or:
http://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks

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]

Issue with pdf docs not showing up

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.