2012-05-09 61 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 –

回答