U
Uwe
I'm still very new to this vba stuff....
So here is what I'm trying to do:
- read a file with file names of figures (png files),
- insert the files
- size them the figures
- add a caption string with style "Figures" after the figures
I've found most of the pieces, but now I'm running out of time. Maybe
someone could point me in the right direction.
The code below seems to handle the images fine and also inserts the
text, however, it does not set the style for the captions correctly,
i.e, the images also get the style "Figures". I want the custom style
"Figures" applied only to the text string and not the images.
Sub Macro4()
Dim FoundFile As Variant
Dim sFilename As Variant
Dim sname As String
Dim scaption As String
Dim s As InlineShape
' Function to get filename
sFilename = WordApplicationGetOpenFileName("*.txt", True, True)
With New Scripting.FileSystemObject
With .OpenTextFile(sFilename, ForReading)
Do Until .AtEndOfStream
sname = .ReadLine
scaption = sname
sname = sname + ".png"
Set s = Selection.InlineShapes.AddPicture
(FileName:=sname, LinkToFile:=False, _
SaveWithDocument:=True)
With s
.Height = 312.5
.Width = 442.1
End With
Selection.TypeText Chr(11)
Selection.Style = "Figures"
Selection.TypeText ("Figure ")
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, _
Text:="STYLEREF 2\s", PreserveFormatting:=True
Selection.TypeText (" - ")
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, _
Text:="SEQ Figure \*ARABIC \s 2",
PreserveFormatting:=True
Selection.TypeText (": " + scaption)
Loop
End With
End With
MsgBox "Done"
End Sub
So here is what I'm trying to do:
- read a file with file names of figures (png files),
- insert the files
- size them the figures
- add a caption string with style "Figures" after the figures
I've found most of the pieces, but now I'm running out of time. Maybe
someone could point me in the right direction.
The code below seems to handle the images fine and also inserts the
text, however, it does not set the style for the captions correctly,
i.e, the images also get the style "Figures". I want the custom style
"Figures" applied only to the text string and not the images.
Sub Macro4()
Dim FoundFile As Variant
Dim sFilename As Variant
Dim sname As String
Dim scaption As String
Dim s As InlineShape
' Function to get filename
sFilename = WordApplicationGetOpenFileName("*.txt", True, True)
With New Scripting.FileSystemObject
With .OpenTextFile(sFilename, ForReading)
Do Until .AtEndOfStream
sname = .ReadLine
scaption = sname
sname = sname + ".png"
Set s = Selection.InlineShapes.AddPicture
(FileName:=sname, LinkToFile:=False, _
SaveWithDocument:=True)
With s
.Height = 312.5
.Width = 442.1
End With
Selection.TypeText Chr(11)
Selection.Style = "Figures"
Selection.TypeText ("Figure ")
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, _
Text:="STYLEREF 2\s", PreserveFormatting:=True
Selection.TypeText (" - ")
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, _
Text:="SEQ Figure \*ARABIC \s 2",
PreserveFormatting:=True
Selection.TypeText (": " + scaption)
Loop
End With
End With
MsgBox "Done"
End Sub