Combining Rich Text Content Control Content in MS Word using VBA - 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

Related

Efficiently copy text from Excel to Word, hyperlink if applicable

What I'm trying to do
I'm trying to copy contents of different Excel cells into their respective Word bookmarks. Then this filled-in Word document gets pasted into an Outlook email. If the cell has a URL in it, I want it to be a clickable hyperlink in the email.
The problem
If the cell's value was a URL, then while it was un-hyperlinked before sending the email, it appeared hyperlinked (for some people) when the email was received. However, others users saw what looked like a clickable link (blue and underlined), but were unable to click because it wasn't actually hyperlinked. My guess is that certain mail programs recognize the URL and automatically hyperlink it while others do not.
What I've tried
I figured the solution was to make sure the URL was hyperlinked before sending. I tried a few forms of .PasteSpecial and .PasteAndFormat with the URL already hyperlinked in Excel. I got close, but either got an unwanted carriage return or no hyperlink. My latest try is a 2-line hack:
'put the cell contents in (sans formatting)
BKMRange.Text = ActiveCell.Value
'If it looks like a hyperlink, then hyperlink it
If UCase(Left(ActiveCell.Value, 4)) = "HTTP" Or UCase(Left(ActiveCell.Value, 4)) = "WWW." Then TempEmailDoc.Hyperlinks.Add BKMRange, ActiveCell.Value
The problem with this is that if the cell is more than just a URL (e.g. "This is your URL: www.google.com"), the URL won't get hyperlinked, since the first 4 characters are not "HTTP" or "WWW.". I could do a more robust search for "HTTP" or "WWW.", but then the code becomes much more complicated. There must be a more efficient way.
My question
How can I efficiently copy cell contents, but preserve hyperlinks if the cell contains them?
Thanks!
You can use the cell's Hyperlinks collection to learn whther the cell contains a hyperlink:
Sub test()
Dim i
i = Selection.Hyperlinks.Count
If (i > 0) Then MsgBox Selection.Hyperlinks(1).Address
End Sub
Now that you know a cell is (contains) a hyperlink, you can insert it as a hyperlink in Outlook. I assume you work in Outlook VBA and open the worksheets in Outlook VBA.
Note that when you enter a proper URL in Excel it will turn it automaticlly into a hyperlink.

MS Word Macro to replace all hyperlinks with a single word

I prepare a MS Word file with a large amount of news articles, each followed by a full, long clickable hyperlink (e.g. http://www.newssite.com/this-is-the-name-of-the-article.html).
I need to replace all the hyperlinks in the file with a single word: "Link", while keeping their url adresses and click functionality. I can use MS Word "change hyperlink" dialogue to do that, but doing it by hand takes incredible amount of time.
The hyperlinks, however, are not always formatted as 'hyperlink' style. Is there any condition for MS Word Macro replacement that will lookup hyperlinks by their functionality, not by their style or text?
Despite this task seems to be quite common, I could not find anything like it in the web.
Since you have the hyperlinks already created. It should just require looping through the documents hyperlinks collection and changing the 'TextToDisplay' property. This should get you started:
Public Sub ChangeHyperlinksText()
Dim hlink As Hyperlink
For Each hlink In ThisDocument.Hyperlinks
hlink.TextToDisplay = "Link"
Next hlink
End Sub

Paste text into a Word Document with a specific format

I'm working on a macro but I'm stuck at this point. I'm copying text from one word document to another, but I need this text to be pasted in a specific format, neither the source formatting, or it's destination's.
Is it possible to determine that the text I'll be pasting, gets pasted with a specific font, a specific size, and a specific color?
The easiest way to do this would be as follows:
Start recording a new macro.
Paste your text.
Select the pasted text.
Set the formatting (font, size, color) as you require.
Stop recording the macro.
Borrow heavily from the auto-generated selection and formatting code of the new macro for use in your original macro.

vb parse word from clipboard into html

I want to copy some word text with tables, links and images and paste it into a rich textbox in my vb project. Where I want to parse it to html.
The Questenion is, how can access the copied word in the clipboard. Using
My.Computer.Clipboard.GetText()
just returns text, without the structure for links, tables or images. But there have to be a way to access it, because my rich textbox seems to know the word format. When I paste it into it, tables, images and links are also displayed in there.
You can try to specify text format in GetText's parameter as follow :
Dim htmlText As String = Clipboard.GetText(TextDataFormat.Html)

automating word 2010 to generate docs

the webapp was already done on office2007 and i need to convert it so it'll work in office2010.
i was able to convert the header generator part of the code but i have problem with the body of the doc itself. the code copy the data from a "data" doc and paste it into the generated doc.
appword.activewindow.activepane.view.seekview = 0
'set appsel1 = appword.activewindow.selection
set appsel1 = appword.window(filepath).selection -that is the original one
appdoc1.bookmarks("b1").select
appword.selection.insertafter("some text")
appsel1.endkey(6) -the code stops here
appword.selection.insertafter("some other text")
the iexplorer debuger says ERROR:appsel1 object required. and when i view its data using the iexplorer debugger its data is "empty" instead of "{...}"
can anyone tell me what i'm doing wrong
if you need more of the code tell me.
From MSDN
After this method is applied, the selection expands to include the new
text.
If you use this method with a selection that refers to an entire
paragraph, the text is inserted after the ending paragraph mark (the
text will appear at the beginning of the next paragraph). To insert
text at the end of a paragraph, determine the ending point and
subtract 1 from this location (the paragraph mark is one character).
However, if the selection ends with a paragraph mark that also happens
to be the end of the document, Microsoft Word inserts the text before
the final paragraph mark rather than creating a new paragraph at the
end of the document.
Also, if the selection is a bookmark, Word inserts the specified
text but does not extend the selection or the bookmark to include the
new text.
So I suspect that you still have no selected text.
I wonder if you can do a Selection Collapse(wdCollapseStart) but that's just a thought.