Word 2007 Application.ActivePrinterは、デフォルトのプリンターがネットワークプリンターであるときに設定されません

StackOverflow https://stackoverflow.com/questions/2337688

質問

Word 2007オートメーションを使用して、ユーザーにプリンターを選択して単語ドキュメントをコンパイルする機能を提供しようとしています。ローカルまたはネットワークプリンターで印刷する機能を示します。ネットワークプリンターは、完全に適格なパス(Printername +ポートがある場合はPrintername +ポート)によってコードで指定されています。

問題は、Windows 2008ターミナルサーバーの変更プリンターがデフォルトの場合に機能しないことです ネットワークプリンター. 。元のデフォルトがaの場合は正常に動作します ローカルプリンター.

ドキュメントを印刷してください。

  1. デフォルトのプリンターをユーザーが望むものに変更します。 (によって達成されました Application.ActivePrinter)
  2. ドキュメントを印刷します。
  3. デフォルトのプリンターを元のデフォルトに戻します。

Word 2007でデフォルトのプリンターをネットワークプリンター(リダイレクトプリンター)に設定すると、プリンターは変更されませんが、Word 2003では機能しますが、それは私の場合は選択肢ではありません。私はこれをWord 2007で動作させなければなりません。

より良い解決策はありますか、それとも私が特に間違っていることはありますか?

サンプルコードは以下にあります。ActivePrinterおよびstrprinternameの変更でブレークポイントを使用してデバッグしようとしました。

サンプルコードの参照:

http://www.erlandsendata.no/english/index.php?d=envbaprintchangeprinter

http://www.ozgrid.com/forum/showthread.php?t=68990

サンプルコード:

Option Explicit

Const PRINTER_ENUM_CONNECTIONS = &H4
Const PRINTER_ENUM_LOCAL = &H2

Private Declare Function EnumPrinters Lib "winspool.drv" Alias "EnumPrintersA" _
        (ByVal flags As Long, ByVal name As String, ByVal Level As Long, _
        pPrinterEnum As Long, ByVal cdBuf As Long, pcbNeeded As Long, _
        pcReturned As Long) As Long

Private Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyA" _
        (ByVal RetVal As String, ByVal Ptr As Long) As Long

Private Declare Function StrLen Lib "kernel32" Alias "lstrlenA" _
       (ByVal Ptr As Long) As Long


Public Function ListPrinters() As Variant

Dim bSuccess As Boolean
Dim iBufferRequired As Long
Dim iBufferSize As Long
Dim iBuffer() As Long
Dim iEntries As Long
Dim iIndex As Long
Dim strPrinterName As String
Dim iDummy As Long
Dim iDriverBuffer() As Long
Dim StrPrinters() As String

iBufferSize = 3072

ReDim iBuffer((iBufferSize \ 4) - 1) As Long

'EnumPrinters will return a value False if the buffer is not big enough
bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
        PRINTER_ENUM_LOCAL, vbNullString, _
        1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)

If Not bSuccess Then
    If iBufferRequired > iBufferSize Then
        iBufferSize = iBufferRequired
        Debug.Print "iBuffer too small. Trying again with "; _
        iBufferSize & " bytes."
        ReDim iBuffer(iBufferSize \ 4) As Long
    End If
    'Try again with new buffer
    bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
            PRINTER_ENUM_LOCAL, vbNullString, _
            1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
End If

If Not bSuccess Then
    'Enumprinters returned False
    MsgBox "Error enumerating printers."
    Exit Function
Else
    'Enumprinters returned True, use found printers to fill the array
    ReDim StrPrinters(iEntries - 1)
    For iIndex = 0 To iEntries - 1
        'Get the printername
        strPrinterName = Space$(StrLen(iBuffer(iIndex * 4 + 2)))
        iDummy = PtrToStr(strPrinterName, iBuffer(iIndex * 4 + 2))
        StrPrinters(iIndex) = strPrinterName
    Next iIndex
End If

ListPrinters = StrPrinters

End Function


'You could call the function as follows:




Sub Test()

