Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Word VBA - HTML fragment, paste to a content control

Posted on 2011-02-17
9
Medium Priority
?
971 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
[X]
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
  • 6
  • 2
9 Comments
 
LVL 10

Expert Comment

by:Prester John
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 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