Looking to Create a Macro for Printing Addresses to Envelopes - vba

First question on here, so hopefully I have followed all the guidelines etc.
I am looking to create a macro for a Word document which prints a headed paper copy, a yellow paper copy and an envelope (based on the address section of the letter). I have the paper copies sorted but I am having difficulty with the envelope.
My understanding is I should be able to use a field, where the address would be typed in and then use the field in the macro to print the envelope out. I have the envelope printing side of this sorted too, in that I can get an envelope to print from the document, just not get it to print only the address.
Whenever I use fields it does not seem to allow me to type into it, it just deletes it out. I have never used Fields before, so this may be something very simple I am missing. Based on some Youtube videos I watched it looks like MacroButton is the field I need to be using?
If anyone was able to assist me in how to use fields for this it would be greatly appreciated.
Currently my code only includes the printing side of things (envelope currently not included):
Sub OrCo()
'
' OrCo Macro
'
'
With ActiveDocument.PageSetup
.FirstPageTray = 259
.OtherPagesTray = 259
End With
Application.PrintOut FileName = "", Range:=wdPrintAllDocument, _
Copies:=1, Pages:="", PageType:=wdPrintAllPages, Collate:=True,
PrintToFile:=False
With ActiveDocument.PageSetup
.FirstPageTray = 260
.OtherPagesTray = 260
End With
Application.PrintOut FileName = "", Range:=wdPrintAllDocument, _
Copies:=1, Pages:="", PageType:=wdPrintAllPages, Collate:=True,
PrintToFile:=False
End Sub

You probably want to look at the ask field as this field links the entered text to a bookmark which makes it easier to access the entered text from vba.
MS help for the Ask field
This if you have an ask field of the form
{ask street "Enter the street name"}
then from VBA you would be able to access the text using
activedocument.bookmarks("street").range.text
NOTE: The ask field does not display the entered text. To do that you need to add a separate ref field (or field containing just the bookmark name). This allows you to put the ask fields anywhere you like and use multiple ref fields to display the result.
You may also want to review the envelope printout method.
MS help for the envelope printout method
Good luck with your endeavors.

Related

Combining Rich Text Content Control Content in MS Word using VBA

I'm trying to create a form for a non-technical user in MS Word to capture some text content in MS Word. This word doc consists of several rich text content controls where the user will type in or paste in some formatted data (bold, underlined, links, ...).
Once they get all the content entered into these various content controls I'm trying to make it easy for them to combine them together to paste in a consistent order into some podcast show notes which is in an HTML form.
So basically, I want to take three rich text content controls that have formatted data in them, combine them together into one formatted piece of content, and then copy it to the clipboard so they can then go to this web form, paste it in, and do some minor cleanup. The problem is that whenever I try to combine the RTF content it loses the formatting.
The only way I seem to be able to keep the formatting is if I copy the range object and then paste it. However, this doesn't paste just the formatted text. It pastes the whole rich text content control.
I've tried creating a blank RTF field at the bottom of the Word doc to combine everything in but I just can't figure it out. I wouldn't think this would be rocket science.
Being none of the code I've tried works and keeps the formatting I"m not sure if posting it here will do any good. Here's how I'm getting the value of the text object:
ActiveDocument.SelectContentControlsByTitle("txtShowNotes").Item(1).Range.Text
tried this:
ActiveDocument.SelectContentControlsByTitle("txtShowNotes").Item(1).Range.Copy
ActiveDocument.SelectContentControlsByTitle("txtCombinedContentSection").Item(1).Range.Paste
but this copies the whole RTF and not just the text.
Try something based on:
Sub Demo()
Dim Rng As Range
With ActiveDocument
Set Rng = .SelectContentControlsByTitle("txtCombinedContentSection").Item(1).Range
Rng.FormattedText = _
.SelectContentControlsByTitle("txtShowNotes").Item(1).Range.FormattedText
Rng.InsertAfter vbCr & vbCr
Rng.Characters.Last.FormattedText = _
.SelectContentControlsByTitle("txtShowNotes").Item(2).Range.FormattedText
End With
End Sub

Wrong barcode exported from Visual Basic Macro in MS Word to PDF

I am trying to export through a Macro in MS Word a String that has 4 numbers. The whole Macro runs just fine, but, when I open the resulting PDF, I see that the barcode displayed seems corrupt.
This is the result:
In the Macro, I select the text I want to format, and change the font to "Free 3 of 9 Extended".
I have tried wrapping this number with "*" characters without success. Also tried "!". None of this seems to work. The funny part is that, if I open a Word document and type the same numbers using the same font, a clear Barcode is displayed:
This is what I see when writing directly in MS Word the same characters using the same font,this is what I see (which is what I want to achieve in the PDF export).
My macro exports to PDF with the following code:
Public Function guardar(id As String) As String
Dim path As String
guardar = id
obj_Word.ActiveDocument.ExportAsFixedFormat OutputFileName:=guardar,
OptimizeFor:=wdExportOptimizeForPrint, UseISO19005_1:=True,
IncludeDocProps:=True, KeepIRM:=True, ExportFormat:=wdExportFormatPDF
End Function
Is it possible that the template you are given is setting the font weight to bold in that portion of the document in which you are introducing the barcode, thus modifying the way it is displayed?
I cannot think of any other reason. The code you are posting does not seem to be the culprit.

