PDA

View Full Version : [SOLVED] Cannot unlink word header



Texasthierry
02-15-2016, 08:10 AM
Hi all, excel is not my birth language and I didn't success to finish this VBA :banghead:.
From an excel (2010) database it does open 3 word sheets (2010), linked the data from excel, save the word in a different folder with specific name and unlinked them. It works fine except for the headers (I don't have footers), it doesn't unlink them. Any idea on how to make it happen ? Thanks for your help.


Sub Generate()

Dim objWord
Dim objDoc1
Dim objDoc2
Dim objDoc3
Dim WO As String
Dim family As String
Dim var As String

'Get infos for file name
Sheets("DATA").Select
WO = Sheets("DATA").Cells(7, 2).Value
SN = Sheets("DATA").Cells(11, 2).Value
family = Sheets("DATA").Cells(1, 11).Value
var = Sheets("DATA").Cells(6, 2).Value

Directory = "C:\Forms Saved\"
Nom1 = "ICS " & family & var & " " & SN & " " & WO & ".docx"
Nom2 = "WS " & family & var & " " & SN & " " & WO & ".docx"
Nom3 = "TR " & family & var & " " & SN & " " & WO & ".docx"

'Open Word files
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc1 = objWord.Documents.Open("C:\Linked ICS, WS, TR\ICS.docx")
Set objDoc2 = objWord.Documents.Open("C:\Linked ICS, WS, TR\Workscope.docx")
Set objDoc3 = objWord.Documents.Open("C:\Linked ICS, WS, TR\Tech Report.docx")

'Unlink word files
objDoc1.Fields.Unlink
objDoc2.Fields.Unlink
objDoc3.Fields.Unlink

'Saved word files
objDoc1.SaveAs (Directory & Nom1)
objDoc2.SaveAs (Directory & Nom2)
objDoc3.SaveAs (Directory & Nom3)

'Close word files
objDoc1.Close
objDoc2.Close
objDoc3.Close
objWord.Quit

End Sub

Texasthierry
02-17-2016, 10:08 AM
Nobody has an idea ? I'm still :banghead:

snb
02-18-2016, 01:15 AM
Please use code tags (in normal plain native language)


Sub M_snb()
with Sheets("DATA")
c00 = .cells(1,11) & .cells(6,2) & " " & cells(11,2) & " " & .Cells(7, 2) & ".docx"
end with

for j=1 to 3
with getobject("C:\Linked ICS_WS_TR\" & choose(j,"ICS","Workscope","Tech Report") & ".docx")
for each it in .storyranges
it.fields.unlink
next
.saveas "C:\Forms Saved\" &choose(j,"ICS","WS","TR") & c00
.close 0
end with
next
End Sub

NB. I don't think a comma is a valid character in a filename.

gmayor
02-18-2016, 01:50 AM
As snb's post implies there are multiple story ranges in a Word document and your code only addresses the main document body. Word VBA can also be rather stubborn when it comes to processing story ranges, so while snb's code may work for you, it equally may miss some of the story ranges, and if you have a Table of Contents, Table of Figures etc, they will almost certainly need to be processed separately. However as your post mentions only headers, let's go with that e.g. Dim oStory as Object


For Each oStory In objDoc1.StoryRanges
oStory.Fields.Unlink
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Unlink
Wend
End If
Next oStory

Texasthierry
02-18-2016, 02:15 PM
As snb's post implies there are multiple story ranges in a Word document and your code only addresses the main document body. Word VBA can also be rather stubborn when it comes to processing story ranges, so while snb's code may work for you, it equally may miss some of the story ranges, and if you have a Table of Contents, Table of Figures etc, they will almost certainly need to be processed separately. However as your post mentions only headers, let's go with that e.g. Dim oStory as Object


For Each oStory In objDoc1.StoryRanges
oStory.Fields.Unlink
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Unlink
Wend
End If
Next oStory

Thank you very much, it works just perfect !