#1
|
|||
|
|||
Filling data with XML
Hello,
I'm stuck starting a development VBA Macro for Word. I want to know the best approach for this dev: 1 - I want to fill some data in a word file. These data type can be a scalar value, an image (and maybe later a table). 2 - All data are stored in an XML file, like this one : Code:
<?xml version="1.0" encoding="UTF-8"?> <results> <result version="1.0"> <scalar name="x" value="789" unit="mm" id="0010214"/> <vector name="v" value="0,1,2,3,4,5" id="6164201"/> <image name="graph" path="img/v1/graph.png" id="5431054"/> </result> <result version="2.0"> <scalar name="x" value="450" unit="mm" id="0010214"/> <vector name="v" value="8,8,2,3,7,5" id="6164201"/> <image name="graph" path="img/v2/graph.png" id="5431054"/> </result> </results> 4 - Every data is more complicate than just a value (unit, etc.). It will be absolutely perfect to store an associated VBA Object.... Thanks for helping me to find the best direction ! |
#2
|
||||
|
||||
You are using nodes and attributes rather than simply nodes but the end game is essentially the same. I'm not sure if English is your first language but I didn't quite understand what you are asking.
Do you need to know how to embed the xml file into a document or are you asking how to make the attribute editable inside the Word document? Are you doing this purely with VBA or do you want to use the Word GUI to achieve the end game? Note that the 'image' file type is actually a string. You won't be able to include a PNG image in a text only format which is necessary for xml.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Sorry, English is not my first language...
The goal is to insert datas (stored in an xml file) in the document with a userform. Data should not be editable in the document ! The only way to change the data is to use a new version of the associated node in the xml file. In my actual development, i can read an xml file and generate the treeview in the userform. But i don't know how to insert this data in the document while keeping a link with this data for future update with a new version. By the way, at the word closure i'm only able to store the xml path (ActiveDocument.Variables.Add) rather than the xml object... I hope it will clarify my needs. |
#4
|
||||
|
||||
OK,
There are two ways to do what you are asking: 1. Use a VBA userform to query the external xml file source and then display the form. The code can then write that info to either bookmarked ranges, document properties or unlinked Content Controls. 2. Embed the xml file inside the Word document itself and use linked Content Controls to display this information automatically. If you update the xml source content, you could use a macro to overwrite the embedded xml file inside the Word document. My preference is to choose option 2. With that option, there is no need to also do the VBA Userform as you can edit the attributes in the embedded XML via the Content Controls or replace the xml if the source changes.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Quote:
After running the code, a "Red Square" will appear in the document Code:
Sub Add_Picture() Dim oCC As Word.ContentControl Dim oCustomPart As Office.CustomXMLPart Set oCC = ActiveDocument.ContentControls.Add(wdContentControlPicture) Set oCustomPart = ActiveDocument.CustomXMLParts.Add("<ccMap><ccData>iVBORw0KGgoAAAANSUhEUgAAADAAAAAmCAIAAAAN749WAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAScwAAEnMBjCK5BwAAAHNJREFUWEftmLEJwDAMBJU9Umb/zbKDE1BjjLkukeCMum+e86nRcZ9XlHpPoVITpdq832UhMKQPoW82bxVmS8hC6ZaEZhV0iFZDQhIiApTrkISIAOU6JCEiQLkOSYgIUK5DEiIClHd26K8zTZ/rh4Ty2DAAkMHKNHSz0cwAAAAASUVORK5CYII=</ccData></ccMap>") With oCC .Title = "Picture" .XMLMapping.SetMapping "/ccMap/ccData[1]" End With Exit Sub End Sub |
#6
|
||||
|
||||
Boris_R
Thanks for the correction. Do you have an automated way to create the XML strings for charts created in other tools such as Excel or from screen captures? What method would you use to create these strings from a PNG filepath?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
Quote:
These content controls are linked to the same node Insert your picture into the "Picture" content control. The string you are interested in will be in the Plain Text content control Code:
Sub Two_Mapped_CC() ' Dim oCC As Word.ContentControl Dim oCustomPart As Office.CustomXMLPart Dim orng As Range Set oCustomPart = ActiveDocument.CustomXMLParts.Add("<ccMap><ccData></ccData></ccMap>") Set oCC = ActiveDocument.ContentControls.Add(wdContentControlPicture) With oCC .Title = "Picture" .XMLMapping.SetMapping "/ccMap/ccData[1]" End With Set orng = oCC.Range.Paragraphs(1).Range With orng .InsertParagraphAfter .InsertParagraphAfter .MoveEnd Unit:=wdCharacter, Count:=2 .Start = orng.End End With Set oCC = orng.ContentControls.Add(wdContentControlText) With oCC .Title = "Str" .XMLMapping.SetMapping "/ccMap/ccData[1]" End With Set oCC = Nothing Set oCustomPart = Nothing Set orng = Nothing Exit Sub End Sub |
#8
|
||||
|
||||
Yep, that would do it.
Nice work.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
Andrew, Boris
With my CC Tools Add-In Content Control Tools, you can view the node value with the Advanced mapping tab: |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Filling in cells based on 2 other cells data | Dawindle | Excel | 4 | 11-10-2018 07:40 AM |
filling in blank cells with data from cell above | RayK | Excel | 1 | 01-12-2017 04:14 PM |
Wierd... "cell" started auto filling with data from other cells I double click on. How stop it?! | KerryOn | Excel | 2 | 08-10-2015 08:41 PM |
matching data from one work sheet to another, then auto filling missing cell information | timomaha | Excel | 1 | 09-12-2014 07:51 AM |
New to Excel - Filling out data | The Accountant | Excel | 2 | 12-16-2013 12:32 PM |