MS Access | How to get content from mail body to table?

I am working on a project where I need to get ms access table/data from mail body and execute some command in SAP. I can manage SAP part but issue is that how to get information from mail body. i tried linking my mailbox in access but it shows me all mail body text but i need some specified contents only. example
Hello,
Please supplement budget
WBS Amt
N.10002077.001 1
from above what i need is just "N.10002077.001" and "1" ,but how to get that information only in table is the issue?
Further, what I will get in my mail will be table with 2 column but access imports it as a simple text.
It is impossible to give a definite answer to your question because it is too vague but it is possible to get you started.
Have a look at this answer of mine: https://stackoverflow.com/a/12146315/973283. The question is not relevant other than the OP did not understand that showing screenshots told us little about what the body looked like to a VBA macro. The answer includes a macro that copies selected properties from every email in Inbox to an Excel worksheet. This will allow you to see what an email’s body looks like to a VBA macro.
How will you identify the emails from which you wish to extract data? The two simple choices are:
Look at every email in a folder and identify the interesting one by examining the subject, sender or some other property.
Select the interesting emails then run a macro which uses ActiveExplorer to access the selected emails.
The answer referenced above demonstrates technique 1. There are lots of answers demonstrating technique 2 but I can add an example macro if necessary.
An email typically has an Html body and a text body. If an email has an Html body, that is the one shown to the user. A macro can access either or both. Your screen shot looks like a text body although appearances can be deceptive. If it is a text body, the email does not have an Html body.
If it is a text body, the layout of the body is probably something like:
Hello,{cr}{lf}
Please supplement budget{cr}{lf}
WBS{tab}{tab}{tab}{tab}{tab}Amt{cr}{lf}
N.10002077.001{tab}{tab}1{cr}{lf}
This assumes, the sender has used variable numbers of tabs to line up the columns.
You could use Split on vbCr & vbLf to convert the string body into an array of strings with one line per array entry. Discard lines up to and including the line starting “WBS” then process each line down to any signature. Split each line on vbTab and expect to find two entries with values with the rest blank.
See how far you can get with the above hints then clarify your answer if you need more information.

My vb.net code just replaces specified string by contents of rich text box. But I want to replace it with alignment also

I need to replace a specific string in a MS Word with text of Rich Text Box. I have achieved my that using the following code.
objDoc.Content.Find.Execute(FindText:="Comments1", _
ReplaceWith:=COMMENTS.Text, _
Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
But, What my actual requirement is.. I want to replace it by exact text of rich text box with alignment.
For example :
Kindly add the title to you article,
Kindly add the abstract to your article
the above text is the content of rich text box.
But, in my word document it replaced as
Kindly add the title to you article, 2. Kindly add the abstract to your article
Have you noticed that ? After the 1st point I have hit enter key and then only I have give 2nd point.
But, resultant text are concatenated with the 1st point.
So, what to do to get exact text with alignment of rich text box in my word document.
I seem to recall something about Rich Text Box using vbLf for line feeds instead of vbCrLf, which I believe is what MS Word would expect to be used for a line feed. You might try something like this (air code):
objDoc.Content.Find.Execute(FindText:="Comments1", _
ReplaceWith:=COMMENTS.Text.Replace(vbLf, vbCrLf), _
Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

Formatting text from Mulitline text box in word with VBA

I'm putting together a template in Word, using a form for the user to fill in to then populate some of the document.
The bit I'm currently stuck on is at the end of the document, where the cc's are listed.
The form has a multiline text box into which the user puts in their cc's, one per line.
I then want to add to the end of the document the contents of the text box, but in the right format. Specifically, it should look like:
cc: First CC contact
Second CC contact
so on and so forth
I attempted to do this using 2 bookmarks, so my code currently is:
' If 'CC' box has content, add it
If doc_CC.TextLength > 0 Then
.Bookmarks("CC").Range.Text = vbCr + "cc:"
.Bookmarks("CCs").Range.Paragraphs.Indent
.Bookmarks("CCs").Range.Text = doc_CC + vbCr
End If
However, when this is run, on the page it looks like:
cc: first contact
second contact
and so on
Realise that the 2 bookmark method is a bit messy but it seemed like a good idea at the time - obviously this is not the case! Have done some searching for a way to do it with Split but am not making much progress down this path - suspect I'm googling for the wrong thing.
How do I do this so that the formatting is as desired? Any help is greatly appreciated.
Try inserting a tab character? + Chr(9) or even + vbTab may work.
Have found a work around which, while doesn't answer the actual question of how to do it, does produce a result to the same effect.
Have used a 2 column table without no lines instead with contents of a1 being "cc:" and contents of a2 being whatever was entered into the multiline text box. If there is nothing in the text box, then the table is deleted.
I'll keep on eye on this question though so if some one does have the proper answer I can mark it accordingly.
Another possibility would be to format the cc paragraph with a hanging indent (like is used for bullets or numbering). Use a newline character - Chr(11) - instead of vbcr to separate each entry. The text should all line up,then...