Dim StrPrinters As Variant, x As Long
Dim strPrinterName As String

StrPrinters = ListPrinters

'Fist check whether the array is filled with anything, by calling another function, IsBounded.
If IsBounded(StrPrinters) Then
    For x = LBound(StrPrinters) To UBound(StrPrinters)
        Debug.Print StrPrinters(x)

     ' Message out Printer name
        strPrinterName = StrPrinters(x)

      ' Message otu Active Printer
        Application.ActivePrinter = GetFullNetworkPrinterName(strPrinterName)


    Next x
Else
    Debug.Print "No printers found"
End If

End Sub




Public Function IsBounded(vArray As Variant) As Boolean

    'If the variant passed to this function is an array, the function will return True;
    'otherwise it will return False
    On Error Resume Next
    IsBounded = IsNumeric(UBound(vArray))

End Function


Function GetFullNetworkPrinterName(strNetworkPrinterName As String) As String
' returns the full network printer name
' returns an empty string if the printer is not found
' e.g. GetFullNetworkPrinterName("HP LaserJet 8100 Series PCL")
' might return "HP LaserJet 8100 Series PCL on Ne04:"
Dim strCurrentPrinterName As String, strTempPrinterName As String, i As Long
    strCurrentPrinterName = Application.ActivePrinter
    i = 0
    Do While i < 100
        strTempPrinterName = strNetworkPrinterName & " on Ne" & Format(i, "00") & ":"
        On Error Resume Next ' try to change to the network printer
        Application.ActivePrinter = strTempPrinterName
        On Error GoTo 0
        If Application.ActivePrinter = strTempPrinterName Then
            ' the network printer was found
            GetFullNetworkPrinterName = strTempPrinterName
            i = 100 ' makes the loop end
        End If
        i = i + 1
    Loop
    ' remove the line below if you want the function to change the active printer
    'Application.ActivePrinter = strCurrentPrinterName ' change back to the original printer
End Function
役に立ちましたか?

解決 2

最終的には、この問題を単語以外で修正しました。現在、Win32 APIを直接使用してから、単語を呼び出して印刷のみを実行します。

他のヒント

以下のコードを使用して、クライアントオブジェクトモデルを使用して指定された文書ライブラリを通過できます。

using Microsoft.SharePoint.Client;

class Program
    {
        static void Main()
        {
            using (ClientContext clientContext = new ClientContext("Your Site URL "))
            {
                List docList = clientContext.Web.Lists.GetByTitle("Document Library Name");
                clientContext.Load(docList); CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View Scope='RecursiveAll'></View>";
                ListItemCollection listItems = docList.GetItems(camlQuery);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                foreach (ListItem listItem in listItems)
                Console.WriteLine("Id: {0} ", listItem.Id);
            }
            Console.ReadLine();
        }
    }
.

上記のコードは、指定された文書ライブラリのフォルダ、サブフォルダを通過し、アイテムIDを印刷します。コードを実行する前に、次のDLLのへの参照を追加してください。

1. Microsoft.SharePoint.Client.dll 
2. Microsoft.SharePoint.Client.Runtime.dll 
.

フォルダNDサブフォルダの内部にフォルダとサブフォルダ名と項目名を表示するには、以下のコードを使用してください。

using (ClientContext ctx = new ClientContext("Your site name"))
            {
                Web web = ctx.Web;
                ctx.Load(web);
                Microsoft.SharePoint.Client.List docList = web.Lists.GetByTitle("Document library Name");
                ctx.Load(docList);
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View Scope='RecursiveAll'></View>";
                ListItemCollection listCol = docList.GetItems(camlQuery);
                ctx.Load(listCol);
                ctx.ExecuteQuery();
                foreach (Microsoft.SharePoint.Client.ListItem item in listCol)
                {
                    Console.WriteLine("FileDirRef: {0}", item["FileDirRef"]);
                    Console.WriteLine("FileLeafRef: {0}", item["FileLeafRef"]);
                    Console.WriteLine();
                }
                Console.ReadKey();
            }
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top