Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-17-2020, 06:53 AM
jocelyn jocelyn is offline Filling data with XML Windows 7 64bit Filling data with XML Office 2010
Novice
Filling data with XML
 
Join Date: Jun 2020
Posts: 2
jocelyn is on a distinguished road
Talking 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>
3 - The tricky part for me is to properly link a data with an XML node. Because I want to update all data with the last version result.


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 !
Reply With Quote
  #2  
Old 06-17-2020, 08:24 PM
Guessed's Avatar
Guessed Guessed is offline Filling data with XML Windows 10 Filling data with XML Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 06-17-2020, 11:39 PM
jocelyn jocelyn is offline Filling data with XML Windows 7 64bit Filling data with XML Office 2010
Novice
Filling data with XML
 
Join Date: Jun 2020
Posts: 2
jocelyn is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 06-18-2020, 01:47 AM
Guessed's Avatar
Guessed Guessed is offline Filling data with XML Windows 10 Filling data with XML Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 08-17-2020, 06:50 AM
Boris_R Boris_R is offline Filling data with XML Windows Vista Filling data with XML Office 2010 32bit
Novice
 
Join Date: Oct 2015
Posts: 5
Boris_R is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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.
Here is some sample code to insert an image using XML mapping.
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
Reply With Quote
  #6  
Old 08-17-2020, 04:06 PM
Guessed's Avatar
Guessed Guessed is offline Filling data with XML Windows 10 Filling data with XML Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 08-18-2020, 04:03 AM
Boris_R Boris_R is offline Filling data with XML Windows Vista Filling data with XML Office 2010 32bit
Novice
 
Join Date: Oct 2015
Posts: 5
Boris_R is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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?
After running the macro, two mapped content controls will appear in your document.
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
Reply With Quote
  #8  
Old 08-18-2020, 06:12 AM
Guessed's Avatar
Guessed Guessed is offline Filling data with XML Windows 10 Filling data with XML Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Yep, that would do it.
Nice work.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #9  
Old 08-18-2020, 08:21 AM
gmaxey gmaxey is offline Filling data with XML Windows 10 Filling data with XML Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Andrew, Boris


With my CC Tools Add-In Content Control Tools, you can view the node value with the Advanced mapping tab:


Attached Images
File Type: jpg Img.jpg (115.9 KB, 17 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

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
Filling data with XML matching data from one work sheet to another, then auto filling missing cell information timomaha Excel 1 09-12-2014 07:51 AM
Filling data with XML New to Excel - Filling out data The Accountant Excel 2 12-16-2013 12:32 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft