2012-05-09 63 views
1

我正在尋找一種方法將數據從電子表格(通過使用字典添加)傳輸到Word文檔中的設置書籤。使用字典和書籤將數據從Excel傳輸到Word

最終產品需要將每一行數據迭代倒在電子表格中的數據轉換爲字當前書籤-then-刪除-then-繼續循環之前的書籤-then-填寫...

但是,如果任何人都可以幫助我將數據從excel傳遞到單詞中的書籤,我真的很感激它,因爲我可以輕鬆地從那裏運行它。

這裏是我到目前爲止(我在代碼底部卡住吧!):

Dim columnLocations As New Dictionary 
Dim bookmarkOrder As New Dictionary 
Dim solutionWorkbook As Workbook 

Sub Main() 
    Set solutionWorkbook = ActiveWorkbook 

    columnLocations.RemoveAll 
    bookmarkOrder.RemoveAll 

    Call PopulateColumnLocations 
    Call PopulateBookmarkMappings 
    Call DictionaryData 
End Sub 

Sub PopulateColumnLocations() 
    'Loop through row1 to populate dictionary. key = header name, value = column number 
    Sheets("Data").Select 
    For Each cell In solutionWorkbook.Worksheets("Data").Range("A10", Range("A10").End(xlToRight)).Cells 
    columnLocations.Add Trim(cell.Value), cell.Column 
    Next 
End Sub 

Sub PopulateBookmarkMappings() 
    'Loop through row1 to populate dictionary. key = header name, value = collumn number 
    Sheets("Mappings").Select 
    Dim Var As Object 
    Dim Key As Object 

    For Each cell In solutionWorkbook.Worksheets("Mappings").Range("A2", Range("A2").End(xlDown)).Cells 
    Debug.Print Cells(cell.Row, 2).Value 
    Debug.Print cell.Value 
    bookmarkOrder.Add Trim(cell.Value), Cells(cell.Row, 2).Value 'the 2 is the column which has the bookmark name in 
    Next 
End Sub 

Sub DictionaryData() 
    Sheets("Data").Select 
    Dim count As Integer 
    count = 1 
    'Loop through all rows in input data sheet 
    For Each cell In solutionWorkbook.Worksheets("Data").Range("A11", Range("A11").End(xlDown)).Cells 
    Dim TweetSummary As String 
    TweetSummary = solutionWorkbook.Sheets("Data").Cells(cell.Row, columnLocations.Item("Summary")).Value 

    Dim TweetDate As String 
    TweetDate = solutionWorkbook.Sheets("Data").Cells(cell.Row, columnLocations.Item("Date")).Value 

    Dim TweetURL As String 
    TweetURL = solutionWorkbook.Sheets("Data").Cells(cell.Row, columnLocations.Item("URL")).Value 

    Dim TweetFollowers As String 
    TweetFollowers = solutionWorkbook.Sheets("Data").Cells(cell.Row, columnLocations.Item("Twitter Followers")).Value 

    Dim TweetFollowing As String 
    TweetFollowing = solutionWorkbook.Sheets("Data").Cells(cell.Row, columnLocations.Item("Twitter Following")).Value 

    Dim TweetTweets As String 
    TweetTweets = solutionWorkbook.Sheets("Data").Cells(cell.Row, columnLocations.Item("Twitter Tweets")).Value 

    Next 
End Sub 

Sub Worddoc() 
    Dim LaunchWord As Object 
    Dim tweetWord As Object 
    Dim Path As String 
    Dim tBookmark As Bookmark 
    Path = solutionWorkbook.Path & "\B_watch_social_twitter_template.dot" 

    Set LaunchWord = CreateObject("Word.Application") 
    Set tweetWord = LaunchWord.Documents.Add(Path) 

    tweetWord.Select 

''IM STUCK HERE!!! 
End Sub 
+0

看起來您在Excel中有多行數據,但尚不清楚Word模板的設置方式或您希望如何填充它。在'CreateTemplate'中,你正在填充一堆字符串變量,然後對它們無所作爲? –

+0

現在讓我們只是說,單詞模板只是設置了一個單詞包裝在書籤中,名爲「B_twitter_date」 - 就像一個快速運行。 ** Sub PopulateColumnLocations()**檢索標題名稱 ** Sub DictionaryData()**存儲工作表標題下的數據,標題爲'data'以及包含書籤映射的工作表。 ** Sub Worddoc()** 我想將字符串中的數據推送到書籤中,然後迭代下來。 如果這太不倫不類,我可以發送一個示例文件來幫助。 謝謝Tim –

回答