Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Word VBA - HTML fragment, paste to a content control

Posted on 2011-02-17
9
914 Views
Last Modified: 2012-06-22
Following on from another q

Not my code but what happens is we have a fragment of HTML and this is placed on the clipboard using windows functions

            CopyMemory ByVal lpData, ByVal sData, Len(sData)
            GlobalUnlock hMemHandle
            EmptyClipboard
            SetClipboardData m_cfHTMLClipFormat, hMemHandle

The fragment is in sData and is still correct. It is copied to memory then SetClipboardData puts it on the clipboard.

The clipboard contents are pasted to the content control using
Selection.PasteSpecial , , , , WdPasteDataType.wdPasteHTML

The problem is that accented characters in the html fragment are getting converted to something else (UTF-8 to something else I guess)

Any idea how to fix what I am doing or another way to approach it?

TIA

I'll have to post another question I guess, but that is where I am
0
Comment
Question by:Philip Pinnell
  • 6
  • 2
9 Comments
 
LVL 10

Expert Comment

by:Horn E. Towed
ID: 34918185
I looked at your previous question and the first thing that came to mind is that the accented characters are not part of the English alphabet.

Do you have the proper language ad-on installed for the non-English characters?
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34924821
I can transfer the accented character from the file to word doc correctly if I leave the html tags as is So I can get <b>Telefónica</b> so there is no problem with these characters per se.

Because the code is trying to translate the html tags into Word styles by putting the fragment on the clipboard then using a Word paste as HTML it is here that the mess up is occurring.

As I say it was not written by me and it uses the Windows functions CopyMemory  and SetClipboardData  to get the fragment onto the clipboard. It seems to be in this step the problem lies.
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34925159
Code that puts the fragment on the cloipboard
Public Sub PutHTMLClipboard(sHtmlFragment As String, _
   Optional sContextStart As String = "<HTML><BODY>", _
   Optional sContextEnd As String = "</BODY></HTML>")
   
   Dim sData As String
   
   If RegisterCF = 0 Then Exit Sub
   
   'Add the starting and ending tags for the HTML fragment
   sContextStart = sContextStart & "<!--StartFragment -->"
   sContextEnd = "<!--EndFragment -->" & sContextEnd
   
   'Build the HTML given the description, the fragment and the context.
   'And, replace the offset place holders in the description with values
   'for the offsets of StartHMTL, EndHTML, StartFragment and EndFragment.
   sData = m_sDescription & sContextStart & sHtmlFragment & sContextEnd
   sData = Replace(sData, "aaaaaaaaaa", _
                   Format(Len(m_sDescription), "0000000000"))
   sData = Replace(sData, "bbbbbbbbbb", Format(Len(sData), "0000000000"))
   sData = Replace(sData, "cccccccccc", Format(Len(m_sDescription & _
                   sContextStart), "0000000000"))
   sData = Replace(sData, "dddddddddd", Format(Len(m_sDescription & _
                   sContextStart & sHtmlFragment), "0000000000"))

   'Add the HTML code to the clipboard
   If CBool(OpenClipboard(0)) Then
   
      Dim hMemHandle As Long, lpData As Long
      
      hMemHandle = GlobalAlloc(0, Len(sData) + 10)
      
      If CBool(hMemHandle) Then
               
         lpData = GlobalLock(hMemHandle)
         If lpData <> 0 Then
            
            CopyMemory ByVal lpData, ByVal sData, Len(sData)
            GlobalUnlock hMemHandle
            EmptyClipboard
            SetClipboardData m_cfHTMLClipFormat, hMemHandle
                        
         End If
      
      End If
   
      Call CloseClipboard
   End If

End Sub

Open in new window

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34925254
How can we get the data that you are using? Is it from a publicly available site?
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34925765
The data is just a text file

Attached is a cut down example.

The macro should read the text file and put the content in to a word document and 'translate' the HTML tags into Word styles. Ovbviously there could be any HTML tags.

The process works ok except that some or accented text is garbled ie Telefónica -> Telefónica

EEUpload.txt
0
 
LVL 13

Accepted Solution

by:
Philip Pinnell earned 0 total points
ID: 34961195
Part of the problem here was reading the input file

We found we needed an encoding parm

Set doc = Documents.Open(FileName:=sFile, Format:=wdOpenFormatUnicodeText, Encoding:=1252)

This still did not work for all accented characters so we moved away from using the clipboard and wrote the chunks to a temp.html file then did a

...range.insertfile("temp.html")
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34961275
That makes more sense. I got a bit lost in your clipboard code, and didn't understand why you were using it.

Unless you need to copy formatting, you can simply set the text property of one range to that of another.

myContentControl.Range.Text = doc.Range.Text



0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34961301
Yeah the input was HTML fragments
0
 
LVL 13

Author Closing Comment

by:Philip Pinnell
ID: 34995348
No solution found.

Workaround detailed here ie "what we did"
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question