2012-08-13 27 views
0

我正在開發一個VB窗體,它將用戶輸入的word文檔作爲輸入,並將分段打破文檔併爲每個節創建一個新文檔。我面臨以下問題。 1.我正在將word doc中作爲宏運行的代碼改寫爲一個新項目。我得到的錯誤如使用Ms-WORD將VB宏代碼更改爲VB express 2010項目時發生的問題

'Application' is ambiguous, imported from the namespaces or types 'Microsoft.Office.Interop.Word, System.Net.Mime.MediaTypeNames'. 
'wdStory' is not declared. It may be inaccessible due to its protection level. 
Reference to a non-shared member requires an object reference. 
'wdStyleHeading1' is not declared. It may be inaccessible due to its protection level. 
'wdFindStop' is not declared. It may be inaccessible due to its protection level. 
Reference to a non-shared member requires an object reference. 
Reference to a non-shared member requires an object reference. 
'wdSectionBreakContinuous' is not declared. It may be inaccessible due to its protection level. 
'Application' is ambiguous, imported from the namespaces or types 'Microsoft.Office.Interop.Word, System.Net.Mime.MediaTypeNames'. 
Reference to a non-shared member requires an object reference. 
Reference to a non-shared member requires an object reference. 
'wdLine' is not declared. It may be inaccessible due to its protection level. 
'wdExtend' is not declared. It may be inaccessible due to its protection level. 
'wdCharacter' is not declared. It may be inaccessible due to its protection level. 
'ChangeFileOpenDirectory' is not declared. It may be inaccessible due to its protection level. 
'Application' is ambiguous, imported from the namespaces or types 'Microsoft.Office.Interop.Word, System.Net.Mime.MediaTypeNames'. 

我覺得他們都是類似的錯誤,我缺少sm導入庫嗎? 請讓我知道任何必須顯式導入的庫/類。 也找到代碼beloow。

Imports System.Net.Mime.MediaTypeNames 
Imports System.Runtime.InteropServices 
Imports Word = Microsoft.Office.Interop.Word 
Imports Microsoft.Office.Interop.Word 

Public Class Form1 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    If TextBox1.Text = "" Then 
     MessageBox.Show("Enter a valid file Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 

    End If 
    Dim filextn As String = Nothing 
    Dim myfile As String = Nothing 

    myfile = TextBox1.Text ' not working 

    Dim oWord As New Word.Application 
    Dim odoc As New Word.Document 
    Dim DocNum As Integer 
    Dim onewword As Word.Application 
    Dim onewdoc As Word.Document 

    'Start Word and open the document template. 
    odoc = oWord.Documents.Open("C:\Users\jy11125t\Desktop\MachineLearning\Doc\1.docx") 
    oWord.WindowState = Word.WdWindowState.wdWindowStateNormal 
    oWord.Visible = True 

    ' Used to set criteria for moving through the document by section. 
    Dim iParagraph As Word.Paragraph 
    Dim sel As Word.Selection 

    ' Inserting section break after every section in the open word doc 
    Application.ScreenUpdating = False 
    Selection.HomeKey(Unit:=wdStory) 
    With Selection.Find 
    .ClearFormatting() 
    .Replacement.ClearFormatting() 
    .Format = True 
    ' You can specify another style if you like 
    .Style = odoc.Styles(wdStyleHeading1) 
    .Forward = True 
    .Wrap = wdFindStop 
    Do While .Execute 
     Selection.Collapse() 
     If Selection.Start > 0 Then 
      Selection.InsertBreak(Type:=wdSectionBreakContinuous) 
      End If 
      .Execute() 
     Loop 
    End With 
    Application.ScreenUpdating = True 


    ' Application.Browser.Target = wdBrowseSection 

    'A mail merge document ends with a section break next page. 
    'Subtracting one from the section count stop error message. 
    For i = 1 To ((onewdoc.Sections.Count) - 1) 

     'Note: If a document does not end with a section break, 
     'substitute the following line of code for the one above: 
     'For I = 1 To ActiveDocument.Sections.Count 

     'Select and copy the section text to the clipboard. 
     onewdoc.Bookmarks("\Section").Range.Copy() 
     'For Each iParagraph In Word.Selection.Paragraphs 
     'If iParagraph.Style = ActiveDocument.Styles("Heading 1") Then 
     ' filenam = iParagraph.Range.Text 
     ' filenam = Left(filenam, Len(filenam) - 1) 
     ' End If 

     'Create a new document to paste text from clipboard. 
     onewword = CreateObject("Word.Application") 
     onewword.Visible = True 
     onewdoc = onewword.Documents.Add 
     onewdoc.paste() 

     Documents.Add() 
     Selection.Paste() 

     ' Removes the break that is copied at the end of the section, if any. 
     Selection.MoveUp(Unit:=wdLine, Count:=1, Extend:=wdExtend) 
     Selection.Delete(Unit:=wdCharacter, Count:=1) 
     ChangeFileOpenDirectory("C:\Users\jy11125t\Desktop\MachineLearning\Doc\Split_files") 
     DocNum = DocNum + 1 
     onewdoc.SaveAs(FileName:=DocNum & ".doc") 
     onewdoc.Close() 
     ' Move the selection to the next section in the document. 
     Application.Browser.Next() 
     'Next iParagraph 
    Next i 
    ' ActiveDocument.Close savechanges:=wdDoNotSaveChanges 

    'Me.Close() 

End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
    OpenFileDialog1.ShowDialog() 
    TextBox1.Text = OpenFileDialog1.FileName 

End Sub 

回答

0

不像在VB6,.NET中的字常量(wdExtend,WdStory等)不會自動只是通過增加一個參考變得可用。他們仍爲,但您必須用它們派生自的命名空間來限定它們,它們並不都在同一個地方。找出它們所在位置的最簡單方法是使用對象瀏覽器進行搜索(按F2)。例如:

Imports Microsoft.Office.Interop.Word 

然後你只需要知道每個項目屬於哪個分支:wdStory位於Microsoft.Office.Interop.Word.wdUnits.wdStory

您可以通過類代碼外進口的共同部分跳過命名空間出線於:

WdUnits.wdStory 
WdBuiltinStyle.wdStyleHeading 
WdFindWrap.wdFindStop 
WdBreakType.wdSectionBreakContinuous 
WdUnits.wdLine 
WdMovementType.wdExtend 
WdUnits.wdCharacter 

當然,這仍然顯得相當麻煩,所以另一種是簡單地自己定義的常量,分配可以在對象瀏覽器中找到的值:

Const wdStory As Integer = 6 
Const wdStyleHeading1 As Integer = -2 
Const wdFindStop As Integer = 0 
Const wdSectionBreakContinuous As Integer = 3 
Const wdLine As Integer = 5 
Const wdExtend As Integer = 1 
Const wdCharacter As Integer = 1