vb.netvisual-studio-2010

Visual Basic 2010 - "function" not declared


I started a few days ago to write a Macro. It basically only consists of an UI in which you can choose some options and based on those it gives you a couple of pages of a word document. It will be started from the .exe in the bin/release folder of the project.

I want to add a footer and a header for each page, but I have not been able to do so.

Whenever I want to use a functionality similar to: "wdHeaderFooterPrimary" as in:

objWord = CreateObject("Word.Application")
objDoc = objWord.Documents.Add
objWord.Visible = True
objWord.Selection.TypeText("")

With objWord.Selection
    .Paragraphs.Alignment = 2
    .Font.size = 9
    .Font.Name = "Arial"
End With

With objWord.Sections(1)
    .Headers(wdHeaderFooterPrimary).Range.Text = "Header text"
    .Footers(wdHeaderFooterPrimary).Range.Text = "Footer text"
End With

It gives me an error, underlining the "wdHeader....". Up to now I have always found a workaround whenever I needed a functionality beginning with "wd...". But now I seem to have hit a wall. (I got some code from: https://msdn.microsoft.com/en-us/library/office/aa221968(v=office.11).aspx )

Can someone explain to me how to use it properly? Do I have to change settings? I am using Visual Basic 2010 Express to write the VBA code.

Edit: The exact error is:

"wdheaderFooterPrimary" was not declared.
(translation following for 2nd part:) Perhaps the object could not be accessed based on its security level (?)


Solution

  • I ended up creating a wordtemplate.dot file where I included the header/footer as I wanted and loading it into the application when creating a new file.