+ Reply to Thread
Results 1 to 2 of 2

help with strange behaviour of macro

  1. #1
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500

    help with strange behaviour of macro

    Hi

    I got a macro that produces 54 word documents which are linked to a workbook full of data. the macro runs perfectly except it does not seem to break the links between word and excel but only on certain tables and graphs.

    this is my code I have at the moment
    Option Explicit

    Sub runReports()
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim rng As Word.Range
    Dim cel As Object
    Dim strStoreDoing As String
    Dim sheet As Worksheet
    Dim counter As Integer

    'open word
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    'loop through cell Stores wanted
    For Each cel In Range("todoStore") ' This is a list of stores which I want to produce
    Sheets("main").Range("e16").Value = cel.Value
    strStoreDoing = Sheets("main").Range("e17").Value


    Application.ScreenUpdating = False


    'miss certain stores
    If (cel.Value + 0) > 1000 Then GoTo donotdoStore
    'open word template
    Set wrdDoc = wrdApp.Documents.Open(ThisWorkbook.Path & "\wrdStore.doc")
    Set rng = wrdApp.ActiveDocument.Range

    'Loop through sheets and autofilter the data according to which store you producing
    For Each sheet In Sheets(Array("Sheet1", "sheet2", "sheet3", _
    "sheet4"))
    sheet.Select
    Range("a1").Select
    Selection.AutoFilter Field:=4, Criteria1:=strstoreDoing

    Next sheet

    'unlink fields
    rng.Fields.Unlink
    'unlink header and footers
    ' doHeadFoot
    ' wrdApp.ActiveDocument.Shapes("Text Box 22").Select
    wrdApp.Selection.Fields.Unlink

    'save as Store name
    wrdDoc.SaveAs (ThisWorkbook.Path & "\reports\" & LCase(strCrtDoing) & " apr 2005 to mar 2006.doc")
    wrdDoc.Close
    'wrdApp.Quit ' close the Word application
    Set wrdDoc = Nothing

    ' turn the Autofilters to All
    For Each sheet In Sheets(Array("Sheet1", "sheet2", "sheet3", _
    "sheet4"))
    sheet.Select
    Range("a1").Select
    Selection.AutoFilter Field:=4

    Next sheet

    Application.ScreenUpdating = True
    Sheets("main").Activate
    Range("c13").Value = cel.Value
    Next cel
    donotdoStore:
    Set wrdApp = Nothing
    End Sub

    Sub doHeadFoot()
    Dim oField As Field
    Dim oSection As Section
    Dim oHeader As HeaderFooter
    Dim oFooter As HeaderFooter
    Dim x As Integer
    Dim z

    For Each oSection In ActiveDocument.Sections
    x = x + 1
    If x = 1 Then GoTo missfirst:

    For Each oHeader In oSection.Headers
    If oHeader.Exists Then
    If oHeader.Index = 0 Then GoTo missheader:
    For Each oField In oHeader.Range.Fields
    oField.Unlink
    Next oField
    missheader:
    End If
    Next oHeader

    For Each oFooter In oSection.Footers
    If oFooter.Exists Then
    If oFooter.Index = 0 Then GoTo missfooter:
    For Each oField In oFooter.Range.Fields
    If Left(oField.Code, 5) = " LINK" Then oField.Unlink
    Next oField
    missfooter:
    End If
    Next oFooter

    missfirst:
    Next oSection


    End Sub




    Any help much appreciated.

  2. #2
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500
    can anybody help this is really causing me a problem,. it almost seems as if it only selects certain things to unlink.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1